Skip to content

Commit

Permalink
feat: add capture analyzis to the King and the Knight
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-derevyanchenko authored and jahrim committed Feb 26, 2023
1 parent 96972d5 commit 1373aa4
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 16 deletions.
Expand Up @@ -10,7 +10,7 @@ import io.github.chess.model.moves.Move
import io.github.chess.model.{ChessGameStatus, Position}

/** Chess rule mixin that checks the presence of a piece in the destination position after finding it. */
trait PresenceRule extends ChessRule:
trait AvoidAllPiecesRule extends ChessRule:

abstract override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
super.findMoves(position, status).filter(move => !status.chessBoard.pieces.contains(move.to))
Expand Up @@ -9,7 +9,7 @@ import io.github.chess.model.{ChessGameStatus, Position}
import io.github.chess.model.moves.{CaptureMove, Move}

/**
* Mixin for [[ChessRule]]s, that removes from the set of [[Move]]s those that would capture an ally piece
* Mixin for [[ChessRule]]s, that removes from the set of [[Move]]s those that would capture an ally piece
* (removes "friendly fire").
*/
trait AvoidAlliesRule extends CaptureMoveMapper:
Expand Down
Expand Up @@ -10,8 +10,8 @@ import io.github.chess.model.{ChessGameStatus, Position}
import io.github.chess.model.moves.{CaptureMove, Move}

/**
* Mixin for [[ChessRule]]s, that checks whether there are some [[Move]]s that imply
* the capture of a piece (without looking if it's an ally or adversary piece)
* Mixin for [[ChessRule]]s, that checks whether there are some [[Move]]s that imply
* the capture of a piece (without looking if it's an ally or adversary piece)
* and maps them into [[CaptureMove]]s
*/
trait CaptureMoveMapper extends ChessRule:
Expand Down
Expand Up @@ -6,7 +6,7 @@
*/
package io.github.chess.model.rules.chess.king

import io.github.chess.model.rules.chess.PresenceRule
import io.github.chess.model.rules.chess.AvoidAlliesRule

/** Represent the movement rule for the [[King]]. */
class KingMovementRule extends AllDirectionsOneStepRule with PresenceRule
class KingMovementRule extends AllDirectionsOneStepRule with AvoidAlliesRule
Expand Up @@ -6,7 +6,7 @@
*/
package io.github.chess.model.rules.chess.knight

import io.github.chess.model.rules.chess.PresenceRule
import io.github.chess.model.rules.chess.AvoidAlliesRule

/** Represent the movement rule for the [[Knight]]. */
class KnightRule extends LRule with PresenceRule
class KnightRule extends LRule with AvoidAlliesRule
Expand Up @@ -6,7 +6,7 @@
*/
package io.github.chess.model.rules.chess.pawn

import io.github.chess.model.rules.chess.PresenceRule
import io.github.chess.model.rules.chess.AvoidAllPiecesRule

/** Implementation of the rule that makes move a piece two positions forward in the column, if there is no other piece there. */
class DoubleMoveRule extends TwoStepRule with PresenceRule
class DoubleMoveRule extends TwoStepRule with AvoidAllPiecesRule
Expand Up @@ -6,7 +6,7 @@
*/
package io.github.chess.model.rules.chess.pawn

import io.github.chess.model.rules.chess.PresenceRule
import io.github.chess.model.rules.chess.AvoidAllPiecesRule

/** Implementation of the rule that makes move a piece one step forward in the column, if there is no other piece there. */
class ForwardOneRule extends SingleStepRule with PresenceRule
class ForwardOneRule extends SingleStepRule with AvoidAllPiecesRule
Expand Up @@ -24,11 +24,11 @@ class PawnCaptureMoves extends ChessRule with RuleShorthands:
case Team.WHITE => whitePawnCaptureRule
case Team.BLACK => blackPawnCaptureRule
).findPositions(position)
.map(toPosition => {
.map(toPosition =>
status.chessBoard.pieces.get(toPosition) match
case Some(capturedPiece) => CaptureMove(position, toPosition, capturedPiece)
case None => Move(position, toPosition)
})
)
.toSet
case None => Set.empty

Expand Down
Expand Up @@ -7,7 +7,7 @@
package io.github.chess.model.rules.chess

import io.github.chess.AbstractSpec
import io.github.chess.model.pieces.Pawn
import io.github.chess.model.pieces.{Knight, Pawn}
import io.github.chess.model.rules.chess.knight.KnightRule
import io.github.chess.model.{ChessBoard, ChessGameStatus, Position, Team}

Expand All @@ -19,6 +19,7 @@ class KnightRuleSpec extends AbstractSpec:
private val lRule = KnightRule()
private val chessBoard: ChessBoard = ChessBoard.empty
chessBoard.setPiece((0, 1), Pawn(Team.WHITE))
chessBoard.setPiece(initialPosition, Knight(Team.WHITE))
private val chessGameStatus = ChessGameStatus(chessBoard)
private val moves = lRule.findMoves(initialPosition, chessGameStatus).map(_.to)

Expand Down
Expand Up @@ -7,7 +7,7 @@
package io.github.chess.model.rules.chess.king

import io.github.chess.AbstractSpec
import io.github.chess.model.pieces.Pawn
import io.github.chess.model.pieces.{King, Pawn}
import io.github.chess.model.{ChessBoard, ChessGameStatus, Position, Team}

/** Test suite for [[King]]. */
Expand All @@ -19,6 +19,7 @@ class KingRuleSpec extends AbstractSpec:
private val rule = KingMovementRule()
private val chessBoard: ChessBoard = ChessBoard.empty
chessBoard.setPiece((0, 1), Pawn(Team.WHITE))
chessBoard.setPiece(initialPosition, King(Team.WHITE))
private val chessGameStatus = ChessGameStatus(chessBoard)
private val moves = rule.findMoves(initialPosition, chessGameStatus).map(_.to)

Expand Down

0 comments on commit 1373aa4

Please sign in to comment.