complete lab04

This commit is contained in:
SowinskiBraeden committed 2026-02-08 16:46:54 -08:00
1 parent c72f36f6c3
commit d49a0c9298
14 files changed
+244 -1

No files matched your search

@@ -0,0 +1,41 @@
package ca.bcit.comp3717.lab03
fun printByKey(k: Int, m: Map<Int, String>) {
println(m[k]);
}
val printByValue = fun(v: String?)
{
println(v);
};
val getValue = { m: Map<Int, String>, k: Int -> m[k]; };
fun getAndPrint(g: (m: Map<Int, String>, k: Int) -> String?, m: Map<Int, String>, k: Int)
{
println(g(m, k));
}
fun consumeValue(action: (String) -> Unit, v: String)
{
action(v);
}
fun main()
{
val facts: Map<Int, String> = mapOf(1492 to "Christopher Columbus discovers America",
1601 to "William Shakespear writes Hamlet",
1632 to "Galileo discovered the acceleration of gravity on Earth to be 9.8m/s",
1838 to "Rouphly 9.46 trillion km, the light-year is first used as a measurement in astronomy",
2020 to "Covid 19 Pandemic");
facts.forEach(::println);
println()
printByKey(1492, facts);
printByValue(facts[1601]);
println(getValue(facts, 1632))
getAndPrint(getValue, facts, 1838);
consumeValue(::println, getValue(facts, 2020).toString());
}
@@ -0,0 +1,10 @@
package ca.bcit.comp3717.lab04
class Dwarf : Minion()
{
override val race: String = "Dwarf";
override val baseHealth: Int = 8;
override val baseSpeed: Int = 2;
override val backpackSize: Int = 8;
override val catchphrase: String = "Wheres' me trusty ol hammer?";
}
@@ -0,0 +1,10 @@
package ca.bcit.comp3717.lab04
class Elf : Minion()
{
override val race: String = "Elf";
override val baseHealth: Int = 2;
override val baseSpeed: Int = 8;
override val backpackSize: Int = 3;
override val catchphrase: String = "My arrows never miss!";
}
@@ -0,0 +1,28 @@
package ca.bcit.comp3717.lab04
class Gather(minion: Minion): Mission(minion), Repeatable
{
override fun determineMissionTime(): Int
{
return (minion.backpackSize + minion.baseSpeed) * (1..4).random();
}
override fun reward(time: Int): String
{
return when (time)
{
in 10..21 -> "bronze"
in 22..33 -> "silver"
in 34..50 -> "gold"
else -> "nothing"
};
}
override fun repeat(times: Int, listener: MissionListener): Unit
{
for (i in 1..times)
{
start(listener);
}
}
}
@@ -0,0 +1,20 @@
package ca.bcit.comp3717.lab04
class Hunt(minion: Minion): Mission(minion)
{
override fun determineMissionTime(): Int
{
return (minion.baseHealth + minion.baseSpeed) * (0..4).random();
}
override fun reward(time: Int): String
{
return when (time)
{
in 11..20 -> "mouse"
in 21..30 -> "fox"
in 31..50 -> "buffalo"
else -> "nothing"
};
}
}
@@ -0,0 +1,45 @@
package ca.bcit.comp3717.lab04
fun main()
{
val thing1 = Dwarf();
val thing2 = Elf()
val manager = MissionManager();
val mission1 = Gather(thing1);
val mission2 = Hunt(thing2);
manager.selectRepeatable(mission1, 1, object: MissionListener {
override fun missionStart(minion: Minion): Unit
{
println("A ${minion.race} was sent to gather resources");
}
override fun missionProgress(): Unit
{
println("...");
}
override fun missionComplete(minion: Minion, reward: String): Unit
{
println("A ${minion.race} has returned from a gather and found ${reward}!\n");
}
});
manager.select(mission2, object: MissionListener {
override fun missionStart(minion: Minion): Unit
{
println("A ${minion.race} started a hunt!");
}
override fun missionProgress(): Unit
{
println("...");
}
override fun missionComplete(minion: Minion, reward: String): Unit
{
println("An ${minion.race} has returned from a hunt, and found ${reward}!\n");
}
});
}
@@ -0,0 +1,10 @@
package ca.bcit.comp3717.lab04
abstract class Minion
{
abstract val race: String;
abstract val baseHealth: Int;
abstract val baseSpeed: Int;
abstract val backpackSize: Int;
abstract val catchphrase: String;
}
@@ -0,0 +1,26 @@
package ca.bcit.comp3717.lab04
abstract class Mission(protected val minion: Minion)
{
init
{
println(minion.catchphrase);
}
fun start(listener: MissionListener): Unit
{
val time = determineMissionTime();
val reward = reward(time);
listener.missionStart(minion)
for (i in 0..2)
{
listener.missionProgress();
Thread.sleep(1000);
}
listener.missionComplete(minion, reward);
}
protected abstract fun determineMissionTime(): Int
protected abstract fun reward(time: Int): String
}
@@ -0,0 +1,8 @@
package ca.bcit.comp3717.lab04
interface MissionListener
{
fun missionStart(minion: Minion): Unit
fun missionProgress(): Unit
fun missionComplete(minion: Minion, message: String): Unit
}
@@ -0,0 +1,14 @@
package ca.bcit.comp3717.lab04
class MissionManager
{
fun select(mission: Mission, listener: MissionListener): Unit
{
mission.start(listener);
}
fun selectRepeatable(mission: Repeatable, i: Int, listener: MissionListener): Unit
{
mission.repeat(i, listener);
}
}
@@ -0,0 +1,6 @@
package ca.bcit.comp3717.lab04
interface Repeatable
{
fun repeat(times: Int, listener: MissionListener)
}