Test and fix promotion by capture

This commit is contained in:
Thibault Duplessis 2012-04-01 17:42:41 +02:00
parent 1007413bda
commit 528d04b732
5 changed files with 64 additions and 25 deletions

View file

@ -24,10 +24,12 @@ case class Actor(piece: Piece, pos: Pos, board: Board) {
case Pawn pawnDir(pos) map { next
val fwd = Some(next) filterNot board.occupations
def capture(horizontal: Direction): Option[Move] = for {
p horizontal(next); if enemies(p);
b board.taking(pos, p)
} yield move(p, b, Some(p))
def capture(horizontal: Direction): Option[Move] = {
for {
p horizontal(next); if enemies(p);
b board.taking(pos, p)
} yield move(p, b, Some(p))
} flatMap maybePromote
def enpassant(horizontal: Direction): Option[Move] = for {
victimPos horizontal(pos); if pos.y == color.passablePawnY
victim board(victimPos); if victim == !color - Pawn
@ -37,10 +39,14 @@ case class Actor(piece: Piece, pos: Pos, board: Board) {
b board.taking(pos, targetPos, Some(victimPos))
} yield move(targetPos, b, Some(victimPos), enpassant = true)
def forward(p: Pos): Option[Move] =
if (pos.y == color.promotablePawnY)
board.promote(pos, p) map { b move(p, b, promotion = Some(Queen)) }
else
board.move(pos, p) map { b move(p, b) }
board.move(pos, p) map { move(p, _) } flatMap maybePromote
def maybePromote(m: Move): Option[Move] =
if (m.dest.y == m.color.promotablePawnY)
(m.after promote m.dest) map { b2
m.copy(after = b2, promotion = Some(Queen))
}
else Some(m)
List(
for {
p fwd

View file

@ -81,11 +81,11 @@ case class Board(pieces: Map[Pos, Piece], history: History) {
lazy val occupations = pieces.keySet
def promote(orig: Pos, dest: Pos): Option[Board] = for {
pawn apply(orig)
b1 move(orig, dest)
b2 b1.take(dest)
b3 b2.place(pawn.color.queen, dest)
def promote(pos: Pos): Option[Board] = for {
pawn apply(pos)
if (pawn is Pawn)
b2 take(pos)
b3 b2.place(pawn.color.queen, pos)
} yield b3
def withHistory(h: History): Board = copy(history = h)

View file

@ -29,7 +29,7 @@ case object White extends Color {
val unmovedPawnY = 2
val passablePawnY = 5
val promotablePawnY = 7
val promotablePawnY = 8
val letter = 'w'
val name = "white"
@ -41,7 +41,7 @@ case object Black extends Color {
val unmovedPawnY = 7
val passablePawnY = 4
val promotablePawnY = 2
val promotablePawnY = 1
val letter = 'b'
val name = "black"

View file

@ -43,7 +43,7 @@ class BoardTest extends ChessTest {
}
"allow a pawn to be promoted to a queen" in {
Board.empty.place(Black.pawn, A7) flatMap (_.promote(A7, A8)) must beSome.like {
Board.empty.place(Black.pawn, A8) flatMap (_ promote A8) must beSome.like {
case b b(A8) must beSome(Black.queen)
}
}

View file

@ -8,20 +8,53 @@ class PromotionTest extends ChessTest {
p
K """
val game = Game(board, Black)
"promote to a queen" in {
game.playMove(C2, C1, Queen) must beGame("""
"promote to a queen" in {
game.playMove(C2, C1, Queen) must beGame("""
K q """)
}
"promote to a queen by default" in {
game.playMove(C2, C1) must beGame("""
}
"promote to a queen by default" in {
game.playMove(C2, C1) must beGame("""
K q """)
}
"promote to a knight" in {
game.playMove(C2, C1, Knight) must beGame("""
}
"promote to a knight" in {
game.playMove(C2, C1, Knight) must beGame("""
K n """)
}
}
"promote to a queen by killing" in {
Game("""
p
K R""", Black).playMove(C2, D1) must beGame("""
K q""")
}
"promote to a knight by killing" in {
Game("""
p
K R""", Black).playMove(C2, D1, Knight) must beGame("""
K n""")
}
"promote to a whiteknight" in {
Game("""
P
K n """).playMove(A7, A8, Knight) must beGame("""
N
K n """)
}
}
}