Skip to content

Instantly share code, notes, and snippets.

@MarkCLewis
Last active December 18, 2021 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkCLewis/f51675a62f1a14f1fb5bed555663f350 to your computer and use it in GitHub Desktop.
Save MarkCLewis/f51675a62f1a14f1fb5bed555663f350 to your computer and use it in GitHub Desktop.
Alternate Lambda Scala Example
/**
* 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