424
|
1 |
import M4a._
|
386
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
//type Pos = (Int, Int) // a position on a chessboard
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
//type Path = List[Pos] // a path...a list of positions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
def add_pair_urban(x: Pos)(y: Pos): Pos =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
(x._1 + y._1, x._2 + y._2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
def moves_urban(x: Pos): List[Pos] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
List(( 1, 2),( 2, 1),( 2, -1),( 1, -2),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
(-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair_urban(x))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
moves_urban(x).filter(is_legal_urban(dim, path))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
def correct_urban(dim: Int)(p: Path): Boolean = p match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
case Nil => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case x::Nil => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case x::y::p => if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
463
|
25 |
import scala.concurrent._
|
|
26 |
import scala.concurrent.duration._
|
|
27 |
import ExecutionContext.Implicits.global
|
|
28 |
import scala.language.postfixOps
|
|
29 |
|
|
30 |
lazy val f = Future {
|
386
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
val ts00_urban = enum_tours(5, List((0, 0)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
assert(ts00_urban.map(correct_urban(5)).forall(_ == true) == true)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
assert(ts00_urban.length == 304)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
val ts_urban = enum_tours(5, List((0, 2)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
assert(ts_urban.map(correct_urban(5)).forall(_ == true) == true)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
assert(ts_urban.length == 56)
|
463
|
40 |
}
|
|
41 |
|
|
42 |
Await.result(f, 32 second)
|
|
43 |
|