476
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Main Part 4 about the Shogun Board Game
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//=========================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
object M4 {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
type Pos = (Int, Int) // a position on a chessboard
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
// Colours: Red or White
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
abstract class Colour
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
case object Red extends Colour
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
case object Wht extends Colour
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
// Pieces: Either Pawns or Kings
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
//===============================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
abstract class Piece {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
def pos : Pos
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
def col : Colour
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
def en : Int // energy for Pawns 1 - 4, for Kings 1 - 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case class Pawn(en: Int, col: Colour, pos: Pos) extends Piece
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case class King(en: Int, col: Colour, pos: Pos) extends Piece
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
//val p = Pawn(4, Wht, (3,2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
//assert(p.pos == (3,2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
//assert(p.col == Wht)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
//assert(p.en == 4)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
// checks if a piece is a king
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
def is_king(pc: Piece) : Boolean = pc match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
case King(_, _, _) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
case _ => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
// incrementing and decrementing the position of a piece
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
def incx(pc: Piece) : Piece = pc match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
case Pawn(en, c, (x,y)) => Pawn(en, c, (x+1,y))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
case King(en, c, (x,y)) => King(en, c, (x+1,y))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
def incy(pc: Piece) : Piece = pc match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
case Pawn(en, c, (x,y)) => Pawn(en, c, (x,y+1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
case King(en, c, (x,y)) => King(en, c, (x,y+1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
def decx(pc: Piece) : Piece = pc match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
case Pawn(en, c, (x,y)) => Pawn(en, c, (x-1,y))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
case King(en, c, (x,y)) => King(en, c, (x-1,y))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
def decy(pc: Piece) : Piece = pc match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
case Pawn(en, c, (x,y)) => Pawn(en, c, (x,y-1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
case King(en, c, (x,y)) => King(en, c, (x,y-1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
//pretty printing colours and pieces
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
def pp_color(c: Colour) : String = c match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
case Red => "R"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
case Wht => "W"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
def pp(pc: Piece) : String = pc match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
case Pawn(n, c, _) => s"P${pp_color(c)}$n"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
case King(n, c, _) => s"K${pp_color(c)}$n"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
// Boards are sets of pieces
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
//===========================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
case class Board(pces: Set[Piece]) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
def +(pc: Piece) : Board = Board(pces + pc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
def -(pc: Piece) : Board = Board(pces - pc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
// checking whether a position is occupied in a board
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
def occupied(p: Pos, b: Board) : Option[Piece] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
b.pces.find(p == _.pos)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
def occupied_by(p: Pos, b: Board) : Option[Colour] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
occupied(p, b).map(_.col)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
def is_occupied(p: Pos, b: Board) : Boolean =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
occupied(p, b).isDefined
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
// is a position inside a board
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
def inside(p: Pos, b: Board): Boolean =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
1 <= p._1 && 1 <= p._2 && p._1 <= 8 && p._2 <= 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
// pretty printing a board
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
def print_board(b: Board): Unit = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
println()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
for (i <- 8 to 1 by -1) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
println("+" ++ "-" * 31 ++ "+")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
for (j <- 1 to 8) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
val opc = occupied((j,i), b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
if (opc.isDefined) print(s"|${pp(opc.get)}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
else print("| ")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
println("|")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
println("+" ++ "-" * 31 ++ "+")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
// example board: initial board
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
val b_init = Board(Set(King(2,Wht,(4,1)), King(1,Red,(5,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
Pawn(4,Wht,(1,1)), Pawn(4,Red,(1,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
Pawn(3,Wht,(2,1)), Pawn(2,Red,(2,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
Pawn(2,Wht,(3,1)), Pawn(3,Red,(3,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
Pawn(1,Wht,(5,1)), Pawn(1,Red,(4,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
Pawn(4,Wht,(6,1)), Pawn(3,Red,(6,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
Pawn(3,Wht,(7,1)), Pawn(1,Red,(7,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
Pawn(2,Wht,(8,1)), Pawn(3,Red,(8,8))))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
//print_board(b_init)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
// |PR4|PR2|PR3|PR1|KR1|PR3|PR1|PR3|
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
// | | | | | | | | |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
// | | | | | | | | |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
// | | | | | | | | |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
// | | | | | | | | |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
// | | | | | | | | |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
// | | | | | | | | |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
// |PW4|PW3|PW2|KW2|PW1|PW4|PW3|PW2|
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
// --------------------------------
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
// Moves
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
//=======
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
abstract class Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
case object U extends Move // up
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
case object D extends Move // down
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
case object R extends Move // right
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
case object L extends Move // left
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
case object RU extends Move // ...
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
case object LU extends Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
case object RD extends Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
case object LD extends Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
case object UR extends Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
case object UL extends Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
case object DR extends Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
case object DL extends Move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
// Task 1: calculates all next possible positions according to a move
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
def eval(pc: Piece, m: Move, en: Int, b: Board) : Set[Piece] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
val p = pc.pos
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
val c = pc.col
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
if (!inside(p, b)) Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
else if (en == 0 && !is_occupied(p, b)) Set(pc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
else if (en == 0 && is_occupied(p, b) && c != occupied_by(p, b).get) Set(pc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
else if (is_occupied(p, b)) Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
else m match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
case U => eval(incy(pc), U, en - 1, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
case D => eval(decy(pc), D, en - 1, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
case R => eval(incx(pc), R, en - 1, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
case L => eval(decx(pc), L, en - 1, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
case RU => eval(incx(pc), RU, en - 1, b) ++ eval(pc, U, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
case LU => eval(decx(pc), LU, en - 1, b) ++ eval(pc, U, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
case RD => eval(incx(pc), RD, en - 1, b) ++ eval(pc, D, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
case LD => eval(decx(pc), LD, en - 1, b) ++ eval(pc, D, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
case UR => eval(incy(pc), UR, en - 1, b) ++ eval(pc, R, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
case UL => eval(incy(pc), UL, en - 1, b) ++ eval(pc, L, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
case DR => eval(decy(pc), DR, en - 1, b) ++ eval(pc, R, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
case DL => eval(decy(pc), DL, en - 1, b) ++ eval(pc, L, en, b)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
}}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
/*
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
// test cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
val pw_a = Pawn(4, Wht, (4,4))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
println(eval(pw_a, U, 4, b_init)) // Set(Pawn(4,Wht,(4,8)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
println(eval(pw_a, U, 3, b_init)) // Set(Pawn(4,Wht,(4,7)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
println(eval(pw_a, RU, 4, b_init)) // Set(Pawn(4,Wht,(6,6)), Pawn(4,Wht,(4,8)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
// Pawn(4,Wht,(5,7)), Pawn(4,Wht,(7,5)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
// Pawn(4,Wht,(8,4)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
val pw_b = Pawn(4, Red, (4,4))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
println(eval(pw_b, RU, 4, b_init)) // Set(Pawn(4,Red,(8,4)), Pawn(4,Red,(7,5)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
Pawn(4,Red,(6,6)), Pawn(4,Red,(5,7)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
*/
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
// Task 2: calculates all possible moves for a piece
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
def all_moves(pc: Piece, b: Board) : Set[Piece] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
Set(U,D,L,R,RU,LU,RD,LD,UR,UL,DR,DL).flatMap(eval(pc, _, pc.en, b - pc))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
/*
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
// test cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
val pw_c = Pawn(2, Wht, (4,4))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
val pw_d = Pawn(3, Red, (4,4))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
println(all_moves(pw_c, b_init))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
// Set(Pawn(2,Wht,(3,5)), Pawn(2,Wht,(2,4)), Pawn(2,Wht,(3,3)), Pawn(2,Wht,(5,5)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
// Pawn(2,Wht,(6,4)), Pawn(2,Wht,(4,6)), Pawn(2,Wht,(4,2)), Pawn(2,Wht,(5,3)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
198 |
println(all_moves(pw_d, b_init))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
// Set(Pawn(3,Red,(4,7)), Pawn(3,Red,(5,2)), Pawn(3,Red,(3,2)), Pawn(3,Red,(1,4)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
// Pawn(3,Red,(6,3)), Pawn(3,Red,(3,6)), Pawn(3,Red,(2,5)), Pawn(3,Red,(2,3)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
// Pawn(3,Red,(4,1)), Pawn(3,Red,(5,6)), Pawn(3,Red,(7,4)), Pawn(3,Red,(6,5)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
*/
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
205 |
// Task 3: calculates all pieces that are attacked by colour
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
def attacked(c: Colour, b: Board) : Set[Piece] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
val (me, opponent) = b.pces.partition(_.col == c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
val all = me.flatMap(all_moves(_, b))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
opponent.filter(pc => is_occupied(pc.pos, Board(all)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
211 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
212 |
// test cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
213 |
val b_checkmate = Board(Set(King(2, Red, (4,2)), King(2, Wht, (7,1)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
214 |
Pawn(3, Red, (6,1)), Pawn(2, Wht, (8,4)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
215 |
Pawn(4, Red, (4,4)), Pawn(2, Wht, (4,1)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
216 |
Pawn(4, Red, (5,3)), Pawn(3, Wht, (8,7)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
217 |
Pawn(3, Red, (6,5))))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
218 |
print_board(b_checkmate)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
println(attacked(Red, b_checkmate)) // Set(Pawn(2,Wht,(8,4)), King(2,Wht,(7,1)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
println(attacked(Wht, b_checkmate)) // Set(Pawn(3,Red,(6,1)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
println(attacked(Wht, b_init)) // Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
println(attacked(Red, b_init)) // Set()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
224 |
// Task 4: calculates the number of pieces that attack a piece
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
def attackedN(pc: Piece, b: Board) : Int = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
val (me, opponent) = b.pces.partition(_.col == pc.col)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
val all = opponent.toList.flatMap(all_moves(_, b))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
all.count(_.pos == pc.pos)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
// test cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
println(attackedN(Pawn(2, Wht, (8,4)), b_checkmate)) // 3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
println(attackedN(King(2, Wht, (7,1)), b_checkmate)) // 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
println(attackedN(Pawn(3, Red, (6,1)), b_checkmate)) // 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
237 |
// Task 5: calculates the number of pieces that protect a piece
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
238 |
def protectedN(pc: Piece, b: Board) : Int = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
239 |
val (me, opponent) = b.pces.partition(_.col == pc.col)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
240 |
val all = (me - pc).toList.flatMap(all_moves(_, (b - pc)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
241 |
all.count(_.pos == pc.pos)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
242 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
243 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
244 |
println(protectedN(Pawn(2, Wht, (8,4)), b_checkmate)) // 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
245 |
println(protectedN(Pawn(4, Red, (5,3)), b_checkmate)) // 3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
246 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
247 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
248 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
249 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
250 |
val pw1 = Pawn(4, Wht, (4,6))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
251 |
val pw2 = Pawn(4, Wht, (2,4))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
252 |
val pw3 = Pawn(3, Red, (6,8))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
253 |
val pw4 = Pawn(2, Red, (2,8))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
254 |
val bt = b_init + pw1 + pw2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
255 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
256 |
print_board(bt)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
257 |
println(s"Capture Red: ${attacked(Wht, bt)}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
258 |
// Set(Pawn(2,Red,(2,8)), Pawn(3,Red,(6,8)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
259 |
println(s"Capture Wht: ${attacked(Red, bt)}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
260 |
// Set(Pawn(4,Wht,(4,6)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
261 |
println(s"ProtectedN: ${protectedN(pw3, bt)}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
262 |
// 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
263 |
println(s"AttackedN: ${attackedN(pw4, bt)}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
264 |
// 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
265 |
println(s"all moves: ${all_moves(pw2, bt)}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
266 |
// Set(Pawn(4,Wht,(4,2)), Pawn(4,Wht,(1,7)), Pawn(4,Wht,(5,3)), Pawn(4,Wht,(5,5)),
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
267 |
// Pawn(4,Wht,(2,8)), Pawn(4,Wht,(3,7)), Pawn(4,Wht,(6,4)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
268 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
269 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
270 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
271 |
|