testing2/knight2.scala
changeset 152 114a89518aea
parent 145 d306102fd33b
equal deleted inserted replaced
151:c5ca7f8e21a5 152:114a89518aea
     4 object CW7b {
     4 object CW7b {
     5 
     5 
     6 type Pos = (Int, Int)    // a position on a chessboard 
     6 type Pos = (Int, Int)    // a position on a chessboard 
     7 type Path = List[Pos]    // a path...a list of positions
     7 type Path = List[Pos]    // a path...a list of positions
     8 
     8 
     9 def print_board(dim: Int, path: Path): Unit = {
     9 def print_board(dim: Int, path: Path) : Unit = {
    10   println
    10   println
    11   for (i <- 0 until dim) {
    11   for (i <- 0 until dim) {
    12     for (j <- 0 until dim) {
    12     for (j <- 0 until dim) {
    13       print(f"${path.reverse.indexOf((i, j))}%3.0f ")
    13       print(f"${path.reverse.indexOf((i, j))}%3.0f ")
    14     }
    14     }
    15     println
    15     println
    16   } 
    16   } 
    17 }
    17 }
    18 
    18 
    19 def add_pair(x: Pos)(y: Pos): Pos = 
    19 def add_pair(x: Pos)(y: Pos) : Pos = 
    20   (x._1 + y._1, x._2 + y._2)
    20   (x._1 + y._1, x._2 + y._2)
    21 
    21 
    22 def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
    22 def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = 
    23   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
    23   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
    24 
    24 
    25 def moves(x: Pos): List[Pos] = 
    25 def moves(x: Pos) : List[Pos] = 
    26   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
    26   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
    27        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    27        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
    28 
    28 
    29 def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
    29 def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = 
    30   moves(x).filter(is_legal(dim, path))
    30   moves(x).filter(is_legal(dim, path))
    31 
    31 
    32 
    32 
    33 def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
    33 def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] = xs match {
    34   case Nil => None
    34   case Nil => None
    35   case x::xs => {
    35   case x::xs => {
    36     val result = f(x)
    36     val result = f(x)
    37     if (result.isDefined) result else first(xs, f)
    37     if (result.isDefined) result else first(xs, f)
    38   }
    38   }
    39 }
    39 }
    40 
    40 
    41 //first(List((1, 0),(2, 0),(3, 0),(4, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
    41 //first(List((1, 0),(2, 0),(3, 0),(4, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
    42 //first(List((1, 0),(2, 0),(3, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
    42 //first(List((1, 0),(2, 0),(3, 0)), (x => if (x._1 > 3) Some(List(x)) else None))
    43 
    43 
    44 def first_tour(dim: Int, path: Path): Option[Path] = {
    44 def first_tour(dim: Int, path: Path) : Option[Path] = {
    45   if (path.length == dim * dim) Some(path)
    45   if (path.length == dim * dim) Some(path)
    46   else
    46   else
    47     first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
    47     first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
    48 }
    48 }
    49 
    49