{-# LANGUAGE CPP, LambdaCase #-}
module BishBosh.State.Board(
Board(
getMaybePieceByCoordinates,
getCoordinatesByRankByLogicalColour,
getNDefendersByCoordinatesByLogicalColour,
getNPiecesDifferenceByRank,
getNPawnsByFileByLogicalColour,
getNPieces,
getPassedPawnCoordinatesByLogicalColour
),
countDefendersByCoordinatesByLogicalColour,
summariseNDefendersByLogicalColour,
sumPieceSquareValueByLogicalColour,
findAttackersOf,
findAttacksBy,
movePiece,
passesThroughCheck,
isKingChecked,
exposesKing
) where
import Control.Arrow((&&&), (***))
import Data.Array.IArray((!), (//))
import qualified BishBosh.Attribute.MoveType as Attribute.MoveType
import qualified BishBosh.Attribute.Rank as Attribute.Rank
import qualified BishBosh.Cartesian.Coordinates as Cartesian.Coordinates
import qualified BishBosh.Cartesian.Vector as Cartesian.Vector
import qualified BishBosh.Colour.LogicalColour as Colour.LogicalColour
import qualified BishBosh.Component.Accountant as Component.Accountant
import qualified BishBosh.Component.Move as Component.Move
import qualified BishBosh.Component.Piece as Component.Piece
import qualified BishBosh.Component.PieceSquareValueByCoordinatesByRank as Component.PieceSquareValueByCoordinatesByRank
import qualified BishBosh.Data.Exception as Data.Exception
import qualified BishBosh.Direction.Direction as Direction.Direction
import qualified BishBosh.Property.Empty as Property.Empty
import qualified BishBosh.Property.ExtendedPositionDescription as Property.ExtendedPositionDescription
import qualified BishBosh.Property.FixedMembership as Property.FixedMembership
import qualified BishBosh.Property.ForsythEdwards as Property.ForsythEdwards
import qualified BishBosh.Property.Opposable as Property.Opposable
import qualified BishBosh.Property.Reflectable as Property.Reflectable
import qualified BishBosh.Property.SelfValidating as Property.SelfValidating
import qualified BishBosh.State.CoordinatesByRankByLogicalColour as State.CoordinatesByRankByLogicalColour
import qualified BishBosh.State.MaybePieceByCoordinates as State.MaybePieceByCoordinates
import qualified BishBosh.StateProperty.Censor as StateProperty.Censor
import qualified BishBosh.StateProperty.Hashable as StateProperty.Hashable
import qualified BishBosh.StateProperty.Mutator as StateProperty.Mutator
import qualified BishBosh.StateProperty.Seeker as StateProperty.Seeker
import qualified BishBosh.StateProperty.View as StateProperty.View
import qualified BishBosh.Type.Count as Type.Count
import qualified BishBosh.Type.Mass as Type.Mass
import qualified Control.Arrow
import qualified Control.DeepSeq
import qualified Control.Exception
import qualified Data.Array.IArray
import qualified Data.Default
import qualified Data.Foldable
import qualified Data.List
import qualified Data.Map.Strict as Map
import qualified Data.Maybe
type Transformation = Board -> Board
type NDefendersByCoordinatesByLogicalColour = Colour.LogicalColour.ArrayByLogicalColour (Map.Map Cartesian.Coordinates.Coordinates Type.Count.NPieces)
data Board = MkBoard {
Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates :: State.MaybePieceByCoordinates.MaybePieceByCoordinates,
Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour :: State.CoordinatesByRankByLogicalColour.CoordinatesByRankByLogicalColour,
Board -> NDefendersByCoordinatesByLogicalColour
getNDefendersByCoordinatesByLogicalColour :: NDefendersByCoordinatesByLogicalColour,
Board -> NPiecesByRank
getNPiecesDifferenceByRank :: StateProperty.Censor.NPiecesByRank,
Board -> NPiecesByFileByLogicalColour
getNPawnsByFileByLogicalColour :: StateProperty.Seeker.NPiecesByFileByLogicalColour,
Board -> NPieces
getNPieces :: Type.Count.NPieces,
Board -> CoordinatesByLogicalColour
getPassedPawnCoordinatesByLogicalColour :: State.CoordinatesByRankByLogicalColour.CoordinatesByLogicalColour
}
instance Eq Board where
MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates } == :: Board -> Board -> Bool
== MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates' } = MaybePieceByCoordinates
maybePieceByCoordinates MaybePieceByCoordinates -> MaybePieceByCoordinates -> Bool
forall a. Eq a => a -> a -> Bool
== MaybePieceByCoordinates
maybePieceByCoordinates'
instance Control.DeepSeq.NFData Board where
rnf :: Board -> ()
rnf MkBoard {
getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates,
getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
getNDefendersByCoordinatesByLogicalColour :: Board -> NDefendersByCoordinatesByLogicalColour
getNDefendersByCoordinatesByLogicalColour = NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour,
#ifndef UNBOX_TYPECOUNT_ARRAYS
getNPiecesDifferenceByRank = nPiecesDifferenceByRank,
#endif
getNPawnsByFileByLogicalColour :: Board -> NPiecesByFileByLogicalColour
getNPawnsByFileByLogicalColour = NPiecesByFileByLogicalColour
nPawnsByFileByLogicalColour,
getNPieces :: Board -> NPieces
getNPieces = NPieces
nPieces,
getPassedPawnCoordinatesByLogicalColour :: Board -> CoordinatesByLogicalColour
getPassedPawnCoordinatesByLogicalColour = CoordinatesByLogicalColour
passedPawnCoordinatesByLogicalColour
} = (MaybePieceByCoordinates, CoordinatesByRankByLogicalColour,
NDefendersByCoordinatesByLogicalColour,
NPiecesByFileByLogicalColour, NPieces, CoordinatesByLogicalColour)
-> ()
forall a. NFData a => a -> ()
Control.DeepSeq.rnf (
MaybePieceByCoordinates
maybePieceByCoordinates,
CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour,
#ifndef UNBOX_TYPECOUNT_ARRAYS
nPiecesDifferenceByRank,
#endif
NPiecesByFileByLogicalColour
nPawnsByFileByLogicalColour,
NPieces
nPieces,
CoordinatesByLogicalColour
passedPawnCoordinatesByLogicalColour
)
instance Read Board where
readsPrec :: NPieces -> ReadS Board
readsPrec NPieces
_ = ReadS Board
forall a. ReadsFEN a => ReadS a
Property.ForsythEdwards.readsFEN
instance Show Board where
showsPrec :: NPieces -> Board -> ShowS
showsPrec NPieces
_ = Board -> ShowS
forall a. ShowsFEN a => a -> ShowS
Property.ForsythEdwards.showsFEN
instance Property.ExtendedPositionDescription.ReadsEPD Board where
readsEPD :: ReadS Board
readsEPD = ((MaybePieceByCoordinates, String) -> (Board, String))
-> [(MaybePieceByCoordinates, String)] -> [(Board, String)]
forall a b. (a -> b) -> [a] -> [b]
map ((MaybePieceByCoordinates -> Board)
-> (MaybePieceByCoordinates, String) -> (Board, String)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
Control.Arrow.first MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates) ([(MaybePieceByCoordinates, String)] -> [(Board, String)])
-> (String -> [(MaybePieceByCoordinates, String)]) -> ReadS Board
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [(MaybePieceByCoordinates, String)]
forall a. ReadsEPD a => ReadS a
Property.ExtendedPositionDescription.readsEPD
instance Property.ExtendedPositionDescription.ShowsEPD Board where
showsEPD :: Board -> ShowS
showsEPD MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates } = MaybePieceByCoordinates -> ShowS
forall a. ShowsEPD a => a -> ShowS
Property.ExtendedPositionDescription.showsEPD MaybePieceByCoordinates
maybePieceByCoordinates
instance Property.ForsythEdwards.ReadsFEN Board where
readsFEN :: ReadS Board
readsFEN = ReadS Board
forall a. ReadsEPD a => ReadS a
Property.ExtendedPositionDescription.readsEPD
instance Property.ForsythEdwards.ShowsFEN Board
instance Data.Default.Default Board where
def :: Board
def = MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates MaybePieceByCoordinates
forall a. Default a => a
Data.Default.def
instance Property.Empty.Empty Board where
empty :: Board
empty = MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates MaybePieceByCoordinates
forall a. Empty a => a
Property.Empty.empty
instance Property.Reflectable.ReflectableOnX Board where
reflectOnX :: Board -> Board
reflectOnX MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates } = MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates (MaybePieceByCoordinates -> Board)
-> MaybePieceByCoordinates -> Board
forall a b. (a -> b) -> a -> b
$ MaybePieceByCoordinates -> MaybePieceByCoordinates
forall a. ReflectableOnX a => a -> a
Property.Reflectable.reflectOnX MaybePieceByCoordinates
maybePieceByCoordinates
instance Property.Reflectable.ReflectableOnY Board where
reflectOnY :: Board -> Board
reflectOnY MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates } = MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates (MaybePieceByCoordinates -> Board)
-> MaybePieceByCoordinates -> Board
forall a b. (a -> b) -> a -> b
$ MaybePieceByCoordinates -> MaybePieceByCoordinates
forall a. ReflectableOnY a => a -> a
Property.Reflectable.reflectOnY MaybePieceByCoordinates
maybePieceByCoordinates
instance StateProperty.Hashable.Hashable Board where
listRandoms :: Zobrist positionHash -> Board -> [positionHash]
listRandoms Zobrist positionHash
zobrist MkBoard { getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour } = Zobrist positionHash
-> CoordinatesByRankByLogicalColour -> [positionHash]
forall hashable positionHash.
Hashable hashable =>
Zobrist positionHash -> hashable -> [positionHash]
StateProperty.Hashable.listRandoms Zobrist positionHash
zobrist CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
instance StateProperty.Mutator.Mutator Board where
defineCoordinates :: Maybe Piece -> Coordinates -> Board -> Board
defineCoordinates Maybe Piece
maybePiece Coordinates
coordinates MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates } = MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates (MaybePieceByCoordinates -> Board)
-> MaybePieceByCoordinates -> Board
forall a b. (a -> b) -> a -> b
$ Maybe Piece
-> Coordinates
-> MaybePieceByCoordinates
-> MaybePieceByCoordinates
forall mutator.
Mutator mutator =>
Maybe Piece -> Coordinates -> mutator -> mutator
StateProperty.Mutator.defineCoordinates Maybe Piece
maybePiece Coordinates
coordinates MaybePieceByCoordinates
maybePieceByCoordinates
movePiece :: Move -> MoveType -> Piece -> Board -> Board
movePiece Move
move MoveType
moveType Piece
sourcePiece MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates } = MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates (MaybePieceByCoordinates -> Board)
-> MaybePieceByCoordinates -> Board
forall a b. (a -> b) -> a -> b
$ Move
-> MoveType
-> Piece
-> MaybePieceByCoordinates
-> MaybePieceByCoordinates
forall mutator.
Mutator mutator =>
Move -> MoveType -> Piece -> mutator -> mutator
StateProperty.Mutator.movePiece Move
move MoveType
moveType Piece
sourcePiece MaybePieceByCoordinates
maybePieceByCoordinates
instance StateProperty.Seeker.Seeker Board where
findProximateKnights :: Board -> LogicalColour -> Coordinates -> [Coordinates]
findProximateKnights MkBoard {
getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
} = CoordinatesByRankByLogicalColour
-> LogicalColour -> Coordinates -> [Coordinates]
forall seeker.
Seeker seeker =>
seeker -> LogicalColour -> Coordinates -> [Coordinates]
StateProperty.Seeker.findProximateKnights CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
findPieces :: (Piece -> Bool) -> Board -> [LocatedPiece]
findPieces Piece -> Bool
predicate MkBoard {
getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
} = (Piece -> Bool)
-> CoordinatesByRankByLogicalColour -> [LocatedPiece]
forall seeker.
Seeker seeker =>
(Piece -> Bool) -> seeker -> [LocatedPiece]
StateProperty.Seeker.findPieces Piece -> Bool
predicate CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
countPawnsByFileByLogicalColour :: Board -> NPiecesByFileByLogicalColour
countPawnsByFileByLogicalColour MkBoard {
getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
} = CoordinatesByRankByLogicalColour -> NPiecesByFileByLogicalColour
forall seeker.
Seeker seeker =>
seeker -> NPiecesByFileByLogicalColour
StateProperty.Seeker.countPawnsByFileByLogicalColour CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
instance Property.SelfValidating.SelfValidating Board where
findInvalidity :: Board -> [String]
findInvalidity = (MaybePieceByCoordinates, CoordinatesByRankByLogicalColour)
-> [String]
forall a. SelfValidating a => a -> [String]
Property.SelfValidating.findInvalidity ((MaybePieceByCoordinates, CoordinatesByRankByLogicalColour)
-> [String])
-> (Board
-> (MaybePieceByCoordinates, CoordinatesByRankByLogicalColour))
-> Board
-> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates (Board -> MaybePieceByCoordinates)
-> (Board -> CoordinatesByRankByLogicalColour)
-> Board
-> (MaybePieceByCoordinates, CoordinatesByRankByLogicalColour)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour)
fromMaybePieceByCoordinates :: State.MaybePieceByCoordinates.MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates :: MaybePieceByCoordinates -> Board
fromMaybePieceByCoordinates MaybePieceByCoordinates
maybePieceByCoordinates = Board
board where
board :: Board
board@MkBoard { getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour } = MkBoard :: MaybePieceByCoordinates
-> CoordinatesByRankByLogicalColour
-> NDefendersByCoordinatesByLogicalColour
-> NPiecesByRank
-> NPiecesByFileByLogicalColour
-> NPieces
-> CoordinatesByLogicalColour
-> Board
MkBoard {
getMaybePieceByCoordinates :: MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates,
getCoordinatesByRankByLogicalColour :: CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = MaybePieceByCoordinates -> CoordinatesByRankByLogicalColour
forall seeker view. (Seeker seeker, View view) => seeker -> view
StateProperty.View.translate MaybePieceByCoordinates
maybePieceByCoordinates,
getNDefendersByCoordinatesByLogicalColour :: NDefendersByCoordinatesByLogicalColour
getNDefendersByCoordinatesByLogicalColour = Board -> NDefendersByCoordinatesByLogicalColour
countDefendersByCoordinatesByLogicalColour Board
board,
getNPiecesDifferenceByRank :: NPiecesByRank
getNPiecesDifferenceByRank = CoordinatesByRankByLogicalColour -> NPiecesByRank
forall censor. Censor censor => censor -> NPiecesByRank
StateProperty.Censor.countPieceDifferenceByRank CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
getNPawnsByFileByLogicalColour :: NPiecesByFileByLogicalColour
getNPawnsByFileByLogicalColour = CoordinatesByRankByLogicalColour -> NPiecesByFileByLogicalColour
forall seeker.
Seeker seeker =>
seeker -> NPiecesByFileByLogicalColour
StateProperty.Seeker.countPawnsByFileByLogicalColour CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
getNPieces :: NPieces
getNPieces = CoordinatesByRankByLogicalColour -> NPieces
forall censor. Censor censor => censor -> NPieces
StateProperty.Censor.countPieces CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
getPassedPawnCoordinatesByLogicalColour :: CoordinatesByLogicalColour
getPassedPawnCoordinatesByLogicalColour = CoordinatesByRankByLogicalColour -> CoordinatesByLogicalColour
State.CoordinatesByRankByLogicalColour.findPassedPawnCoordinatesByLogicalColour CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
}
movePiece
:: Component.Move.Move
-> Maybe Attribute.MoveType.MoveType
-> Transformation
movePiece :: Move -> Maybe MoveType -> Board -> Board
movePiece Move
move Maybe MoveType
maybeMoveType board :: Board
board@MkBoard {
getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates,
getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
getNDefendersByCoordinatesByLogicalColour :: Board -> NDefendersByCoordinatesByLogicalColour
getNDefendersByCoordinatesByLogicalColour = NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour,
getNPiecesDifferenceByRank :: Board -> NPiecesByRank
getNPiecesDifferenceByRank = NPiecesByRank
nPiecesDifferenceByRank,
getNPieces :: Board -> NPieces
getNPieces = NPieces
nPieces
}
| Just Piece
sourcePiece <- MaybePieceByCoordinates -> Coordinates -> Maybe Piece
State.MaybePieceByCoordinates.dereference MaybePieceByCoordinates
maybePieceByCoordinates Coordinates
source = let
(LogicalColour
logicalColour, (LogicalColour
opponentsLogicalColour, Piece
oppositePiece)) = Piece -> LogicalColour
Component.Piece.getLogicalColour (Piece -> LogicalColour)
-> (Piece -> (LogicalColour, Piece))
-> Piece
-> (LogicalColour, (LogicalColour, Piece))
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((Piece -> LogicalColour
Component.Piece.getLogicalColour (Piece -> LogicalColour)
-> (Piece -> Piece) -> Piece -> (LogicalColour, Piece)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Piece -> Piece
forall a. a -> a
id) (Piece -> (LogicalColour, Piece))
-> (Piece -> Piece) -> Piece -> (LogicalColour, Piece)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Piece -> Piece
forall a. Opposable a => a -> a
Property.Opposable.getOpposite) (Piece -> (LogicalColour, (LogicalColour, Piece)))
-> Piece -> (LogicalColour, (LogicalColour, Piece))
forall a b. (a -> b) -> a -> b
$ Piece
sourcePiece
moveType :: Attribute.MoveType.MoveType
moveType :: MoveType
moveType
| Just MoveType
explicitMoveType <- Maybe MoveType
maybeMoveType = MoveType
explicitMoveType
| MaybePieceByCoordinates -> Move -> Bool
State.MaybePieceByCoordinates.isEnPassantMove MaybePieceByCoordinates
maybePieceByCoordinates Move
move = MoveType
Attribute.MoveType.enPassant
| Bool
otherwise = Maybe Rank -> Maybe Rank -> MoveType
Attribute.MoveType.mkNormalMoveType (
Piece -> Rank
Component.Piece.getRank (Piece -> Rank) -> Maybe Piece -> Maybe Rank
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MaybePieceByCoordinates -> Coordinates -> Maybe Piece
State.MaybePieceByCoordinates.dereference MaybePieceByCoordinates
maybePieceByCoordinates Coordinates
destination
) (Maybe Rank -> MoveType) -> Maybe Rank -> MoveType
forall a b. (a -> b) -> a -> b
$ if Piece -> Coordinates -> Bool
Component.Piece.isPawnPromotion Piece
sourcePiece Coordinates
destination
then Rank -> Maybe Rank
forall a. a -> Maybe a
Just Rank
Attribute.Rank.defaultPromotionRank
else Maybe Rank
forall a. Maybe a
Nothing
(Piece
destinationPiece, (Maybe Piece
maybeExplicitlyTakenPiece, Bool
wasPawnTakenExplicitly)) = ((Piece -> Piece) -> Piece -> Piece
forall a b. (a -> b) -> a -> b
$ Piece
sourcePiece) ((Piece -> Piece) -> Piece)
-> (MoveType -> Piece -> Piece) -> MoveType -> Piece
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Piece -> Piece)
-> (Rank -> Piece -> Piece) -> Maybe Rank -> Piece -> Piece
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe Piece -> Piece
forall a. a -> a
id Rank -> Piece -> Piece
Component.Piece.promote (Maybe Rank -> Piece -> Piece)
-> (MoveType -> Maybe Rank) -> MoveType -> Piece -> Piece
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MoveType -> Maybe Rank
forall a. Promotable a => a -> Maybe Rank
Attribute.Rank.getMaybePromotionRank (MoveType -> Piece)
-> (MoveType -> (Maybe Piece, Bool))
-> MoveType
-> (Piece, (Maybe Piece, Bool))
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((Rank -> Piece) -> Maybe Rank -> Maybe Piece
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (LogicalColour -> Rank -> Piece
Component.Piece.mkPiece LogicalColour
opponentsLogicalColour) (Maybe Rank -> Maybe Piece)
-> (Maybe Rank -> Bool) -> Maybe Rank -> (Maybe Piece, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& (Maybe Rank -> Maybe Rank -> Bool
forall a. Eq a => a -> a -> Bool
== Rank -> Maybe Rank
forall a. a -> Maybe a
Just Rank
Attribute.Rank.Pawn)) (Maybe Rank -> (Maybe Piece, Bool))
-> (MoveType -> Maybe Rank) -> MoveType -> (Maybe Piece, Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MoveType -> Maybe Rank
Attribute.MoveType.getMaybeExplicitlyTakenRank (MoveType -> (Piece, (Maybe Piece, Bool)))
-> MoveType -> (Piece, (Maybe Piece, Bool))
forall a b. (a -> b) -> a -> b
$! MoveType
moveType
movePiece' :: StateProperty.Mutator.Mutator mutator => mutator -> mutator
movePiece' :: mutator -> mutator
movePiece' = Move -> MoveType -> Piece -> mutator -> mutator
forall mutator.
Mutator mutator =>
Move -> MoveType -> Piece -> mutator -> mutator
StateProperty.Mutator.movePiece Move
move MoveType
moveType Piece
sourcePiece
enpassantCaptureCoordinates :: Cartesian.Coordinates.Coordinates
enpassantCaptureCoordinates :: Coordinates
enpassantCaptureCoordinates = LogicalColour -> Transformation
Cartesian.Coordinates.retreat LogicalColour
logicalColour Coordinates
destination
board' :: Board
board'@MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates' } = MkBoard :: MaybePieceByCoordinates
-> CoordinatesByRankByLogicalColour
-> NDefendersByCoordinatesByLogicalColour
-> NPiecesByRank
-> NPiecesByFileByLogicalColour
-> NPieces
-> CoordinatesByLogicalColour
-> Board
MkBoard {
getMaybePieceByCoordinates :: MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates -> MaybePieceByCoordinates
forall mutator. Mutator mutator => mutator -> mutator
movePiece' MaybePieceByCoordinates
maybePieceByCoordinates,
getCoordinatesByRankByLogicalColour :: CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
-> CoordinatesByRankByLogicalColour
forall mutator. Mutator mutator => mutator -> mutator
movePiece' CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
getNDefendersByCoordinatesByLogicalColour :: NDefendersByCoordinatesByLogicalColour
getNDefendersByCoordinatesByLogicalColour = (
\(Map Coordinates NPieces
nBlackDefendersByCoordinates, Map Coordinates NPieces
nWhiteDefendersByCoordinates) -> [Map Coordinates NPieces] -> NDefendersByCoordinatesByLogicalColour
forall (a :: * -> * -> *) e. IArray a e => [e] -> a LogicalColour e
Colour.LogicalColour.listArrayByLogicalColour [Map Coordinates NPieces
nBlackDefendersByCoordinates, Map Coordinates NPieces
nWhiteDefendersByCoordinates]
) ((Map Coordinates NPieces, Map Coordinates NPieces)
-> NDefendersByCoordinatesByLogicalColour)
-> ([LocatedPiece]
-> (Map Coordinates NPieces, Map Coordinates NPieces))
-> [LocatedPiece]
-> NDefendersByCoordinatesByLogicalColour
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LocatedPiece
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces))
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> [LocatedPiece]
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (
\(Coordinates
affectedCoordinates, Piece
affectedPiece) -> if Piece -> Bool
Component.Piece.isKing Piece
affectedPiece
then (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall a. a -> a
id
else let
logicalColour' :: LogicalColour
logicalColour' = Piece -> LogicalColour
Component.Piece.getLogicalColour Piece
affectedPiece
in (
if LogicalColour -> Bool
Colour.LogicalColour.isBlack LogicalColour
logicalColour'
then (Map Coordinates NPieces -> Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
Control.Arrow.first
else (Map Coordinates NPieces -> Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
Control.Arrow.second
) ((Map Coordinates NPieces -> Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces))
-> ([(Coordinates, Rank)]
-> Map Coordinates NPieces -> Map Coordinates NPieces)
-> [(Coordinates, Rank)]
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coordinates
-> NPieces -> Map Coordinates NPieces -> Map Coordinates NPieces
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Coordinates
affectedCoordinates (NPieces -> Map Coordinates NPieces -> Map Coordinates NPieces)
-> ([(Coordinates, Rank)] -> NPieces)
-> [(Coordinates, Rank)]
-> Map Coordinates NPieces
-> Map Coordinates NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> NPieces
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NPieces -> NPieces)
-> ([(Coordinates, Rank)] -> NPieces)
-> [(Coordinates, Rank)]
-> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Coordinates, Rank)] -> NPieces
forall (t :: * -> *) a. Foldable t => t a -> NPieces
length ([(Coordinates, Rank)]
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces))
-> [(Coordinates, Rank)]
-> (Map Coordinates NPieces, Map Coordinates NPieces)
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall a b. (a -> b) -> a -> b
$! Board -> LogicalColour -> Coordinates -> [(Coordinates, Rank)]
findAttackersOf Board
board' (
LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite LogicalColour
logicalColour'
) Coordinates
affectedCoordinates
) (
(NDefendersByCoordinatesByLogicalColour
-> LogicalColour -> Map Coordinates NPieces
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour
Colour.LogicalColour.Black) (NDefendersByCoordinatesByLogicalColour -> Map Coordinates NPieces)
-> (NDefendersByCoordinatesByLogicalColour
-> Map Coordinates NPieces)
-> NDefendersByCoordinatesByLogicalColour
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& (NDefendersByCoordinatesByLogicalColour
-> LogicalColour -> Map Coordinates NPieces
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour
Colour.LogicalColour.White) (NDefendersByCoordinatesByLogicalColour
-> (Map Coordinates NPieces, Map Coordinates NPieces))
-> NDefendersByCoordinatesByLogicalColour
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall a b. (a -> b) -> a -> b
$ NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour NDefendersByCoordinatesByLogicalColour
-> [(LogicalColour, Map Coordinates NPieces)]
-> NDefendersByCoordinatesByLogicalColour
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> [(i, e)] -> a i e
// (
let
nDefendersByCoordinates :: Map Coordinates NPieces
nDefendersByCoordinates = NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour NDefendersByCoordinatesByLogicalColour
-> LogicalColour -> Map Coordinates NPieces
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour
opponentsLogicalColour
in (Bool
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)],
[(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)],
(Maybe Rank, Maybe Rank)
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)])
-> MoveType
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall a.
(Bool -> a, a, (Maybe Rank, Maybe Rank) -> a) -> MoveType -> a
Attribute.MoveType.apply (
([(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)])
-> Bool
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall a b. a -> b -> a
const [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall a. a -> a
id,
(:) ((LogicalColour, Map Coordinates NPieces)
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)])
-> (Map Coordinates NPieces
-> (LogicalColour, Map Coordinates NPieces))
-> Map Coordinates NPieces
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) LogicalColour
opponentsLogicalColour (Map Coordinates NPieces
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)])
-> Map Coordinates NPieces
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall a b. (a -> b) -> a -> b
$ Coordinates -> Map Coordinates NPieces -> Map Coordinates NPieces
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Coordinates
enpassantCaptureCoordinates Map Coordinates NPieces
nDefendersByCoordinates,
\case
(Just Rank
_, Maybe Rank
_) -> (:) ((LogicalColour, Map Coordinates NPieces)
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)])
-> (Map Coordinates NPieces
-> (LogicalColour, Map Coordinates NPieces))
-> Map Coordinates NPieces
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) LogicalColour
opponentsLogicalColour (Map Coordinates NPieces
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)])
-> Map Coordinates NPieces
-> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall a b. (a -> b) -> a -> b
$ Coordinates -> Map Coordinates NPieces -> Map Coordinates NPieces
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Coordinates
destination Map Coordinates NPieces
nDefendersByCoordinates
(Maybe Rank, Maybe Rank)
_ -> [(LogicalColour, Map Coordinates NPieces)]
-> [(LogicalColour, Map Coordinates NPieces)]
forall a. a -> a
id
) MoveType
moveType
) [
LogicalColour -> LogicalColour
forall a. a -> a
id (LogicalColour -> LogicalColour)
-> (LogicalColour -> Map Coordinates NPieces)
-> LogicalColour
-> (LogicalColour, Map Coordinates NPieces)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Coordinates -> Map Coordinates NPieces -> Map Coordinates NPieces
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Coordinates
source (Map Coordinates NPieces -> Map Coordinates NPieces)
-> (LogicalColour -> Map Coordinates NPieces)
-> LogicalColour
-> Map Coordinates NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour NDefendersByCoordinatesByLogicalColour
-> LogicalColour -> Map Coordinates NPieces
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!) (LogicalColour -> (LogicalColour, Map Coordinates NPieces))
-> LogicalColour -> (LogicalColour, Map Coordinates NPieces)
forall a b. (a -> b) -> a -> b
$ LogicalColour
logicalColour
]
) ([LocatedPiece]
-> (Map Coordinates NPieces, Map Coordinates NPieces))
-> ([LocatedPiece] -> [LocatedPiece])
-> [LocatedPiece]
-> (Map Coordinates NPieces, Map Coordinates NPieces)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LocatedPiece -> LocatedPiece -> Bool)
-> [LocatedPiece] -> [LocatedPiece]
forall a. (a -> a -> Bool) -> [a] -> [a]
Data.List.nubBy (
\LocatedPiece
l LocatedPiece
r -> LocatedPiece -> Coordinates
forall a b. (a, b) -> a
fst LocatedPiece
l Coordinates -> Coordinates -> Bool
forall a. Eq a => a -> a -> Bool
== LocatedPiece -> Coordinates
forall a b. (a, b) -> a
fst LocatedPiece
r
) ([LocatedPiece] -> NDefendersByCoordinatesByLogicalColour)
-> [LocatedPiece] -> NDefendersByCoordinatesByLogicalColour
forall a b. (a -> b) -> a -> b
$ [
(Coordinates
affectedCoordinates, Piece
affectedPiece) |
(Coordinates
knightsCoordinates, Piece
knight) <- (Coordinates
source, Piece
sourcePiece) LocatedPiece -> [LocatedPiece] -> [LocatedPiece]
forall a. a -> [a] -> [a]
: (,) Coordinates
destination (Piece -> LocatedPiece) -> [Piece] -> [LocatedPiece]
forall a b. (a -> b) -> [a] -> [b]
`map` (
Piece
destinationPiece Piece -> [Piece] -> [Piece]
forall a. a -> [a] -> [a]
: Maybe Piece -> [Piece]
forall a. Maybe a -> [a]
Data.Maybe.maybeToList Maybe Piece
maybeExplicitlyTakenPiece
),
Piece -> Bool
Component.Piece.isKnight Piece
knight,
Coordinates
affectedCoordinates <- (Vector -> Maybe Coordinates) -> [Vector] -> [Coordinates]
forall a b. (a -> Maybe b) -> [a] -> [b]
Data.Maybe.mapMaybe (Vector -> Coordinates -> Maybe Coordinates
`Cartesian.Vector.maybeTranslate` Coordinates
knightsCoordinates) [Vector]
Cartesian.Vector.attackVectorsForKnight,
Piece
affectedPiece <- Maybe Piece -> [Piece]
forall a. Maybe a -> [a]
Data.Maybe.maybeToList (Maybe Piece -> [Piece]) -> Maybe Piece -> [Piece]
forall a b. (a -> b) -> a -> b
$! MaybePieceByCoordinates -> Coordinates -> Maybe Piece
State.MaybePieceByCoordinates.dereference MaybePieceByCoordinates
maybePieceByCoordinates' Coordinates
affectedCoordinates,
Piece -> Piece -> Bool
Component.Piece.isFriend Piece
knight Piece
affectedPiece
] [LocatedPiece] -> [LocatedPiece] -> [LocatedPiece]
forall a. [a] -> [a] -> [a]
++ [
(Coordinates
blockingCoordinates, Piece
blockingPiece) |
Coordinates
passingPawnsDestination <- [Coordinates
enpassantCaptureCoordinates | MoveType -> Bool
Attribute.MoveType.isEnPassant MoveType
moveType],
(Direction
direction, Direction
antiParallelDirection) <- [(Direction, Direction)]
Direction.Direction.opposites,
(Coordinates
blockingCoordinates, Piece
blockingPiece) <- case MaybePieceByCoordinates
-> Coordinates -> Maybe [Direction] -> [LocatedPiece]
State.MaybePieceByCoordinates.findBlockingPieces MaybePieceByCoordinates
maybePieceByCoordinates' Coordinates
passingPawnsDestination (Maybe [Direction] -> [LocatedPiece])
-> Maybe [Direction] -> [LocatedPiece]
forall a b. (a -> b) -> a -> b
$ [Direction] -> Maybe [Direction]
forall a. a -> Maybe a
Just [Direction
direction, Direction
antiParallelDirection] of
[LocatedPiece
cp, LocatedPiece
cp'] -> [
LocatedPiece
cp | (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(||) ((Bool, Bool) -> Bool)
-> ((LocatedPiece -> Bool) -> (Bool, Bool))
-> (LocatedPiece -> Bool)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
((LocatedPiece -> Bool) -> LocatedPiece -> Bool
forall a b. (a -> b) -> a -> b
$ (Coordinates
passingPawnsDestination, Piece
oppositePiece)) ((LocatedPiece -> Bool) -> Bool)
-> ((LocatedPiece -> Bool) -> Bool)
-> (LocatedPiece -> Bool)
-> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((LocatedPiece -> Bool) -> LocatedPiece -> Bool
forall a b. (a -> b) -> a -> b
$ LocatedPiece
cp')
) ((LocatedPiece -> Bool) -> Bool) -> (LocatedPiece -> Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ \(Coordinates
from, Piece
piece) -> (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(&&) ((Bool, Bool) -> Bool) -> (Bool, Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ ((Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool))
-> (Piece -> Bool, Piece -> Bool) -> Piece -> (Bool, Bool)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
(&&&) (
Coordinates -> Coordinates -> Piece -> Bool
Component.Piece.canAttackAlong Coordinates
from (Coordinates -> Piece -> Bool)
-> (Piece -> Piece -> Bool)
-> LocatedPiece
-> (Piece -> Bool, Piece -> Bool)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Piece -> Piece -> Bool
Component.Piece.isFriend (LocatedPiece -> (Piece -> Bool, Piece -> Bool))
-> LocatedPiece -> (Piece -> Bool, Piece -> Bool)
forall a b. (a -> b) -> a -> b
$ LocatedPiece
cp
) Piece
piece
] [LocatedPiece] -> [LocatedPiece] -> [LocatedPiece]
forall a. [a] -> [a] -> [a]
++ [
LocatedPiece
cp' | (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(||) ((Bool, Bool) -> Bool)
-> ((LocatedPiece -> Bool) -> (Bool, Bool))
-> (LocatedPiece -> Bool)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
((LocatedPiece -> Bool) -> LocatedPiece -> Bool
forall a b. (a -> b) -> a -> b
$ (Coordinates
passingPawnsDestination, Piece
oppositePiece)) ((LocatedPiece -> Bool) -> Bool)
-> ((LocatedPiece -> Bool) -> Bool)
-> (LocatedPiece -> Bool)
-> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((LocatedPiece -> Bool) -> LocatedPiece -> Bool
forall a b. (a -> b) -> a -> b
$ LocatedPiece
cp)
) ((LocatedPiece -> Bool) -> Bool) -> (LocatedPiece -> Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ \(Coordinates
from, Piece
piece) -> (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(&&) ((Bool, Bool) -> Bool) -> (Bool, Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ ((Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool))
-> (Piece -> Bool, Piece -> Bool) -> Piece -> (Bool, Bool)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
(&&&) (
Coordinates -> Coordinates -> Piece -> Bool
Component.Piece.canAttackAlong Coordinates
from (Coordinates -> Piece -> Bool)
-> (Piece -> Piece -> Bool)
-> LocatedPiece
-> (Piece -> Bool, Piece -> Bool)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Piece -> Piece -> Bool
Component.Piece.isFriend (LocatedPiece -> (Piece -> Bool, Piece -> Bool))
-> LocatedPiece -> (Piece -> Bool, Piece -> Bool)
forall a b. (a -> b) -> a -> b
$ LocatedPiece
cp'
) Piece
piece
]
[LocatedPiece]
locatedPieces -> (LocatedPiece -> Bool) -> [LocatedPiece] -> [LocatedPiece]
forall a. (a -> Bool) -> [a] -> [a]
filter (
\LocatedPiece
locatedPiece -> (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(&&) ((Bool, Bool) -> Bool) -> (Bool, Bool) -> Bool
forall a b. (a -> b) -> a -> b
$ ((Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool))
-> (Piece -> Bool, Piece -> Bool) -> Piece -> (Bool, Bool)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
(&&&) (
Coordinates -> Coordinates -> Piece -> Bool
Component.Piece.canAttackAlong Coordinates
passingPawnsDestination (Coordinates -> Piece -> Bool)
-> (Piece -> Piece -> Bool)
-> LocatedPiece
-> (Piece -> Bool, Piece -> Bool)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Piece -> Piece -> Bool
Component.Piece.isFriend (LocatedPiece -> (Piece -> Bool, Piece -> Bool))
-> LocatedPiece -> (Piece -> Bool, Piece -> Bool)
forall a b. (a -> b) -> a -> b
$ LocatedPiece
locatedPiece
) Piece
oppositePiece
) [LocatedPiece]
locatedPieces
] [LocatedPiece] -> [LocatedPiece] -> [LocatedPiece]
forall a. [a] -> [a] -> [a]
++ (Coordinates
destination, Piece
destinationPiece) LocatedPiece -> [LocatedPiece] -> [LocatedPiece]
forall a. a -> [a] -> [a]
: [
(Coordinates
blockingCoordinates, Piece
blockingPiece) |
(Direction
direction, Direction
antiParallelDirection) <- [(Direction, Direction)]
Direction.Direction.opposites,
(Coordinates
coordinates, Piece
piece) <- [(Coordinates
source, Piece
sourcePiece), (Coordinates
destination, Piece
destinationPiece)],
(Coordinates
blockingCoordinates, Piece
blockingPiece) <- case MaybePieceByCoordinates
-> Coordinates -> Maybe [Direction] -> [LocatedPiece]
State.MaybePieceByCoordinates.findBlockingPieces MaybePieceByCoordinates
maybePieceByCoordinates' Coordinates
coordinates (Maybe [Direction] -> [LocatedPiece])
-> Maybe [Direction] -> [LocatedPiece]
forall a b. (a -> b) -> a -> b
$ [Direction] -> Maybe [Direction]
forall a. a -> Maybe a
Just [Direction
direction, Direction
antiParallelDirection] of
[LocatedPiece
cp, LocatedPiece
cp'] -> [
LocatedPiece
cp |
let isDefendedBy :: Coordinates -> Piece -> Bool
isDefendedBy Coordinates
from = (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(&&) ((Bool, Bool) -> Bool) -> (Piece -> (Bool, Bool)) -> Piece -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool))
-> (Piece -> Bool, Piece -> Bool) -> Piece -> (Bool, Bool)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
(&&&) (Coordinates -> Coordinates -> Piece -> Bool
Component.Piece.canAttackAlong Coordinates
from (Coordinates -> Piece -> Bool)
-> (Piece -> Piece -> Bool)
-> LocatedPiece
-> (Piece -> Bool, Piece -> Bool)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Piece -> Piece -> Bool
Component.Piece.isFriend (LocatedPiece -> (Piece -> Bool, Piece -> Bool))
-> LocatedPiece -> (Piece -> Bool, Piece -> Bool)
forall a b. (a -> b) -> a -> b
$ LocatedPiece
cp),
Coordinates -> Piece -> Bool
isDefendedBy Coordinates
coordinates Piece
piece Bool -> Bool -> Bool
|| Coordinates
coordinates Coordinates -> Coordinates -> Bool
forall a. Eq a => a -> a -> Bool
== Coordinates
destination Bool -> Bool -> Bool
&& Bool -> (Piece -> Bool) -> Maybe Piece -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe Bool
False (Coordinates -> Piece -> Bool
isDefendedBy Coordinates
destination) Maybe Piece
maybeExplicitlyTakenPiece Bool -> Bool -> Bool
|| (Coordinates -> Piece -> Bool) -> LocatedPiece -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Coordinates -> Piece -> Bool
isDefendedBy LocatedPiece
cp'
] [LocatedPiece] -> [LocatedPiece] -> [LocatedPiece]
forall a. [a] -> [a] -> [a]
++ [
LocatedPiece
cp' |
let isDefendedBy :: Coordinates -> Piece -> Bool
isDefendedBy Coordinates
from = (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(&&) ((Bool, Bool) -> Bool) -> (Piece -> (Bool, Bool)) -> Piece -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool))
-> (Piece -> Bool, Piece -> Bool) -> Piece -> (Bool, Bool)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
(&&&) (Coordinates -> Coordinates -> Piece -> Bool
Component.Piece.canAttackAlong Coordinates
from (Coordinates -> Piece -> Bool)
-> (Piece -> Piece -> Bool)
-> LocatedPiece
-> (Piece -> Bool, Piece -> Bool)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Piece -> Piece -> Bool
Component.Piece.isFriend (LocatedPiece -> (Piece -> Bool, Piece -> Bool))
-> LocatedPiece -> (Piece -> Bool, Piece -> Bool)
forall a b. (a -> b) -> a -> b
$ LocatedPiece
cp'),
Coordinates -> Piece -> Bool
isDefendedBy Coordinates
coordinates Piece
piece Bool -> Bool -> Bool
|| Coordinates
coordinates Coordinates -> Coordinates -> Bool
forall a. Eq a => a -> a -> Bool
== Coordinates
destination Bool -> Bool -> Bool
&& Bool -> (Piece -> Bool) -> Maybe Piece -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe Bool
False (Coordinates -> Piece -> Bool
isDefendedBy Coordinates
destination) Maybe Piece
maybeExplicitlyTakenPiece Bool -> Bool -> Bool
|| (Coordinates -> Piece -> Bool) -> LocatedPiece -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Coordinates -> Piece -> Bool
isDefendedBy LocatedPiece
cp
]
[LocatedPiece]
locatedPieces -> (LocatedPiece -> Bool) -> [LocatedPiece] -> [LocatedPiece]
forall a. (a -> Bool) -> [a] -> [a]
filter (
\LocatedPiece
locatedPiece -> let
isDefendedBy :: Piece -> Bool
isDefendedBy = (Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(&&) ((Bool, Bool) -> Bool) -> (Piece -> (Bool, Bool)) -> Piece -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool))
-> (Piece -> Bool, Piece -> Bool) -> Piece -> (Bool, Bool)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Piece -> Bool) -> (Piece -> Bool) -> Piece -> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
(&&&) (Coordinates -> Coordinates -> Piece -> Bool
Component.Piece.canAttackAlong Coordinates
coordinates (Coordinates -> Piece -> Bool)
-> (Piece -> Piece -> Bool)
-> LocatedPiece
-> (Piece -> Bool, Piece -> Bool)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Piece -> Piece -> Bool
Component.Piece.isFriend (LocatedPiece -> (Piece -> Bool, Piece -> Bool))
-> LocatedPiece -> (Piece -> Bool, Piece -> Bool)
forall a b. (a -> b) -> a -> b
$ LocatedPiece
locatedPiece)
in Piece -> Bool
isDefendedBy Piece
piece Bool -> Bool -> Bool
|| Coordinates
coordinates Coordinates -> Coordinates -> Bool
forall a. Eq a => a -> a -> Bool
== Coordinates
destination Bool -> Bool -> Bool
&& Bool -> (Piece -> Bool) -> Maybe Piece -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe Bool
False Piece -> Bool
isDefendedBy Maybe Piece
maybeExplicitlyTakenPiece
) [LocatedPiece]
locatedPieces
],
getNPiecesDifferenceByRank :: NPiecesByRank
getNPiecesDifferenceByRank = (NPieces -> NPieces -> NPieces)
-> NPiecesByRank -> [(Rank, NPieces)] -> NPiecesByRank
forall (a :: * -> * -> *) e i e'.
(IArray a e, Ix i) =>
(e -> e' -> e) -> a i e -> [(i, e')] -> a i e
Data.Array.IArray.accum (
if LogicalColour -> Bool
Colour.LogicalColour.isBlack LogicalColour
logicalColour
then (-)
else NPieces -> NPieces -> NPieces
forall a. Num a => a -> a -> a
(+)
) NPiecesByRank
nPiecesDifferenceByRank ([(Rank, NPieces)] -> NPiecesByRank)
-> [(Rank, NPieces)] -> NPiecesByRank
forall a b. (a -> b) -> a -> b
$ (Bool -> [(Rank, NPieces)], [(Rank, NPieces)],
(Maybe Rank, Maybe Rank) -> [(Rank, NPieces)])
-> MoveType -> [(Rank, NPieces)]
forall a.
(Bool -> a, a, (Maybe Rank, Maybe Rank) -> a) -> MoveType -> a
Attribute.MoveType.apply (
[(Rank, NPieces)] -> Bool -> [(Rank, NPieces)]
forall a b. a -> b -> a
const [],
[(Rank
Attribute.Rank.Pawn, NPieces
1)],
(([(Rank, NPieces)] -> [(Rank, NPieces)])
-> [(Rank, NPieces)] -> [(Rank, NPieces)])
-> ([(Rank, NPieces)] -> [(Rank, NPieces)], [(Rank, NPieces)])
-> [(Rank, NPieces)]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ([(Rank, NPieces)] -> [(Rank, NPieces)])
-> [(Rank, NPieces)] -> [(Rank, NPieces)]
forall a b. (a -> b) -> a -> b
($) (([(Rank, NPieces)] -> [(Rank, NPieces)], [(Rank, NPieces)])
-> [(Rank, NPieces)])
-> ((Maybe Rank, Maybe Rank)
-> ([(Rank, NPieces)] -> [(Rank, NPieces)], [(Rank, NPieces)]))
-> (Maybe Rank, Maybe Rank)
-> [(Rank, NPieces)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
([(Rank, NPieces)] -> [(Rank, NPieces)])
-> (Rank -> [(Rank, NPieces)] -> [(Rank, NPieces)])
-> Maybe Rank
-> [(Rank, NPieces)]
-> [(Rank, NPieces)]
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe [(Rank, NPieces)] -> [(Rank, NPieces)]
forall a. a -> a
id (
(:) ((Rank, NPieces) -> [(Rank, NPieces)] -> [(Rank, NPieces)])
-> (Rank -> (Rank, NPieces))
-> Rank
-> [(Rank, NPieces)]
-> [(Rank, NPieces)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Rank -> NPieces -> (Rank, NPieces))
-> NPieces -> Rank -> (Rank, NPieces)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (,) NPieces
1
) (Maybe Rank -> [(Rank, NPieces)] -> [(Rank, NPieces)])
-> (Maybe Rank -> [(Rank, NPieces)])
-> (Maybe Rank, Maybe Rank)
-> ([(Rank, NPieces)] -> [(Rank, NPieces)], [(Rank, NPieces)])
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** [(Rank, NPieces)]
-> (Rank -> [(Rank, NPieces)]) -> Maybe Rank -> [(Rank, NPieces)]
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe [] (
\Rank
promotionRank -> [
(
Rank
promotionRank,
NPieces
1
), (
Rank
Attribute.Rank.Pawn,
NPieces -> NPieces
forall a. Num a => a -> a
negate NPieces
1
)
]
)
)
) MoveType
moveType,
getNPawnsByFileByLogicalColour :: NPiecesByFileByLogicalColour
getNPawnsByFileByLogicalColour = if Piece -> Bool
Component.Piece.isPawn Piece
sourcePiece Bool -> Bool -> Bool
&& Bool -> Bool
not (MoveType -> Bool
Attribute.MoveType.isQuiet MoveType
moveType) Bool -> Bool -> Bool
|| Bool
wasPawnTakenExplicitly
then CoordinatesByRankByLogicalColour -> NPiecesByFileByLogicalColour
forall seeker.
Seeker seeker =>
seeker -> NPiecesByFileByLogicalColour
StateProperty.Seeker.countPawnsByFileByLogicalColour CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour'
else Board -> NPiecesByFileByLogicalColour
getNPawnsByFileByLogicalColour Board
board,
getNPieces :: NPieces
getNPieces = MoveType -> NPieces -> NPieces
forall nPieces. Enum nPieces => MoveType -> nPieces -> nPieces
Attribute.MoveType.nPiecesMutator MoveType
moveType NPieces
nPieces,
getPassedPawnCoordinatesByLogicalColour :: CoordinatesByLogicalColour
getPassedPawnCoordinatesByLogicalColour = if Piece -> Bool
Component.Piece.isPawn Piece
sourcePiece Bool -> Bool -> Bool
|| Bool
wasPawnTakenExplicitly
then CoordinatesByRankByLogicalColour -> CoordinatesByLogicalColour
State.CoordinatesByRankByLogicalColour.findPassedPawnCoordinatesByLogicalColour CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour'
else Board -> CoordinatesByLogicalColour
getPassedPawnCoordinatesByLogicalColour Board
board
}
coordinatesByRankByLogicalColour' :: CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour' = Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour Board
board'
in Board
board'
| Bool
otherwise = Exception -> Board
forall a e. Exception e => e -> a
Control.Exception.throw (Exception -> Board) -> (String -> Exception) -> String -> Board
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Exception
Data.Exception.mkSearchFailure (String -> Exception) -> ShowS -> String -> Exception
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"BishBosh.State.Board.movePiece:\tno piece exists at " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coordinates -> ShowS
forall a. Show a => a -> ShowS
shows Coordinates
source ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"; " (String -> Board) -> String -> Board
forall a b. (a -> b) -> a -> b
$ Board -> ShowS
forall a. Show a => a -> ShowS
shows Board
board String
"."
where
(Coordinates
source, Coordinates
destination) = Move -> Coordinates
Component.Move.getSource (Move -> Coordinates)
-> (Move -> Coordinates) -> Move -> (Coordinates, Coordinates)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Move -> Coordinates
Component.Move.getDestination (Move -> (Coordinates, Coordinates))
-> Move -> (Coordinates, Coordinates)
forall a b. (a -> b) -> a -> b
$! Move
move
sumPieceSquareValueByLogicalColour
:: Component.PieceSquareValueByCoordinatesByRank.PieceSquareValueByCoordinatesByRank
-> Board
-> [Type.Mass.Base]
sumPieceSquareValueByLogicalColour :: PieceSquareValueByCoordinatesByRank -> Board -> [Base]
sumPieceSquareValueByLogicalColour PieceSquareValueByCoordinatesByRank
pieceSquareValueByCoordinatesByRank MkBoard {
getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour,
getNPieces :: Board -> NPieces
getNPieces = NPieces
nPieces
} = PieceSquareValueByCoordinatesByRank
-> CoordinatesByRankByLogicalColour -> NPieces -> [Base]
forall accountant.
Accountant accountant =>
PieceSquareValueByCoordinatesByRank
-> accountant -> NPieces -> [Base]
Component.Accountant.sumPieceSquareValueByLogicalColour PieceSquareValueByCoordinatesByRank
pieceSquareValueByCoordinatesByRank CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour NPieces
nPieces
findAttackersOf
:: Board
-> Colour.LogicalColour.LogicalColour
-> Cartesian.Coordinates.Coordinates
-> [(Cartesian.Coordinates.Coordinates, Attribute.Rank.Rank)]
findAttackersOf :: Board -> LogicalColour -> Coordinates -> [(Coordinates, Rank)]
findAttackersOf board :: Board
board@MkBoard { getMaybePieceByCoordinates :: Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates = MaybePieceByCoordinates
maybePieceByCoordinates } LogicalColour
destinationLogicalColour Coordinates
destination = [
(Coordinates
coordinates, Rank
Attribute.Rank.Knight) |
Coordinates
coordinates <- Board -> LogicalColour -> Coordinates -> [Coordinates]
forall seeker.
Seeker seeker =>
seeker -> LogicalColour -> Coordinates -> [Coordinates]
StateProperty.Seeker.findProximateKnights Board
board (LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite LogicalColour
destinationLogicalColour) Coordinates
destination
] [(Coordinates, Rank)]
-> [(Coordinates, Rank)] -> [(Coordinates, Rank)]
forall a. [a] -> [a] -> [a]
++ MaybePieceByCoordinates
-> LogicalColour
-> Coordinates
-> Maybe [Direction]
-> [(Coordinates, Rank)]
State.MaybePieceByCoordinates.findAttackerInDirections MaybePieceByCoordinates
maybePieceByCoordinates LogicalColour
destinationLogicalColour Coordinates
destination Maybe [Direction]
forall a. Maybe a
Nothing
findAttacksBy
:: Board
-> Component.Piece.Piece
-> Cartesian.Coordinates.Coordinates
-> [Cartesian.Coordinates.Coordinates]
findAttacksBy :: Board -> Piece -> Coordinates -> [Coordinates]
findAttacksBy Board
board Piece
piece Coordinates
destination
| Rank
rank Rank -> Rank -> Bool
forall a. Eq a => a -> a -> Bool
== Rank
Attribute.Rank.Knight = Board -> LogicalColour -> Coordinates -> [Coordinates]
forall seeker.
Seeker seeker =>
seeker -> LogicalColour -> Coordinates -> [Coordinates]
StateProperty.Seeker.findProximateKnights Board
board LogicalColour
logicalColour Coordinates
destination
| Bool
otherwise = (Coordinates -> Bool) -> [Coordinates] -> [Coordinates]
forall a. (a -> Bool) -> [a] -> [a]
filter (
\Coordinates
source -> Coordinates
source Coordinates -> Coordinates -> Bool
forall a. Eq a => a -> a -> Bool
/= Coordinates
destination Bool -> Bool -> Bool
&& Coordinates -> Coordinates -> Piece -> Bool
Component.Piece.canAttackAlong Coordinates
source Coordinates
destination Piece
piece Bool -> Bool -> Bool
&& MaybePieceByCoordinates -> Coordinates -> Coordinates -> Bool
State.MaybePieceByCoordinates.isClear (Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates Board
board) Coordinates
source Coordinates
destination
) ([Coordinates] -> [Coordinates]) -> [Coordinates] -> [Coordinates]
forall a b. (a -> b) -> a -> b
$ CoordinatesByRankByLogicalColour
-> LogicalColour -> Rank -> [Coordinates]
State.CoordinatesByRankByLogicalColour.dereference (Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour Board
board) LogicalColour
logicalColour Rank
rank
where
(LogicalColour
logicalColour, Rank
rank) = Piece -> LogicalColour
Component.Piece.getLogicalColour (Piece -> LogicalColour)
-> (Piece -> Rank) -> Piece -> (LogicalColour, Rank)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Piece -> Rank
Component.Piece.getRank (Piece -> (LogicalColour, Rank)) -> Piece -> (LogicalColour, Rank)
forall a b. (a -> b) -> a -> b
$ Piece
piece
isKingChecked
:: Board
-> Colour.LogicalColour.LogicalColour
-> Bool
isKingChecked :: Board -> LogicalColour -> Bool
isKingChecked board :: Board
board@MkBoard { getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour } = Bool -> Bool
not (Bool -> Bool) -> (LogicalColour -> Bool) -> LogicalColour -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Coordinates, Rank)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([(Coordinates, Rank)] -> Bool)
-> (LogicalColour -> [(Coordinates, Rank)])
-> LogicalColour
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Coordinates -> [(Coordinates, Rank)])
-> Coordinates -> [(Coordinates, Rank)])
-> (Coordinates -> [(Coordinates, Rank)], Coordinates)
-> [(Coordinates, Rank)]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Coordinates -> [(Coordinates, Rank)])
-> Coordinates -> [(Coordinates, Rank)]
forall a b. (a -> b) -> a -> b
($!) ((Coordinates -> [(Coordinates, Rank)], Coordinates)
-> [(Coordinates, Rank)])
-> (LogicalColour
-> (Coordinates -> [(Coordinates, Rank)], Coordinates))
-> LogicalColour
-> [(Coordinates, Rank)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
Board -> LogicalColour -> Coordinates -> [(Coordinates, Rank)]
findAttackersOf Board
board (LogicalColour -> Coordinates -> [(Coordinates, Rank)])
-> (LogicalColour -> Coordinates)
-> LogicalColour
-> (Coordinates -> [(Coordinates, Rank)], Coordinates)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& CoordinatesByRankByLogicalColour -> LogicalColour -> Coordinates
State.CoordinatesByRankByLogicalColour.getKingsCoordinates CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour
)
passesThroughCheck
:: Board
-> Colour.LogicalColour.LogicalColour
-> Component.Move.Move
-> Bool
passesThroughCheck :: Board -> LogicalColour -> Move -> Bool
passesThroughCheck Board
board LogicalColour
logicalColour = Bool -> Bool
not (Bool -> Bool) -> (Move -> Bool) -> Move -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Coordinates, NPieces) -> Bool)
-> [(Coordinates, NPieces)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (
[(Coordinates, Rank)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([(Coordinates, Rank)] -> Bool)
-> ((Coordinates, NPieces) -> [(Coordinates, Rank)])
-> (Coordinates, NPieces)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Board -> LogicalColour -> Coordinates -> [(Coordinates, Rank)]
findAttackersOf Board
board LogicalColour
logicalColour (Coordinates -> [(Coordinates, Rank)])
-> ((Coordinates, NPieces) -> Coordinates)
-> (Coordinates, NPieces)
-> [(Coordinates, Rank)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Coordinates, NPieces) -> Coordinates
forall a b. (a, b) -> a
fst
) ([(Coordinates, NPieces)] -> Bool)
-> (Move -> [(Coordinates, NPieces)]) -> Move -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Move -> [(Coordinates, NPieces)]
Component.Move.interpolate
exposesKing
:: Board
-> Colour.LogicalColour.LogicalColour
-> Component.Move.Move
-> Bool
exposesKing :: Board -> LogicalColour -> Move -> Bool
exposesKing board :: Board
board@MkBoard { getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour } LogicalColour
logicalColour Move
move
| Coordinates
source Coordinates -> Coordinates -> Bool
forall a. Eq a => a -> a -> Bool
== Coordinates
kingsCoordinates = Bool -> Bool
not (Bool -> Bool) -> (Coordinates -> Bool) -> Coordinates -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Coordinates, Rank)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([(Coordinates, Rank)] -> Bool)
-> (Coordinates -> [(Coordinates, Rank)]) -> Coordinates -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Board -> LogicalColour -> Coordinates -> [(Coordinates, Rank)]
findAttackersOf Board
board LogicalColour
logicalColour (Coordinates -> Bool) -> Coordinates -> Bool
forall a b. (a -> b) -> a -> b
$! Move -> Coordinates
Component.Move.getDestination Move
move
| Just Direction
directionFromKing <- Vector -> Maybe Direction
Cartesian.Vector.toMaybeDirection (Vector -> Maybe Direction) -> Vector -> Maybe Direction
forall a b. (a -> b) -> a -> b
$! Coordinates -> Coordinates -> Vector
Cartesian.Vector.measureDistance Coordinates
kingsCoordinates Coordinates
source
, let maybePieceByCoordinates :: MaybePieceByCoordinates
maybePieceByCoordinates = Board -> MaybePieceByCoordinates
getMaybePieceByCoordinates Board
board
, MaybePieceByCoordinates -> Coordinates -> Coordinates -> Bool
State.MaybePieceByCoordinates.isClear MaybePieceByCoordinates
maybePieceByCoordinates Coordinates
kingsCoordinates Coordinates
source
, Bool -> (Direction -> Bool) -> Maybe Direction -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe Bool
True (
Bool -> Bool
not (Bool -> Bool) -> (Direction -> Bool) -> Direction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Direction -> Direction -> Bool
Direction.Direction.areAligned Direction
directionFromKing
) (Maybe Direction -> Bool)
-> (Vector -> Maybe Direction) -> Vector -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector -> Maybe Direction
Cartesian.Vector.toMaybeDirection (Vector -> Bool) -> Vector -> Bool
forall a b. (a -> b) -> a -> b
$! Move -> Vector
Component.Move.measureDistance Move
move
, Just (Coordinates
_, Rank
attackersRank) <- MaybePieceByCoordinates
-> LogicalColour
-> Coordinates
-> Direction
-> Maybe (Coordinates, Rank)
State.MaybePieceByCoordinates.findAttackerInDirection MaybePieceByCoordinates
maybePieceByCoordinates LogicalColour
logicalColour Coordinates
source Direction
directionFromKing
= Rank
attackersRank Rank -> [Rank] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Rank]
Attribute.Rank.plodders
| Bool
otherwise = Bool
False
where
source :: Coordinates
source = Move -> Coordinates
Component.Move.getSource Move
move
kingsCoordinates :: Coordinates
kingsCoordinates = CoordinatesByRankByLogicalColour -> LogicalColour -> Coordinates
State.CoordinatesByRankByLogicalColour.getKingsCoordinates CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour LogicalColour
logicalColour
countDefendersByCoordinatesByLogicalColour :: Board -> NDefendersByCoordinatesByLogicalColour
countDefendersByCoordinatesByLogicalColour :: Board -> NDefendersByCoordinatesByLogicalColour
countDefendersByCoordinatesByLogicalColour board :: Board
board@MkBoard { getCoordinatesByRankByLogicalColour :: Board -> CoordinatesByRankByLogicalColour
getCoordinatesByRankByLogicalColour = CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour } = [Map Coordinates NPieces] -> NDefendersByCoordinatesByLogicalColour
forall (a :: * -> * -> *) e. IArray a e => [e] -> a LogicalColour e
Colour.LogicalColour.listArrayByLogicalColour [
[(Coordinates, NPieces)] -> Map Coordinates NPieces
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [
(
Coordinates
coordinates,
NPieces -> NPieces
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NPieces -> NPieces)
-> ([(Coordinates, Rank)] -> NPieces)
-> [(Coordinates, Rank)]
-> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Coordinates, Rank)] -> NPieces
forall (t :: * -> *) a. Foldable t => t a -> NPieces
length ([(Coordinates, Rank)] -> NPieces)
-> [(Coordinates, Rank)] -> NPieces
forall a b. (a -> b) -> a -> b
$ Board -> LogicalColour -> Coordinates -> [(Coordinates, Rank)]
findAttackersOf Board
board (
LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite LogicalColour
logicalColour
) Coordinates
coordinates
) |
Rank
rank <- [Rank]
Attribute.Rank.expendable,
Coordinates
coordinates <- CoordinatesByRankByLogicalColour
-> LogicalColour -> Rank -> [Coordinates]
State.CoordinatesByRankByLogicalColour.dereference CoordinatesByRankByLogicalColour
coordinatesByRankByLogicalColour LogicalColour
logicalColour Rank
rank
] | LogicalColour
logicalColour <- [LogicalColour]
forall a. FixedMembership a => [a]
Property.FixedMembership.members
]
summariseNDefendersByLogicalColour :: Board -> Colour.LogicalColour.ArrayByLogicalColour Type.Count.NPieces
summariseNDefendersByLogicalColour :: Board -> ArrayByLogicalColour NPieces
summariseNDefendersByLogicalColour MkBoard { getNDefendersByCoordinatesByLogicalColour :: Board -> NDefendersByCoordinatesByLogicalColour
getNDefendersByCoordinatesByLogicalColour = NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour } = (Map Coordinates NPieces -> NPieces)
-> NDefendersByCoordinatesByLogicalColour
-> ArrayByLogicalColour NPieces
forall (a :: * -> * -> *) e' e i.
(IArray a e', IArray a e, Ix i) =>
(e' -> e) -> a i e' -> a i e
Data.Array.IArray.amap (
(NPieces -> NPieces -> NPieces)
-> NPieces -> Map Coordinates NPieces -> NPieces
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.Foldable.foldl' NPieces -> NPieces -> NPieces
forall a. Num a => a -> a -> a
(+) NPieces
0
) NDefendersByCoordinatesByLogicalColour
nDefendersByCoordinatesByLogicalColour