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