templates3-bak/knight1.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 01 Dec 2018 15:09:37 +0000
changeset 228 e1e3e259e9b7
parent 212 c86e40fb3b21
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
146
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about finding and counting Knight's tours
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================================
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW7a {
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)    // a position on a chessboard 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]    // a path...a list of positions
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//(1a) Complete the function that tests whether the position 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
//     is inside the board and not yet element in the path.
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
212
c86e40fb3b21 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    12
//def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ...
146
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
//(1b) Complete the function that calculates for a position 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
//     all legal onward moves that are not already in the path. 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
//     The moves should be ordered in a "clockwise" manner.
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
//def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ...
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
//some test cases
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
//
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
//assert(legal_moves(8, Nil, (2,2)) == 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//(1c) Complete the two recursive functions below. 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
//     They exhaustively search for knight's tours starting from the 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
//     given path. The first function counts all possible tours, 
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//     and the second collects all tours in a list of paths.
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
//def count_tours(dim: Int, path: Path) : Int = ...
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//def enum_tours(dim: Int, path: Path) : List[Path] = ...
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
ed68f7239293 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}