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