main_templates4/knight1.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 23 Sep 2023 23:49:44 +0100
changeset 470 86a456f8cb92
parent 428 cdfa6a293453
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
397
085fefce672e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     1
// Main Part 4 about finding Knight's tours
085fefce672e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     2
//==========================================
296
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
     3
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
     4
397
085fefce672e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     5
object M4a {
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 397
diff changeset
     7
// If you need any auxiliary functions, feel free to 
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 397
diff changeset
     8
// implement them, but do not make any changes to the
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// templates below. Also have a look whether the functions
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 397
diff changeset
    10
// at the end of the file are of any help.
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
type Pos = (Int, Int)    // a position on a chessboard 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
type Path = List[Pos]    // a path...a list of positions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    15
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    16
//======================
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    18
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    19
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    20
//(1) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    21
def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ???
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    24
//(2) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    25
def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ???
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
296
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
    28
//some testcases
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//assert(legal_moves(8, Nil, (2,2)) == 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    38
// (3) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    39
def count_tours(dim: Int, path: Path) : Int = ???
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    41
def enum_tours(dim: Int, path: Path) : List[Path] = ???
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    43
// (4) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    44
def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ???
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
296
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
    47
// testcases
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
    48
//
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
//def foo(x: (Int, Int)) = if (x._1 > 3) Some(List(x)) else None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)   // Some(List((4,0)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
//first(List((1, 0),(2, 0),(3, 0)), foo)          // None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    55
//(5) 
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    56
def first_tour(dim: Int, path: Path) : Option[Path] = ???
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
/* Helper functions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
// for measuring time
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def time_needed[T](code: => T) : T = {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  val start = System.nanoTime()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  val result = code
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  val end = System.nanoTime()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  result
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// can be called for example with
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 397
diff changeset
    73
//
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
//     time_needed(count_tours(dim, List((0, 0))))
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 397
diff changeset
    75
//
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
// in order to print out the time that is needed for 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// running count_tours
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
296
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
    79
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
// for printing a board
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
def print_board(dim: Int, path: Path): Unit = {
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    82
  println()
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  for (i <- 0 until dim) {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
    for (j <- 0 until dim) {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
      print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
    }
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    87
    println()
214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  } 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
*/
296
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
    93
12dc251fc47e updated
Christian Urban <urbanc@in.tum.de>
parents: 214
diff changeset
    94
}