| 45 |      1 | // Part 2 about finding a single tour for a board
 | 
|  |      2 | //================================================
 | 
|  |      3 | 
 | 
| 50 |      4 | type Pos = (Int, Int)    // a position on a chessboard 
 | 
|  |      5 | type Path = List[Pos]    // a path...a list of positions
 | 
| 45 |      6 | 
 | 
|  |      7 | def print_board(dim: Int, path: Path): Unit = {
 | 
|  |      8 |   println
 | 
|  |      9 |   for (i <- 0 until dim) {
 | 
|  |     10 |     for (j <- 0 until dim) {
 | 
|  |     11 |       print(f"${path.reverse.indexOf((i, j))}%3.0f ")
 | 
|  |     12 |     }
 | 
|  |     13 |     println
 | 
|  |     14 |   } 
 | 
|  |     15 | }
 | 
|  |     16 | 
 | 
|  |     17 | def add_pair(x: Pos)(y: Pos): Pos = 
 | 
|  |     18 |   (x._1 + y._1, x._2 + y._2)
 | 
|  |     19 | 
 | 
|  |     20 | def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
 | 
|  |     21 |   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
 | 
|  |     22 | 
 | 
|  |     23 | def moves(x: Pos): List[Pos] = 
 | 
|  |     24 |   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
 | 
|  |     25 |        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
 | 
|  |     26 | 
 | 
|  |     27 | def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
 | 
|  |     28 |   moves(x).filter(is_legal(dim, path))
 | 
|  |     29 | 
 | 
|  |     30 | 
 | 
|  |     31 | def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
 | 
|  |     32 |   case Nil => None
 | 
|  |     33 |   case x::xs => {
 | 
|  |     34 |     val result = f(x)
 | 
|  |     35 |     if (result.isDefined) result else first(xs, f)
 | 
|  |     36 |   }
 | 
|  |     37 | }
 | 
|  |     38 | 
 | 
| 86 |     39 | first(List((1, 0),(2, 0),(3, 0),(4, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
 | 
|  |     40 | first(List((1, 0),(2, 0),(3, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
 | 
|  |     41 | 
 | 
|  |     42 | 
 | 
| 45 |     43 | 
 | 
|  |     44 | def first_tour(dim: Int, path: Path): Option[Path] = {
 | 
|  |     45 |   if (path.length == dim * dim) Some(path)
 | 
|  |     46 |   else
 | 
|  |     47 |     first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
 | 
|  |     48 | }
 | 
|  |     49 | 
 | 
| 66 |     50 | /*
 | 
| 45 |     51 | for (dim <- 1 to 8) {
 | 
|  |     52 |   val t = first_tour(dim, List((0, 0)))
 | 
|  |     53 |   println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
 | 
|  |     54 | }
 | 
| 66 |     55 | */
 | 
| 86 |     56 | 
 | 
|  |     57 | def add_pair_urban(x: Pos)(y: Pos): Pos = 
 | 
|  |     58 |   (x._1 + y._1, x._2 + y._2)
 | 
|  |     59 | 
 | 
|  |     60 | def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
 | 
|  |     61 |   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
 | 
|  |     62 | 
 | 
|  |     63 | def moves_urban(x: Pos): List[Pos] = 
 | 
|  |     64 |   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
 | 
|  |     65 |        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
 | 
|  |     66 | 
 | 
|  |     67 | def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
 | 
|  |     68 |   moves_urban(x).filter(is_legal_urban(dim, path))
 | 
|  |     69 | 
 | 
|  |     70 | def correct_urban(dim: Int)(p: Path): Boolean = p match {
 | 
|  |     71 |   case Nil => true
 | 
|  |     72 |   case x::Nil => true
 | 
|  |     73 |   case x::y::p => 
 | 
|  |     74 |     if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
 | 
|  |     75 | }
 | 
|  |     76 | 
 | 
|  |     77 | 
 | 
|  |     78 | 
 | 
| 45 |     79 |  
 | 
| 86 |     80 | val ts1 = first_tour(8, List((0, 0))).get
 | 
|  |     81 |   assert(correct_urban(8)(ts1) == true)
 | 
|  |     82 | 
 | 
|  |     83 | val ts2 = first_tour(4, List((0, 0)))
 | 
|  |     84 | assert(ts2 == None)  
 | 
|  |     85 | 
 | 
| 66 |     86 | print_board(8, first_tour(8, List((0, 0))).get)
 | 
| 64 |     87 | 
 | 
|  |     88 | 
 |