marking/knight1b_test.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 21 Dec 2016 01:19:25 +0000
changeset 93 21f41e08457d
parent 86 f8a781322499
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.concurrent._
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import scala.concurrent.duration._
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import ExecutionContext.Implicits.global
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import scala.language.postfixOps 
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
     7
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
     8
def count_all_tours(dim: Int) = {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
     9
  for (i <- (0 until dim).toList; 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    10
       j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
85
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
}
fd3f8581ce85 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    13
def add_pair_urban(x: Pos)(y: Pos): Pos = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    14
  (x._1 + y._1, x._2 + y._2)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    15
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    16
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    17
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    18
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    19
def moves_urban(x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    20
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    21
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    22
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    23
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    24
  moves_urban(x).filter(is_legal_urban(dim, path))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    25
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    26
def correct_urban(dim: Int)(p: Path): Boolean = p match {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    27
  case Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    28
  case x::Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    29
  case x::y::p => 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: 85
diff changeset
    30
}
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    31
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    32
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    33
lazy val f = Future {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    34
  assert(count_all_tours(1) == List(1))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    35
  assert(count_all_tours(2) == List(0, 0, 0, 0))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    36
  assert(count_all_tours(3) == List(0, 0, 0, 0, 0, 0, 0, 0, 0))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    37
  assert(count_all_tours(4) == List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    38
  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))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    39
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    40
  val ts = enum_tours(5, List((0, 2)))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    41
  assert(ts.map(correct_urban(5)).forall(_ == true) == true)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    42
  assert(ts.length == 56)  
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    43
}
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    44
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 85
diff changeset
    45
Await.result(f, 360 second)