testing2/knight3.scala
changeset 161 6ea450e999e2
parent 160 863feeb5c760
--- a/testing2/knight3.scala	Wed Nov 29 21:22:29 2017 +0000
+++ b/testing2/knight3.scala	Sun Dec 03 21:11:49 2017 +0000
@@ -1,45 +1,96 @@
-import scala.annotation.tailrec
+// Part 3 about finding a single tour using the Warnsdorf Rule
+//=============================================================
+
 object CW7c {
-type Pos = (Int, Int)    // a position on a chessboard 
-type Path = List[Pos]    // a path...a list of positions
+
+type Pos = (Int, Int)
+type Path = List[Pos]
 
-def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = {
-    if((x._1 >= 0) && (x._2 >= 0) && (x._1 < dim) && (x._2 < dim)){
-       !(path.contains(x))
-    } else false
- }
-
-def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
-  val lst = List( (1,2),(2,1),(2,-1),(1,-2), (-1,-2),(-2,-1),(-2,1),(-1,2) )
-  val mapping = lst.map(s => ( s._1 + x._1, s._2 + x._2) )   
-       for( i <- mapping if ( is_legal(dim,path)(i) )) yield i     
-  }
-
-def ordered_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
-legal_moves(dim,path,x).sortBy(legal_moves(dim,path,_).length  )
+def print_board(dim: Int, path: Path): Unit = {
+  println
+  for (i <- 0 until dim) {
+    for (j <- 0 until dim) {
+      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
+    }
+    println
+  } 
 }
 
-def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] ={
-  if(xs.isEmpty)
-  None 
-  else {
-            val b = f(xs.head)
-            if (b!=None)
-            b
-          else
-             first(xs.tail,f)
-       }
+def add_pair(x: Pos)(y: Pos): Pos = 
+  (x._1 + y._1, x._2 + y._2)
+
+def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
+  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
+
+def moves(x: Pos): List[Pos] = 
+  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
+       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
+
+def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
+  moves(x).filter(is_legal(dim, path))
+
+def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
+  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
+
+
+import scala.annotation.tailrec
+
+@tailrec
+def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
+  case Nil => None
+  case x::xs => {
+    val result = f(x)
+    if (result.isDefined) result else first(xs, f)
   }
-
-def first_closed_tour_heuristic(dim: Int, path: Path) : Option[Path] = {
-  if (dim < 5) None 
-    else 
-        if(path.length==dim*dim) Some(path)
-       else 
-      first(ordered_moves(dim,path,path.head),y => first_closed_tour_heuristic(dim, y::path))
-  }
-  
 }
 
 
-first_closed_tour_heuristic(6, List((3, 3)))
+def first_closed_tour_heuristic(dim: Int, path: Path): Option[Path] = {
+  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
+  else
+    first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristic(dim, x::path))
+}
+
+/*
+for (dim <- 1 to 6) {
+  val t = first_closed_tour_heuristic(dim, List((dim / 2, dim / 2)))
+  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
+}*/
+
+
+def first_tour_heuristic(dim: Int, path: Path): Option[Path] = {
+
+  @tailrec
+  def aux(dim: Int, path: Path, moves: List[Pos]): Option[Path] =  
+  if (path.length == dim * dim) Some(path)
+  else
+    moves match {
+      case Nil => None
+      case x::xs => {
+        val r = first_tour_heuristic(dim, x::path)
+        if (r.isDefined) r else aux(dim, path, xs)
+    }
+  }    
+ 
+  aux(dim, path, ordered_moves(dim, path, path.head)) 
+}
+
+/*
+def first_tour_heuristic(dim: Int, path: Path): Option[Path] = {
+  if (path.length == dim * dim) Some(path)
+  else
+    first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristic(dim, x::path))
+}
+*/
+
+/*
+for (dim <- 1 to 50) {
+  val t = first_tour_heuristic(dim, List((dim / 2, dim / 2)))
+  println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
+}
+*/
+
+}
+
+
+//CW7c.first_tour_heuristic(50, List((0,0))).get