Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active January 4, 2016 13:02
Show Gist options
  • Select an option

  • Save ElectricCoffee/86f8b05dd3eeeba56cf3 to your computer and use it in GitHub Desktop.

Select an option

Save ElectricCoffee/86f8b05dd3eeeba56cf3 to your computer and use it in GitHub Desktop.
Based on Dr. James Grime's nontransitive dice (aka. "Grime Dice") I set to test if they actually work as advertised, by throwing every possible combination 1000 times and printing the result on-screen. (spoiler alert they work). The Die class works on all dice of all sizes, with any numeric value on the faces
package com.wausoft.core
import com.wausoft.dice.Die
import com.wausoft.dice.Die.Multiple
object Program extends App {
// define the five different Grime Dice
val red = new Die("red" , 4, 4, 4, 4, 4, 9)
val olive = new Die("olive" , 5, 5, 5, 5, 5, 0)
val blue = new Die("blue" , 7, 7, 7, 2, 2, 2)
val yellow = new Die("yellow" , 3, 3, 3, 3, 8, 8)
val magenta = new Die("magenta", 6, 6, 6, 6, 1, 1)
// make all the possible different combinations of 2 dice (in this case the total amount is 10)
val combinations = Seq(red, olive, blue, yellow, magenta).combinations(2)
// go through all the combinations and roll the dice 1000 times, then print the outcome
combinations.foreach {
case Seq(a, b) => a printComparisonWith b
}
println() // make break
// define five clusters of two identical dice
val reds = Seq(red , red)
val olives = Seq(olive , olive)
val blues = Seq(blue , blue)
val yellows = Seq(yellow , yellow)
val magentas = Seq(magenta, magenta)
// go through every possible combination of those five clusters, and print the result like before
Seq(reds, olives, blues, yellows, magentas).combinations(2).foreach {
case Seq(a, b) => a printComparisonWith b
}
}
package com.wausoft.dice
import util.Random
// it's called "Die" not "Dice" because the singular of "dice" is "die", "dice" is plural
// the object name is not meant as a threat to anyone
object Die {
// define extension methods for lists of dice
implicit class Multiple(val left: Seq[Die]) extends AnyVal {
def roll() = left.map(_.roll()).reduce(_ + _)
def printComparisonWith(right: Seq[Die]): Unit = {
val rolls = for(_ <- 1 to 1000) yield (left.roll(), right.roll())
val eq = rolls count { case (a, b) => a == b }
val gt = rolls count { case (a, b) => a > b }
val lt = rolls count { case (a, b) => a < b }
println(s"For [${left mkString ", "}] compared to [${right mkString ", "}]\n Greater: $gt, Equivalent: $eq, Lesser: $lt")
}
}
}
class Die(val colour: String, values: Int*) {
val faces = values.size
def roll(): Int = values(Random nextInt faces)
def rollTimes(n: Int): Int = (1 to n).map(_ => roll()).reduce(_ + _)
def this(col: String, vals: Range) = this(col, vals: _*)
def printComparisonWith(that: Die): Unit = Seq(this) printComparisonWith Seq(that)
override def toString = colour
}
For [red] compared to [olive]
Greater: 317, Equivalent: 0, Lesser: 683
For [red] compared to [blue]
Greater: 581, Equivalent: 0, Lesser: 419
For [red] compared to [yellow]
Greater: 713, Equivalent: 0, Lesser: 287
For [red] compared to [magenta]
Greater: 433, Equivalent: 0, Lesser: 567
For [olive] compared to [blue]
Greater: 411, Equivalent: 0, Lesser: 589
For [olive] compared to [yellow]
Greater: 548, Equivalent: 0, Lesser: 452
For [olive] compared to [magenta]
Greater: 306, Equivalent: 0, Lesser: 694
For [blue] compared to [yellow]
Greater: 358, Equivalent: 0, Lesser: 642
For [blue] compared to [magenta]
Greater: 673, Equivalent: 0, Lesser: 327
For [yellow] compared to [magenta]
Greater: 549, Equivalent: 0, Lesser: 451
For [red, red] compared to [olive, olive]
Greater: 510, Equivalent: 0, Lesser: 490
For [red, red] compared to [blue, blue]
Greater: 436, Equivalent: 0, Lesser: 564
For [red, red] compared to [yellow, yellow]
Greater: 581, Equivalent: 0, Lesser: 419
For [red, red] compared to [magenta, magenta]
Greater: 690, Equivalent: 0, Lesser: 310
For [olive, olive] compared to [blue, blue]
Greater: 587, Equivalent: 0, Lesser: 413
For [olive, olive] compared to [yellow, yellow]
Greater: 312, Equivalent: 0, Lesser: 688
For [olive, olive] compared to [magenta, magenta]
Greater: 411, Equivalent: 0, Lesser: 589
For [blue, blue] compared to [yellow, yellow]
Greater: 470, Equivalent: 0, Lesser: 530
For [blue, blue] compared to [magenta, magenta]
Greater: 528, Equivalent: 0, Lesser: 472
For [yellow, yellow] compared to [magenta, magenta]
Greater: 414, Equivalent: 0, Lesser: 586
Process finished with exit code 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment