main_templates4/knight1.scala
changeset 428 cdfa6a293453
parent 400 e48ea8300b2d
equal deleted inserted replaced
427:6e93040e3378 428:cdfa6a293453
     7 // If you need any auxiliary functions, feel free to 
     7 // If you need any auxiliary functions, feel free to 
     8 // implement them, but do not make any changes to the
     8 // implement them, but do not make any changes to the
     9 // templates below. Also have a look whether the functions
     9 // templates below. Also have a look whether the functions
    10 // at the end of the file are of any help.
    10 // at the end of the file are of any help.
    11 
    11 
    12 
       
    13 
       
    14 type Pos = (Int, Int)    // a position on a chessboard 
    12 type Pos = (Int, Int)    // a position on a chessboard 
    15 type Path = List[Pos]    // a path...a list of positions
    13 type Path = List[Pos]    // a path...a list of positions
    16 
    14 
    17 //(1) Complete the function that tests whether the position x
    15 // ADD YOUR CODE BELOW
    18 //    is inside the board and not yet element in the path.
    16 //======================
    19 
    17 
       
    18 
       
    19 
       
    20 //(1) 
    20 def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ???
    21 def is_legal(dim: Int, path: Path, x: Pos) : Boolean = ???
    21 
    22 
    22 
    23 
    23 
    24 //(2) 
    24 //(2) Complete the function that calculates for a position x
       
    25 //    all legal onward moves that are not already in the path. 
       
    26 //    The moves should be ordered in a "clockwise" manner.
       
    27  
       
    28 def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ???
    25 def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ???
    29 
    26 
    30 
    27 
    31 //some testcases
    28 //some testcases
    32 //
    29 //
    36 //assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
    33 //assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
    37 //  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
    34 //  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
    38 //assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
    35 //assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
    39 
    36 
    40 
    37 
    41 //(3) Complete the two recursive functions below. 
    38 // (3) 
    42 //    They exhaustively search for knight's tours starting from the 
       
    43 //    given path. The first function counts all possible tours, 
       
    44 //    and the second collects all tours in a list of paths.
       
    45 
       
    46 def count_tours(dim: Int, path: Path) : Int = ???
    39 def count_tours(dim: Int, path: Path) : Int = ???
    47 
    40 
    48 def enum_tours(dim: Int, path: Path) : List[Path] = ???
    41 def enum_tours(dim: Int, path: Path) : List[Path] = ???
    49 
    42 
    50 
    43 // (4) 
    51 //(4) Implement a first-function that finds the first 
       
    52 //    element, say x, in the list xs where f is not None. 
       
    53 //    In that case Return f(x), otherwise None. If possible,
       
    54 //    calculate f(x) only once.
       
    55 
       
    56 def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ???
    44 def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = ???
    57 
    45 
    58 
    46 
    59 // testcases
    47 // testcases
    60 //
    48 //
    62 //
    50 //
    63 //first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)   // Some(List((4,0)))
    51 //first(List((1, 0),(2, 0),(3, 0),(4, 0)), foo)   // Some(List((4,0)))
    64 //first(List((1, 0),(2, 0),(3, 0)), foo)          // None
    52 //first(List((1, 0),(2, 0),(3, 0)), foo)          // None
    65 
    53 
    66 
    54 
    67 //(5) Implement a function that uses the first-function from (4) for
    55 //(5) 
    68 //    trying out onward moves, and searches recursively for a
       
    69 //    knight tour on a dim * dim-board.
       
    70 
       
    71 def first_tour(dim: Int, path: Path) : Option[Path] = ???
    56 def first_tour(dim: Int, path: Path) : Option[Path] = ???
    72  
    57  
    73 
    58 
    74 
    59 
    75 /* Helper functions
    60 /* Helper functions