Try an extension method

This commit is contained in:
Luke Hubmayer-Werner 2022-12-02 19:30:06 +10:30
parent 0528d4b50e
commit cdb80d9b9d
1 changed files with 5 additions and 3 deletions

View File

@ -1,16 +1,18 @@
import scala.io.Source
import scala.math.floorMod
extension (i: Int) def %%(modulus: Int) = floorMod(i, modulus)
enum RPS(val score: Int):
case Rock extends RPS(1)
case Paper extends RPS(2)
case Scissors extends RPS(3)
def +(amount: Int) = RPS.fromOrdinal(floorMod(ordinal + amount, 3))
def -(amount: Int) = RPS.fromOrdinal(floorMod(ordinal - amount, 3))
def +(amount: Int) = RPS.fromOrdinal((ordinal + amount) %% 3)
def -(amount: Int) = RPS.fromOrdinal((ordinal - amount) %% 3)
def -(other: RPS) = score - other.score
def vs(other: RPS) = floorMod(this - other, 3) match
def vs(other: RPS) = (this - other) %% 3 match
case 1 => 6
case 2 => 0
case _ => 3