# HG changeset patch # User Christian Urban # Date 1478093186 0 # Node ID c1e6123d02f46529942ca6d55310872326b8e385 # Parent f31c22f4f1047e2b0965d1ad3ce777d83b7e541b updated diff -r f31c22f4f104 -r c1e6123d02f4 progs/knight2.scala --- a/progs/knight2.scala Wed Nov 02 13:22:40 2016 +0000 +++ b/progs/knight2.scala Wed Nov 02 13:26:26 2016 +0000 @@ -1,6 +1,8 @@ import scala.util._ +type Pos = (Int, Int) -def print_board(n: Int)(steps: List[(Int, Int)]): Unit = { + +def print_board(n: Int)(steps: List[Pos]): Unit = { for (i <- 0 until n) { for (j <- 0 until n) { print(f"${steps.indexOf((i, j))}%3.0f ") @@ -11,26 +13,26 @@ System.exit(0) } -def add_pair(x: (Int, Int))(y: (Int, Int)) = +def add_pair(x: Pos)(y: Pos) = (x._1 + y._1, x._2 + y._2) -def is_legal(n: Int)(x: (Int, Int)) = +def is_legal(n: Int)(x: Pos) = 0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n -def moves(n: Int)(x: (Int, Int)): List[(Int, Int)] = { +def moves(n: Int)(x: Pos): List[Pos] = { List((1, 2),(2, 1),(2, -1),(1, -2), (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)) } -def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = - moves(n)(x).sortBy((x: (Int, Int)) => moves(n)(x).filterNot(steps.contains(_)).length) +def ordered_moves(n: Int)(steps: List[Pos])(x : Pos): List[Pos] = + moves(n)(x).sortBy((x: Pos) => moves(n)(x).filterNot(steps.contains(_)).length) moves(8)(1,3) ordered_moves(8)(Nil)(1,3) ordered_moves(8)(List((2, 4), (2, 6)))(1,3) -// non-circle tour parallel -def tour(n: Int)(steps: List[(Int, Int)]): Unit = { +// non-circle tour +def tour(n: Int)(steps: List[Pos]): Unit = { if (steps.length == n * n) print_board(n)(steps) else