testing3/testing3-bak/knight1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 05 Aug 2019 20:14:06 +0100
changeset 269 86a85865e772
parent 245 975d34506e88
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
     1
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// Part 1 about finding and counting Knight's tours
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
//==================================================
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
     5
object CW7a extends App{
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Pos = (Int, Int)    // a position on a chessboard 
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
type Path = List[Pos]    // a path...a list of positions
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    10
//(1a) Complete the function that tests whether the position 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    11
//     is inside the board and not yet element in the path.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    12
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    13
//def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = ...
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    15
def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    16
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    17
// if ((x._1<dim && x._2<dim) && (x._1>0 || x._2>0)) false else !path.contains(x)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    18
 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    19
  if (x._1 < 0 || x._2 < 0) false 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    20
  else if (x._1 < dim && x._2 < dim && !path.contains(x)) true 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    21
  else false
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    22
 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    23
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    24
}
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    28
def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    29
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    30
  val allPossibleMoves = List((x._1+1, x._2+2), (x._1+2, x._2+1), (x._1+2, x._2-1), (x._1+1, x._2-2), (x._1-1, x._2-2), (x._1-2, x._2-1), (x._1-2, x._2+1), (x._1-1, x._2+2));
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    31
 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    32
  //val finalList = allPossibleMoves.filter((a=>a._1<dim && a._2<dim && x._1 >= 0 && a._2 >= 0));
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    33
  
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    34
  val finalList = for(pos<-allPossibleMoves if(is_legal(dim,path)(pos))) yield pos;
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    35
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    36
  // println("Space in board: " + dim*dim + " for dim: " + dim)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    37
   
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    38
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    39
  finalList.toList;
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    40
    
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    41
  
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    42
}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    43
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    44
println(legal_moves(8, Nil, (2,2)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    45
println(legal_moves(8, Nil, (7,7)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    46
println(legal_moves(8, List((4,1), (1,0)), (2,2)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    47
println(legal_moves(8, List((6,6)), (7,7)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    48
println(legal_moves(1, Nil, (0,0)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    49
println(legal_moves(2, Nil, (0,0)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    50
println(legal_moves(3, Nil, (0,0)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    51
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    52
println("=================================================================================")
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    53
println("================================Comparision output===============================")
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    54
println("=================================================================================")
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    55
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    56
println(legal_moves(8, Nil, (2,2)) == List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    57
println(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    58
println(legal_moves(8, List((4,1), (1,0)), (2,2)) == List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    59
println(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    60
println(legal_moves(1, Nil, (0,0)) == Nil)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    61
println(legal_moves(2, Nil, (0,0)) == Nil)
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    62
println(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    63
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    65
def count_tours(dim: Int, path: Path) : Int = {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    66
     
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    67
  val allMovesFromCurrentPosition = legal_moves(dim, path, path.head);
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    68
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    69
  if (path.length == dim*dim) 1 else  {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    70
    
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    71
    if (allMovesFromCurrentPosition.size == 0 ) 0  else {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    72
      
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    73
      allMovesFromCurrentPosition.map( element => count_tours(dim, element::path)).sum
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    74
      
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    75
      
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    76
    }
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    77
    
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    78
  }
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    79
  
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
}
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    81
    
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    82
  
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    84
println ( count_tours(5, List((0,0))) )
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    85
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    86
def enum_tours(dim: Int, path: Path) : List[Path] = {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    87
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    88
     val allMovesFromCurrentPosition = legal_moves(dim, path, path.head);
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    89
  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    90
  if (path.length == dim*dim) List(path) else  {
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    91
    
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    92
  allMovesFromCurrentPosition.map( element => enum_tours(dim, element::path)).flatten ;
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    93
      
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    94
      
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    95
      }
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    96
    }
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    97
  println ( enum_tours(6, List((0,2))).size)
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   100
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   101
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   102
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   104
 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   105
 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   106
//(1b) Complete the function that calculates for a position 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   107
//     all legal onward moves that are not already in the path. 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   108
//     The moves should be ordered in a "clockwise" manner.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   109
 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   110
//def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   111
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   112
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   113
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   115
//some test cases
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   116
//
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   117
//assert(legal_moves(8, Nil, (2,2)) == 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   118
//  List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   119
//assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   120
//assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   121
//  List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   122
//assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   123
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   125
//(1c) Complete the two recursive functions below. 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   126
//     They exhaustively search for knight's tours starting from the 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   127
//     given path. The first function counts all possible tours, 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   128
//     and the second collects all tours in a list of paths.
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   129
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   130
//def count_tours(dim: Int, path: Path) : Int = ...
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   131
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   132
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   133
//def enum_tours(dim: Int, path: Path) : List[Path] = ...