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