marking/knight3b_test.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 16 Dec 2016 13:25:08 +0000
changeset 87 9384b8c98014
parent 86 marking/knight2b_test.scala@f8a781322499
child 88 990a1531135d
permissions -rw-r--r--
updatd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.concurrent._
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import scala.concurrent.duration._
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import ExecutionContext.Implicits.global
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.language.postfixOps 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def add_pair_urban(x: Pos)(y: Pos): Pos = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  (x._1 + y._1, x._2 + y._2)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def moves_urban(x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  moves_urban(x).filter(is_legal_urban(dim, path))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def correct_urban(dim: Int)(p: Path): Boolean = p match {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  case Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case x::Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case x::y::p => 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
    if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
}
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
87
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
    28
def correct_closed_urban(dim: Int)(path: Path): Boolean = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
    29
  correct_urban(dim)(path) && moves_urban(path.head).contains(path.last)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
    30
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
lazy val f = Future {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
87
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
    34
  val tsc = first_tour(6, List((3, 3))).get
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
    35
  assert(correct_closed_urban(6)(tsc) == true)
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
Await.result(f, 300 second)