testing2/knight2.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 15 May 2018 10:57:20 +0100
changeset 175 526542d8d700
parent 152 114a89518aea
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 about finding a single tour for a board
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//================================================
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW7b {
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)    // a position on a chessboard 
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]    // a path...a list of positions
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     9
def print_board(dim: Int, path: Path) : Unit = {
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  println
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  for (i <- 0 until dim) {
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    for (j <- 0 until dim) {
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    }
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    println
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  } 
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    19
def add_pair(x: Pos)(y: Pos) : Pos = 
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  (x._1 + y._1, x._2 + y._2)
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    22
def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = 
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    25
def moves(x: Pos) : List[Pos] = 
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    29
def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = 
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  moves(x).filter(is_legal(dim, path))
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    33
def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = xs match {
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  case Nil => None
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  case x::xs => {
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    val result = f(x)
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    if (result.isDefined) result else first(xs, f)
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  }
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
//first(List((1, 0),(2, 0),(3, 0),(4, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//first(List((1, 0),(2, 0),(3, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    44
def first_tour(dim: Int, path: Path) : Option[Path] = {
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  if (path.length == dim * dim) Some(path)
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  else
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
/* 
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
val ts1 = first_tour(8, List((0, 0))).get
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  assert(correct_urban(8)(ts1) == true)
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
val ts2 = first_tour(4, List((0, 0)))
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
assert(ts2 == None)  
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
print_board(8, first_tour(8, List((0, 0))).get)
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
*/
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61