main_testing4/knight_test3.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 02 Dec 2020 01:15:14 +0000
changeset 382 1bd800376e0c
parent 347 4de31fdc0d67
child 403 ffce7b61b446
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 284
diff changeset
     1
import CW9a._
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
//type Pos = (Int, Int)    // a position on a chessboard 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
//type Path = List[Pos]    // a path...a list of positions
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
def count_all_tours_urban(dim: Int) = {
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
  for (i <- (0 until dim).toList; 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
       j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
}
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
def add_pair_urban(x: Pos)(y: Pos): Pos = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  (x._1 + y._1, x._2 + y._2)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def moves_urban(x: Pos): List[Pos] = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  moves_urban(x).filter(is_legal_urban(dim, path))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def correct_urban(dim: Int)(p: Path): Boolean = p match {
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case Nil => true
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case x::Nil => true
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case x::y::p => if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
assert(count_all_tours_urban(1) == List(1))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
assert(count_all_tours_urban(2) == List(0, 0, 0, 0))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
assert(count_all_tours_urban(3) == List(0, 0, 0, 0, 0, 0, 0, 0, 0))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
assert(count_all_tours_urban(4) == List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//assert(count_all_tours_urban(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))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
val ts00_urban = enum_tours(5, List((0, 0)))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
assert(ts00_urban.map(correct_urban(5)).forall(_ == true) == true)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
assert(ts00_urban.length == 304)  
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
val ts01_urban = enum_tours(5, List((0, 1)))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
assert(ts01_urban.map(correct_urban(5)).forall(_ == true) == true)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
assert(ts01_urban.length == 0)  
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
val ts02_urban = enum_tours(5, List((0, 2)))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
assert(ts02_urban.map(correct_urban(5)).forall(_ == true) == true)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
assert(ts02_urban.length == 56)