testing2/knight3.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 29 Nov 2017 21:22:29 +0000
changeset 160 863feeb5c760
parent 145 d306102fd33b
child 161 6ea450e999e2
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
160
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     1
import scala.annotation.tailrec
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
object CW7c {
160
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     3
type Pos = (Int, Int)    // a position on a chessboard 
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     4
type Path = List[Pos]    // a path...a list of positions
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
160
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     6
def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = {
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     7
    if((x._1 >= 0) && (x._2 >= 0) && (x._1 < dim) && (x._2 < dim)){
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     8
       !(path.contains(x))
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     9
    } else false
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    10
 }
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    11
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    12
def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    13
  val lst = List( (1,2),(2,1),(2,-1),(1,-2), (-1,-2),(-2,-1),(-2,1),(-1,2) )
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    14
  val mapping = lst.map(s => ( s._1 + x._1, s._2 + x._2) )   
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    15
       for( i <- mapping if ( is_legal(dim,path)(i) )) yield i     
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    16
  }
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    17
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    18
def ordered_moves(dim: Int, path: Path, x: Pos) : List[Pos] = {
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    19
legal_moves(dim,path,x).sortBy(legal_moves(dim,path,_).length  )
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
160
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    22
def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] ={
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    23
  if(xs.isEmpty)
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    24
  None 
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    25
  else {
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    26
            val b = f(xs.head)
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    27
            if (b!=None)
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    28
            b
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    29
          else
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    30
             first(xs.tail,f)
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    31
       }
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    32
  }
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
160
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    34
def first_closed_tour_heuristic(dim: Int, path: Path) : Option[Path] = {
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    35
  if (dim < 5) None 
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    36
    else 
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    37
        if(path.length==dim*dim) Some(path)
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    38
       else 
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    39
      first(ordered_moves(dim,path,path.head),y => first_closed_tour_heuristic(dim, y::path))
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  }
160
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    41
  
145
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
d306102fd33b updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
160
863feeb5c760 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    45
first_closed_tour_heuristic(6, List((3, 3)))