marking2/knight2b_test.scala
changeset 162 6d25ccbb3cf2
equal deleted inserted replaced
161:6ea450e999e2 162:6d25ccbb3cf2
       
     1 
       
     2 //import scala.concurrent._
       
     3 //import scala.concurrent.duration._
       
     4 //import ExecutionContext.Implicits.global
       
     5 //import scala.language.postfixOps 
       
     6 
       
     7 type Pos = (Int, Int)    // a position on a chessboard 
       
     8 type Path = List[Pos]    // a path...a list of positions
       
     9 
       
    10 def add_pair_urban(x: Pos)(y: Pos): Pos = 
       
    11   (x._1 + y._1, x._2 + y._2)
       
    12 
       
    13 def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
       
    14   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
       
    15 
       
    16 def moves_urban(x: Pos): List[Pos] = 
       
    17   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
       
    18        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
       
    19 
       
    20 def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
       
    21   moves_urban(x).filter(is_legal_urban(dim, path))
       
    22 
       
    23 def correct_urban(dim: Int)(p: Path): Boolean = p match {
       
    24   case Nil => true
       
    25   case x::Nil => true
       
    26   case x::y::p => 
       
    27     if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
       
    28 }
       
    29 
       
    30 
       
    31 //lazy val f = Future {
       
    32 
       
    33   val ts1 = CW7b.first_tour(8, List((0, 0))).get
       
    34   assert(correct_urban(8)(ts1) == true)
       
    35 
       
    36   val ts2 = CW7b.first_tour(4, List((0, 0)))
       
    37   assert(ts2 == None)  
       
    38 //}
       
    39 
       
    40 //Await.result(f, 300 second)