Skip to content

Commit

Permalink
fix: edit Diagonal and Straight Rules to work with the generic captur…
Browse files Browse the repository at this point in the history
…e analysis
  • Loading branch information
maxim-derevyanchenko authored and jahrim committed Feb 26, 2023
1 parent 1373aa4 commit 68bcaef
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
Expand Up @@ -9,6 +9,7 @@ package io.github.chess.model.rules.chess
import io.github.chess.model.{ChessGameStatus, Position}
import io.github.chess.model.moves.{CaptureMove, Move}
import io.github.chess.model.pieces.Piece
import io.github.chess.model.Position.given
import io.github.chess.model.rules.prolog.PrologRule
import io.github.chess.util.general.GivenExtension.within

Expand Down Expand Up @@ -43,11 +44,27 @@ trait DirectionalRule extends RuleShorthands:
moves.toSet
}
}
// TODO: search for a better solution that considers captures like the one above,
// but much cleaner like the one below?
// rules.flatMap(
// _.findPositions(position)
// .takeWhile((x, y) => !status.chessBoard.pieces.contains((x, y)))
// .map(moves.Move(position, _))
// .toSet
// )

/**
* Analyzes the specified direction and removes all the moves that cannot be performed due to an obstacle.
* The move that end up on an occupied cell is kept.
*
* @param currentPosition the current position
* @param status the current state of the game
* @param direction the specified direction
* @return the set of the moves found, without the moves that would end up behind an obstacle,
* if there is one in the path
*/
protected def limitDirection(
currentPosition: Position,
status: ChessGameStatus,
direction: PrologRule
): Set[Move] =
within(status) {
val positions: LazyList[Position] =
direction.findPositions(currentPosition).map[Position](pos => pos)

(positions.takeWhile(!occupied(_)) ++ positions.dropWhile(!occupied(_)).take(1))
.map(Move(currentPosition, _))
.toSet
}
Expand Up @@ -8,12 +8,7 @@ package io.github.chess.model.rules.chess.bishop

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

/** Represent the movement rule for the [[Bishop]]. */
class BishopRule extends ChessRule:

private val diagonalRule = DiagonalRule()

override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
diagonalRule.findMoves(position, status)
class BishopRule extends DiagonalRule with AvoidAlliesRule
Expand Up @@ -21,7 +21,7 @@ class DiagonalRule extends ChessRule with DirectionalRule:
private val rules = Set(northWestRule, northEastRule, southEastRule, southWestRule)

override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
this.rules.flatMap { analyseDirection(position, status, _) }
this.rules.flatMap { limitDirection(position, status, _) }

/**
* Object for the Diagonal Rule that creates and stores a single instance of the prolog engine
Expand Down
Expand Up @@ -7,11 +7,8 @@
package io.github.chess.model.rules.chess.queen

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

/** Represent the movement rule for the [[Queen]]. */
class QueenRule extends ChessRule:
private val allDirectionsRule = AllDirectionsRule()
override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
this.allDirectionsRule.findMoves(position, status)
class QueenRule extends AllDirectionsRule with AvoidAlliesRule
Expand Up @@ -7,11 +7,8 @@
package io.github.chess.model.rules.chess.rook

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

/** Represent the movement rule for the [[Rook]]. */
class RookRule extends ChessRule:
private val straightRule = StraightRule()
override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
this.straightRule.findMoves(position, status)
class RookRule extends StraightRule with AvoidAlliesRule
Expand Up @@ -8,14 +8,9 @@ package io.github.chess.model.rules.chess.rook

import io.github.chess.util.general.GivenExtension.within
import io.github.chess.model.moves.Move
import io.github.chess.model.rules.chess.ChessRule.*
import io.github.chess.model.rules.chess.{ChessRule, DirectionalRule}
import io.github.chess.model.rules.chess.ChessRule.*
import io.github.chess.model.rules.chess.rook.StraightRule.{
ePrologRule,
nPrologRule,
sPrologRule,
wPrologRule
}
import io.github.chess.model.rules.prolog.{
EPrologRule,
NPrologRule,
Expand All @@ -35,7 +30,7 @@ class StraightRule extends ChessRule with DirectionalRule:
StraightRule.ePrologRule
)
override def findMoves(position: Position, status: ChessGameStatus): Set[Move] =
this.rules.flatMap { analyseDirection(position, status, _) }
this.rules.flatMap { limitDirection(position, status, _) }

/**
* Object for the Straight Rule that creates and stores a single instance of the prolog engine
Expand Down

0 comments on commit 68bcaef

Please sign in to comment.