progs/knight.scala
changeset 1 b595d654cb2d
parent 0 02f53f76f828
child 2 e0ec73c462c7
equal deleted inserted replaced
0:02f53f76f828 1:b595d654cb2d
    16 
    16 
    17 def is_legal(n: Int)(x: (Int, Int)) = 
    17 def is_legal(n: Int)(x: (Int, Int)) = 
    18   0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
    18   0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
    19 
    19 
    20 def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = {
    20 def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = {
    21   Set((1, 2),(2, 1),(2, -1),(1, -2),
    21   List((1, 2),(2, 1),(2, -1),(1, -2),
    22       (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
    22        (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n))
    23 }
    23 }
    24 
    24 
    25 def tour(n: Int)(steps: List[(Int, Int)]): Unit = {
    25 def tour(n: Int)(steps: List[(Int, Int)]): Unit = {
    26   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
    26   if (steps.length ==  n * n && moves(n)(steps.head).contains(steps.last))
    27     print_board(n)(steps)
    27     print_board(n)(steps)