marking3/knight3c_test.scala
changeset 243 9bb36426c781
equal deleted inserted replaced
242:e6b34f617915 243:9bb36426c781
       
     1 
       
     2 //import scala.concurrent._
       
     3 //import scala.concurrent.duration._
       
     4 //import ExecutionContext.Implicits.global
       
     5 //import scala.language.postfixOps 
       
     6 
       
     7 println(Runtime.getRuntime.maxMemory)
       
     8 
       
     9 type Pos = (Int, Int)
       
    10 type Path = List[Pos]
       
    11 
       
    12 def add_pair_urban(x: Pos)(y: Pos): Pos = 
       
    13   (x._1 + y._1, x._2 + y._2)
       
    14 
       
    15 def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
       
    16   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
       
    17 
       
    18 def moves_urban(x: Pos): List[Pos] = 
       
    19   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
       
    20        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
       
    21 
       
    22 def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
       
    23   moves_urban(x).filter(is_legal_urban(dim, path))
       
    24 
       
    25 def correct_urban(dim: Int)(p: Path): Boolean = p match {
       
    26   case Nil => true
       
    27   case x::Nil => true
       
    28   case x::y::p => 
       
    29     if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
       
    30 }
       
    31 
       
    32 // !!!!!!! the futures need to be removed
       
    33 //lazy val f1 = Future {
       
    34 
       
    35   val ts8 = CW7c.first_tour_heuristic(8, List((0,0))).get
       
    36   assert(correct_urban(8)(ts8) == true)
       
    37 
       
    38 //}
       
    39 
       
    40 //Await.result(f1, 360 second)
       
    41 
       
    42 
       
    43 //lazy val f2 = Future {
       
    44 
       
    45   val ts40 = CW7c.first_tour_heuristic(40, List((0,0))).get
       
    46   assert(correct_urban(40)(ts40) == true)
       
    47 
       
    48 //}
       
    49 
       
    50 //Await.result(f2, 360 second)