assignment2021scala/main4/knight1.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 08 Nov 2022 18:35:41 +0000
changeset 434 8c5804b2f9d2
parent 415 fced9a61c881
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
415
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Main Part 4 about finding Knight's tours
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//==========================================
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
object M4a {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
// If you need any auxiliary functions, feel free to 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// implement them, but do not make any changes to the
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
// templates below. Also have a look whether the functions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// at the end of the file are of any help.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
type Pos = (Int, Int)    // a position on a chessboard 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
type Path = List[Pos]    // a path...a list of positions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
//(1) Complete the function that tests whether the position x
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
//    is inside the board and not yet element in the path.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
//(2) Complete the function that calculates for a position x
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
//    all legal onward moves that are not already in the path. 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
//    The moves should be ordered in a "clockwise" manner.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
//some testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
//assert(legal_moves(8, Nil, (2,2)) == 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
//(3) Complete the two recursive functions below. 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
//    They exhaustively search for knight's tours starting from the 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
//    given path. The first function counts all possible tours, 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
//    and the second collects all tours in a list of paths.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
def count_tours(dim: Int, path: Path) : Int = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
def enum_tours(dim: Int, path: Path) : List[Path] = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
//(4) Implement a first-function that finds the first 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
//    element, say x, in the list xs where f is not None. 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
//    In that case Return f(x), otherwise None. If possible,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
//    calculate f(x) only once.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
// testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
//def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
//first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)   // Some(List((4,0)))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
//first(List((1, 0),(2, 0),(3, 0)), foo)          // None
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
//(5) Implement a function that uses the first-function from (4) for
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
//    trying out onward moves, and searches recursively for a
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
//    knight tour on a dim * dim-board.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
def first_tour(dim: Int, path: Path) : Option[Path] = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
/* Helper functions
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
// for measuring time
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
def time_needed[T](code: => T) : T = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  val start = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  val result = code
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  val end = System.nanoTime()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  result
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
// can be called for example with
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
//     time_needed(count_tours(dim, List((0, 0))))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
//
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
// in order to print out the time that is needed for 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
// running count_tours
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
// for printing a board
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
def print_board(dim: Int, path: Path): Unit = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  println()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  for (i <- 0 until dim) {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
    for (j <- 0 until dim) {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
    println()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  } 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
*/
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
}