testing3/knight_test5.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 28 Nov 2018 23:26:47 +0000
changeset 220 3020f8c76baa
child 296 12dc251fc47e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//type Pos = (Int, Int)    // a position on a chessboard 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
//type Path = List[Pos]    // a path...a list of positions
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
def add_pair_urban(x: Pos)(y: Pos): Pos = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  (x._1 + y._1, x._2 + y._2)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  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
    10
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
def moves_urban(x: Pos): List[Pos] = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  moves_urban(x).filter(is_legal_urban(dim, path))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def correct_urban(dim: Int)(p: Path): Boolean = p match {
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  case Nil => true
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  case x::Nil => true
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  case x::y::p => 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
    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
    23
}
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
val ts1_urban = first_tour(6, List((0, 0))).get
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
assert(correct_urban(6)(ts1_urban) == true)
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
val ts2_urban = first_tour(4, List((0, 0)))
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
assert(ts2_urban == None)