Skip to content

Instantly share code, notes, and snippets.

@akhileshs
Created April 3, 2015 13:15
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 akhileshs/2594fdaa879f1aeee23a to your computer and use it in GitHub Desktop.
Save akhileshs/2594fdaa879f1aeee23a to your computer and use it in GitHub Desktop.
simulating a knight's move
scala> case class KnightPos(c: Int, r: Int)
defined class KnightPos
scala> :pa
// Entering paste mode (ctrl-D to finish)
case class KnightPos(c: Int, r: Int) {
def move: List[KnightPos] =
for {
KnightPos(c2, r2) <- List(KnightPos(c + 2, r - 1), KnightPos(c + 2, r + 1),
KnightPos(c - 2, r - 1), KnightPos(c - 2, r + 1)
,
KnightPos(c + 1, r - 2), KnightPos(c + 1, r + 2),
KnightPos(c - 1, r - 2), KnightPos(c - 1, r + 2)) if (
((1 |-> 8) contains c2) && ((1 |-> 8) contains r2))
} yield KnightPos(c2, r2)
}
// Exiting paste mode, now interpreting.
defined class KnightPos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment