marking/knight1b_test.scala
changeset 86 f8a781322499
parent 85 fd3f8581ce85
--- a/marking/knight1b_test.scala	Thu Dec 08 12:50:54 2016 +0000
+++ b/marking/knight1b_test.scala	Tue Dec 13 13:02:52 2016 +0000
@@ -4,16 +4,42 @@
 import ExecutionContext.Implicits.global
 import scala.language.postfixOps 
 
-lazy val f = Future {
-  assert(legal_moves(8, Nil, (2,2)) == 
-             List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
-  assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
-  assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
-             List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
-  assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
-  assert(legal_moves(1, Nil, (0,0)) == List())
-  assert(legal_moves(2, Nil, (0,0)) == List())
-  assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
+
+def count_all_tours(dim: Int) = {
+  for (i <- (0 until dim).toList; 
+       j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
 }
 
-Await.result(f, 120 second)
+def add_pair_urban(x: Pos)(y: Pos): Pos = 
+  (x._1 + y._1, x._2 + y._2)
+
+def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
+  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
+
+def moves_urban(x: Pos): List[Pos] = 
+  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
+       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
+
+def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
+  moves_urban(x).filter(is_legal_urban(dim, path))
+
+def correct_urban(dim: Int)(p: Path): Boolean = p match {
+  case Nil => true
+  case x::Nil => true
+  case x::y::p => if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
+}
+
+
+lazy val f = Future {
+  assert(count_all_tours(1) == List(1))
+  assert(count_all_tours(2) == List(0, 0, 0, 0))
+  assert(count_all_tours(3) == List(0, 0, 0, 0, 0, 0, 0, 0, 0))
+  assert(count_all_tours(4) == List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
+  assert(count_all_tours(5) == List(304, 0, 56, 0, 304, 0, 56, 0, 56, 0, 56, 0, 64, 0, 56, 0, 56, 0, 56, 0, 304, 0, 56, 0, 304))
+
+  val ts = enum_tours(5, List((0, 2)))
+  assert(ts.map(correct_urban(5)).forall(_ == true) == true)
+  assert(ts.length == 56)  
+}
+
+Await.result(f, 360 second)