progs/knight2_sol.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 21 Nov 2022 15:57:45 +0000
changeset 447 f51e593903ac
parent 86 f8a781322499
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 about finding a single tour for a board
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//================================================
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
     4
type Pos = (Int, Int)    // a position on a chessboard 
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
     5
type Path = List[Pos]    // a path...a list of positions
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
def print_board(dim: Int, path: Path): Unit = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  for (i <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    for (j <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  } 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
def add_pair(x: Pos)(y: Pos): Pos = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  (x._1 + y._1, x._2 + y._2)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def moves(x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  moves(x).filter(is_legal(dim, path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  case Nil => None
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  case x::xs => {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    val result = f(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    if (result.isDefined) result else first(xs, f)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    39
first(List((1, 0),(2, 0),(3, 0),(4, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    40
first(List((1, 0),(2, 0),(3, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    41
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    42
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
def first_tour(dim: Int, path: Path): Option[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  if (path.length == dim * dim) Some(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  else
8399976b77fe 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))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 64
diff changeset
    50
/*
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
for (dim <- 1 to 8) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  val t = first_tour(dim, List((0, 0)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 64
diff changeset
    55
*/
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    56
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    57
def add_pair_urban(x: Pos)(y: Pos): Pos = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    58
  (x._1 + y._1, x._2 + y._2)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    59
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    60
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    61
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    62
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    63
def moves_urban(x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    64
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    65
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    66
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    67
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    68
  moves_urban(x).filter(is_legal_urban(dim, path))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    69
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    70
def correct_urban(dim: Int)(p: Path): Boolean = p match {
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    71
  case Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    72
  case x::Nil => true
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    73
  case x::y::p => 
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    74
    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: 66
diff changeset
    75
}
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    76
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    77
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    78
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
 
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    80
val ts1 = first_tour(8, List((0, 0))).get
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    81
  assert(correct_urban(8)(ts1) == true)
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    82
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    83
val ts2 = first_tour(4, List((0, 0)))
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    84
assert(ts2 == None)  
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
    85
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 64
diff changeset
    86
print_board(8, first_tour(8, List((0, 0))).get)
64
d6f97b562424 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    87
d6f97b562424 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    88