Implement check, checkmate and stalemate
parent
2efd31ec0a
commit
c3d4800e06
|
@ -19,11 +19,13 @@ case class Board(pieces: Map[Pos, Piece], history: History) {
|
|||
case (pos, piece) ⇒ (pos, Actor(piece, pos, this))
|
||||
}
|
||||
|
||||
def actorsOf(c: Color) = actors.values filter (_ is c)
|
||||
lazy val colorActors: Map[Color, Iterable[Actor]] = actors.values groupBy (_.color)
|
||||
|
||||
def actorsOf(c: Color) = colorActors get c getOrElse Nil
|
||||
|
||||
def actorAt(at: Pos): Valid[Actor] = actors get at toSuccess ("No piece on " + at)
|
||||
|
||||
def kingPosOf(c: Color) = pieces find (_._2 == c.king) map (_._1)
|
||||
def kingPosOf(c: Color): Option[Pos] = pieces find (_._2 == c.king) map (_._1)
|
||||
|
||||
def movesFrom(from: Pos): Valid[Set[Pos]] = actorAt(from) map (_.moves)
|
||||
|
||||
|
|
|
@ -3,16 +3,26 @@ package model
|
|||
|
||||
case class Game(
|
||||
board: Board,
|
||||
nextPlayer: Color) {
|
||||
player: Color) {
|
||||
|
||||
def this() = this(Board.empty, White)
|
||||
|
||||
val players = List(White, Black)
|
||||
|
||||
def moves: Map[Pos, Set[Pos]] = board.actors collect {
|
||||
case (pos, actor) if actor is nextPlayer ⇒ pos -> actor.moves
|
||||
lazy val actors = board actorsOf player
|
||||
|
||||
lazy val moves: Map[Pos, Set[Pos]] = actors map { actor ⇒
|
||||
actor.pos -> actor.moves
|
||||
} toMap
|
||||
|
||||
lazy val check: Boolean = board kingPosOf player map { king ⇒
|
||||
board actorsOf !player exists (_ threatens king)
|
||||
} getOrElse false
|
||||
|
||||
lazy val checkMate: Boolean = check && moves.isEmpty
|
||||
|
||||
lazy val staleMate: Boolean = !check && moves.isEmpty
|
||||
|
||||
/**
|
||||
* Find all valid moves on the board from the given position for the next turn of play
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue