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