Skip to content

Commit

Permalink
feat: add the finding of all moving positions of a Pawn
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-derevyanchenko authored and mirko-felice committed Feb 20, 2023
1 parent be93e3f commit 330e591
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 27 deletions.
@@ -0,0 +1,16 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.rules

import io.github.chess.model.rules.ChessRule
import io.github.chess.model.{ChessGameStatus, Move, Position}

/** Chess rule mixin that checks the presence of a piece in the destination position after finding it. */
trait PresenceRule 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 @@ -6,13 +6,7 @@
*/
package io.github.chess.model.rules.pawn

import io.github.chess.model.{Move, Position}
import io.github.chess.model.rules.ChessRule
import io.github.chess.model.rules.PresenceRule

/** Implementation of the Pawn rule that makes it move two steps forward as the first move. */
//TODO Check that it's actually its first move.
class DoubleMoveRule extends ChessRule:

override def findMoves(position: Position): Set[Move] = Set(
Move(position, position.rankUp().rankUp())
)
/** 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
Expand Up @@ -6,10 +6,7 @@
*/
package io.github.chess.model.rules.pawn

import io.github.chess.model.{Move, Position}
import io.github.chess.model.rules.ChessRule
import io.github.chess.model.rules.PresenceRule

/** Implementation of the Pawn rule that makes it move one step forward. */
class ForwardOneRule extends ChessRule:

override def findMoves(position: Position): Set[Move] = Set(Move(position, position.rankUp()))
/** 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
@@ -0,0 +1,25 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.rules.pawn

import io.github.chess.model.{ChessGameStatus, Move, Position, Rank, Team}
import io.github.chess.model.rules.ChessRule

/** Set of Pawn movement rules. */
class PawnMovementRule extends ChessRule:

override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
val firstStep = ForwardOneRule().findMoves(position, status)
if firstStep.nonEmpty && isFirstMove(position, status) then
firstStep ++ DoubleMoveRule().findMoves(position, status)
else firstStep

private def isFirstMove(position: Position, status: ChessGameStatus): Boolean =
position.rank == (status.currentTurn match
case Team.WHITE => Rank._2
case Team.BLACK => Rank._7
)
Expand Up @@ -6,13 +6,11 @@
*/
package io.github.chess.model.rules.pawn

import io.github.chess.model.{Move, Position}
import io.github.chess.model.{ChessGameStatus, Move, Pawn, Position}
import io.github.chess.model.rules.ChessRule
import io.github.chess.model.Pawn

/** Implementation of the [[ChessRule]] that applies the rules of the [[Pawn]]. */
case class PawnRule() extends ChessRule:

override def findMoves(position: Position): Set[Move] =
ForwardOneRule().findMoves(position) ++
DoubleMoveRule().findMoves(position)
override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
PawnMovementRule().findMoves(position, status)
@@ -0,0 +1,23 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.rules.pawn

import io.github.chess.model.{ChessGameStatus, Move, Position, Team}
import io.github.chess.model.rules.ChessRule

/** Implementation of a chess rule that makes move a piece one step forward in the column. */
class SingleStepRule extends ChessRule:

override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
Set(
Move(
position,
status.currentTurn match
case Team.WHITE => position.rankUp()
case Team.BLACK => position.rankDown()
)
)
@@ -0,0 +1,23 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.rules.pawn

import io.github.chess.model.rules.ChessRule
import io.github.chess.model.{ChessGameStatus, Move, Position, Team}

/** Implementation of a chess rule that makes move a piece two positions forward in the column. */
class TwoStepRule extends ChessRule:

override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
Set(
Move(
position,
status.currentTurn match
case Team.WHITE => position.rankUp().rankUp()
case Team.BLACK => position.rankDown().rankDown()
)
)
Expand Up @@ -7,7 +7,8 @@
package io.github.chess.model.rules.pawn

import io.github.chess.AbstractSpec
import io.github.chess.model.Position
import io.github.chess.model.{ChessBoard, ChessGameStatus, Position}
import io.vertx.core.Vertx

/** Test suit for all Pawn movement rules. */
class PawnRulesSpec extends AbstractSpec:
Expand All @@ -16,22 +17,24 @@ class PawnRulesSpec extends AbstractSpec:
val pawnNextPosition: Position = (0, 2)
val pawnDoubleStepPosition: Position = (0, 3)
val doubleStepRule: DoubleMoveRule = DoubleMoveRule()
val board: ChessBoard = ChessBoard(Vertx.vertx())
val status: ChessGameStatus = ChessGameStatus(board)

"The Forward rule" should "let move the pawn only to the following rank, without changing its file" in {
val oneStepRule = ForwardOneRule()
val moves = oneStepRule.findMoves(pawnInitialPosition)
val moves = oneStepRule.findMoves(pawnInitialPosition, status)
moves should have size 1
all(moves) should have(Symbol("to")(pawnNextPosition))
}

"Double move rule" should "let move the pawn only to the rank two steps ahead of the pawn's current position, " +
"without changing its file" in {
val moves = doubleStepRule.findMoves(pawnInitialPosition)
moves should have size 1
all(moves) should have(Symbol("to")(pawnDoubleStepPosition))
}
val moves = doubleStepRule.findMoves(pawnInitialPosition, status)
moves should have size 1
all(moves) should have(Symbol("to")(pawnDoubleStepPosition))
}

// TODO add test to check that the double step works only on the first move of the pawn and not on the next ones.
// TODO add test to check that the double step works only on the first move of the pawn and not on the next ones.
// it should "give no moves if the pawn is not in its first position" in {
// val moves = doubleStepRule.findMoves(pawnNextPosition)
// moves should have size 0
Expand Down

0 comments on commit 330e591

Please sign in to comment.