main_templates4/knight4.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 03 Nov 2022 11:30:09 +0000
changeset 432 de701b64a4e0
parent 430 274c865b3878
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
430
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Part 4 about finding a single tour on "mutilated" chessboards
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//==============================================================
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
432
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
     4
object M4d { 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
     5
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
     6
// !!! Copy any function you need from file knight1.scala !!!
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
     7
// !!! or knight2.scala or knight3.scala                  !!! 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
     8
//
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
     9
// If you need any auxiliary function, feel free to 
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    10
// implement it, but do not make any changes to the
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    11
// templates below.
430
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
type Pos = (Int, Int)
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
type Path = List[Pos]
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
def print_board(dim: Int, path: Path): Unit = {
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  println()
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
  for (i <- 0 until dim) {
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
    for (j <- 0 until dim) {
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
      print(f"${path.reverse.indexOf((i, j))}%4.0f ")
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
    }
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
    println()
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  } 
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
}
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
432
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    26
// ADD YOUR CODE BELOW
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    27
//======================
430
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
432
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    29
// (10)
de701b64a4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 430
diff changeset
    30
def one_tour_pred(dim: Int, path: Path, n: Int, pred: Pos => Boolean): Option[Path] = ???
430
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
274c865b3878 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
}