Last active
December 18, 2021 18:23
-
-
Save MarkCLewis/f51675a62f1a14f1fb5bed555663f350 to your computer and use it in GitHub Desktop.
Alternate Lambda Scala Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return a list of functions for attacks. Max roll takes 10% of health. | |
* Other values do that much damage. | |
*/ | |
def attack(numTimes: Int, dieSides: Int): Seq[Int => Int] = | |
for i <- 1 to numTimes yield | |
val d = util.Random.between(1, dieSides + 1) | |
if d == dieSides then | |
(hits: Int) => math.max(d, hits / 10) | |
else | |
(hits: Int) => d | |
@main def start = | |
val damage = attack(5, 6) | |
var hp = 200 | |
for d <- damage do | |
hp -= d(hp) | |
println(hp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment