progs/knight1.scala
changeset 212 4bda49ec24da
parent 53 9f8751912560
child 213 f968188d4a9b
equal deleted inserted replaced
211:092e0879a5ae 212:4bda49ec24da
     2 //==================================================
     2 //==================================================
     3 
     3 
     4 type Pos = (Int, Int)    // a position on a chessboard 
     4 type Pos = (Int, Int)    // a position on a chessboard 
     5 type Path = List[Pos]    // a path...a list of positions
     5 type Path = List[Pos]    // a path...a list of positions
     6 
     6 
     7 //(1a) Complete the function that tests whether the position 
     7 def print_board(dim: Int, path: Path): Unit = {
     8 // is inside the board and not yet element in the path.
     8   println
     9 
     9   for (i <- 0 until dim) {
    10 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = ...
    10     for (j <- 0 until dim) {
       
    11       print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
       
    12     }
       
    13     println
       
    14   } 
       
    15 }
    11 
    16 
    12 
    17 
    13 //(1b) Complete the function that calculates for a position 
    18 // 1 mark
    14 // all legal onward moves that are not already in the path. 
       
    15 // The moves should be ordered in a "clockwise" order.
       
    16  
       
    17 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = ...
       
    18 
    19 
    19 //assert(legal_moves(8, Nil, (2,2)) == 
    20 def is_legal(dim: Int, path: Path, x: Pos): Boolean = 
    20 //  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
    21   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
    21 //assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
    22 
    22 //assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
    23 assert(is_legal(8, Nil)((3,4)) == true)
    23 //  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
    24 assert(is_legal(8, List((4,1), (1,0)))((4,1)) == false)
    24 //assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
    25 assert(is_legal(2, Nil)((0,0)) == true)
    25 
    26 
    26 
    27 
    27 //(1c) Complete the two recursive functions below. 
    28 def add_pair(x: Pos)(y: Pos): Pos = 
    28 // They exhaustively search for open tours starting from the 
    29   (x._1 + y._1, x._2 + y._2)
    29 // given path. The first function counts all possible open tours, 
       
    30 // and the second collects all open tours in a list of paths.
       
    31 
    30 
    32 def count_tours(dim: Int, path: Path): Int = ...
    31 def moves(x: Pos): List[Pos] = 
       
    32   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
       
    33        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    33 
    34 
    34 def enum_tours(dim: Int, path: Path): List[Path] = ...
    35 // 1 mark
       
    36 
       
    37 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
       
    38   moves(x).filter(is_legal(dim, path))
       
    39 
       
    40 assert(legal_moves(8, Nil, (2,2)) == 
       
    41   List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
       
    42 assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
       
    43 assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
       
    44   List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
       
    45 assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
       
    46 assert(legal_moves(1, Nil, (0,0)) == List())
       
    47 assert(legal_moves(2, Nil, (0,0)) == List())
       
    48 assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
       
    49 
       
    50 // 2 marks
       
    51 
       
    52 def count_tours(dim: Int, path: Path): Int = {
       
    53   if (path.length == dim * dim) 1
       
    54   else 
       
    55     (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
       
    56 }
       
    57 
       
    58 def enum_tours(dim: Int, path: Path): List[Path] = {
       
    59   if (path.length == dim * dim) List(path)
       
    60   else 
       
    61     (for (x <- legal_moves(dim, path, path.head)) yield enum_tours(dim, x::path)).flatten
       
    62 }
       
    63 
       
    64 // as far as tasks go
    35 
    65 
    36 
    66 
       
    67 
       
    68 def count_all_tours(dim: Int) = {
       
    69   for (i <- (0 until dim).toList; 
       
    70        j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
       
    71 }
       
    72 
       
    73 def enum_all_tours(dim: Int): List[Path] = {
       
    74   (for (i <- (0 until dim).toList; 
       
    75         j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
       
    76 }
       
    77 
       
    78 
       
    79 println("Number of tours starting from (0, 0)")
       
    80 
       
    81 for (dim <- 1 to 5) {
       
    82   println(s"${dim} x ${dim} " + count_tours(dim, List((0, 0))))
       
    83 }
       
    84 
       
    85 for (dim <- 1 to 5) {
       
    86   println(s"${dim} x ${dim} " + count_all_tours(dim))
       
    87 }
       
    88 
       
    89 for (dim <- 1 to 5) {
       
    90   val ts = enum_tours(dim, List((0, 0)))
       
    91   println(s"${dim} x ${dim} ")   
       
    92   if (ts != Nil) {
       
    93     print_board(dim, ts.head)
       
    94     println(ts.head)
       
    95   }
       
    96 }
       
    97 
       
    98