| 221 |      1 | // Part 1 about finding Knight's tours
 | 
|  |      2 | //=====================================
 | 
| 220 |      3 | 
 | 
| 221 |      4 | // If you need any auxiliary function, feel free to 
 | 
|  |      5 | // implement it, but do not make any changes to the
 | 
|  |      6 | // templates below. Also have a look whether the functions
 | 
|  |      7 | // at the end are of any help.
 | 
|  |      8 | 
 | 
| 220 |      9 | 
 | 
| 222 |     10 | 
 | 
| 220 |     11 | type Pos = (Int, Int)    // a position on a chessboard 
 | 
|  |     12 | type Path = List[Pos]    // a path...a list of positions
 | 
|  |     13 | 
 | 
| 221 |     14 | //(1) Complete the function that tests whether the position x
 | 
|  |     15 | //    is inside the board and not yet element in the path.
 | 
| 220 |     16 | 
 | 
| 222 |     17 | def is_legal(dim: Int, path: Path, x: Pos) : Boolean = {
 | 
|  |     18 |  if ((x._1 < dim && x._2 < dim) && !(path.contains(x)))
 | 
|  |     19 |   true 
 | 
|  |     20 |     else 
 | 
|  |     21 |   false
 | 
|  |     22 | }
 | 
| 221 |     23 | 
 | 
|  |     24 | 
 | 
|  |     25 | 
 | 
|  |     26 | //(2) Complete the function that calculates for a position x
 | 
|  |     27 | //    all legal onward moves that are not already in the path. 
 | 
|  |     28 | //    The moves should be ordered in a "clockwise" manner.
 | 
| 222 |     29 |  
 | 
| 221 |     30 | 
 | 
| 222 |     31 | def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
 | 
|  |     32 |   val legalMovesList = List((x._1 + 1, x._2 + 2), (x._1 + 2, x._2 + 1), (x._1 + 2, x._2 - 1), (x._1 + 1, x._2 - 2), (x._1 - 1, x._2 - 2), (x._1 - 2, x._2 - 1), (x._1 - 2, x._2 + 1), (x._1 - 1, x._2 + 2))
 | 
| 221 |     33 | 
 | 
| 222 |     34 |   for (i <- legalMovesList
 | 
|  |     35 |     if (is_legal(dim, path, i)))
 | 
|  |     36 |       yield i
 | 
|  |     37 | 
 | 
| 221 |     38 | }
 | 
|  |     39 | 
 | 
| 222 |     40 | 
 | 
| 221 |     41 | //some test cases
 | 
|  |     42 | //
 | 
| 222 |     43 | //assert(legal_moves(8, Nil, (2,2)) == 
 | 
|  |     44 | //  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
 | 
| 221 |     45 | //assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
 | 
| 222 |     46 | //assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
 | 
|  |     47 | //  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
 | 
| 221 |     48 | //assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
 | 
|  |     49 | 
 | 
|  |     50 | 
 | 
|  |     51 | //(3) Complete the two recursive functions below. 
 | 
|  |     52 | //    They exhaustively search for knight's tours starting from the 
 | 
|  |     53 | //    given path. The first function counts all possible tours, 
 | 
|  |     54 | //    and the second collects all tours in a list of paths.
 | 
|  |     55 | 
 | 
|  |     56 | def count_tours(dim: Int, path: Path) : Int = {
 | 
| 222 |     57 |   if (path.size == (dim ^ 2)){
 | 
|  |     58 |     List(path).size
 | 
|  |     59 |   }  else {
 | 
|  |     60 |     val totalTours = legal_moves(dim, path, path.head) 
 | 
|  |     61 |     totalTours.map(element => count_tours(dim, element :: path)).sum
 | 
|  |     62 |   } 
 | 
| 221 |     63 | }
 | 
|  |     64 | 
 | 
| 222 |     65 | def enum_tours(dim: Int, path: Path) : List[Path] = {
 | 
|  |     66 |   if (path.size == (dim ^ 2)){
 | 
|  |     67 |     List(path)
 | 
|  |     68 |   } else {
 | 
|  |     69 |     val totalEnums = legal_moves(dim, path, path.head)
 | 
|  |     70 |     totalEnums.map(element => enum_tours(dim, element :: path)).flatten
 | 
|  |     71 |   }
 | 
| 221 |     72 | }
 | 
|  |     73 | 
 | 
| 222 |     74 | 
 | 
| 221 |     75 | //(5) Implement a first-function that finds the first 
 | 
|  |     76 | //    element, say x, in the list xs where f is not None. 
 | 
|  |     77 | //    In that case Return f(x), otherwise None. If possible,
 | 
|  |     78 | //    calculate f(x) only once.
 | 
|  |     79 | 
 | 
|  |     80 | def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = {
 | 
| 222 |     81 |   if (xs eq Nil) {
 | 
|  |     82 |     None
 | 
|  |     83 |   } else {
 | 
|  |     84 |     if (f(xs.head) != None) {
 | 
|  |     85 |       f(xs.head)
 | 
|  |     86 |     } else {
 | 
|  |     87 |       first(xs.tail, f)
 | 
| 221 |     88 |     }
 | 
| 222 |     89 |   }
 | 
|  |     90 | 
 | 
| 221 |     91 | }
 | 
|  |     92 | 
 | 
| 222 |     93 | 
 | 
| 221 |     94 | // test cases
 | 
|  |     95 | //def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None
 | 
|  |     96 | //
 | 
|  |     97 | //first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)   // Some(List((4,0)))
 | 
|  |     98 | //first(List((1, 0),(2, 0),(3, 0)), foo)          // None
 | 
|  |     99 | 
 | 
|  |    100 | 
 | 
|  |    101 | 
 | 
|  |    102 | 
 | 
|  |    103 | //(6) Implement a function that uses the first-function from (5) for
 | 
|  |    104 | //    trying out onward moves, and searches recursively for a
 | 
|  |    105 | //    knight tour on a dim * dim-board.
 | 
|  |    106 | 
 | 
|  |    107 | 
 | 
| 222 |    108 | //def first_tour(dim: Int, path: Path) : Option[Path] = ...
 | 
| 221 |    109 |  
 | 
| 222 |    110 | 
 | 
|  |    111 | 
 | 
|  |    112 | 
 | 
|  |    113 | 
 | 
|  |    114 | 
 | 
| 221 |    115 | /* Helper functions
 | 
|  |    116 | 
 | 
|  |    117 | 
 | 
|  |    118 | // for measuring time
 | 
| 220 |    119 | def time_needed[T](code: => T) : T = {
 | 
|  |    120 |   val start = System.nanoTime()
 | 
|  |    121 |   val result = code
 | 
|  |    122 |   val end = System.nanoTime()
 | 
|  |    123 |   println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
 | 
|  |    124 |   result
 | 
|  |    125 | }
 | 
|  |    126 | 
 | 
| 221 |    127 | // can be called for example with
 | 
|  |    128 | //     time_needed(count_tours(dim, List((0, 0))))
 | 
|  |    129 | // in order to print out the time that is needed for 
 | 
|  |    130 | // running count_tours
 | 
|  |    131 | 
 | 
| 220 |    132 | // for printing a board
 | 
|  |    133 | def print_board(dim: Int, path: Path): Unit = {
 | 
|  |    134 |   println
 | 
|  |    135 |   for (i <- 0 until dim) {
 | 
|  |    136 |     for (j <- 0 until dim) {
 | 
|  |    137 |       print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
 | 
|  |    138 |     }
 | 
|  |    139 |     println
 | 
|  |    140 |   } 
 | 
|  |    141 | }
 | 
|  |    142 | 
 | 
|  |    143 | 
 | 
|  |    144 | */
 |