module BishBosh.Model.GameTerminationReason(
GameTerminationReason(),
toResult,
mkCheckMate,
mkResignation,
mkDraw,
isCheckMateBy,
isCheckMate,
isResignation,
isDraw,
isDrawByInsufficientMaterial,
isStaleMate
) where
import qualified BishBosh.Attribute.LogicalColour as Attribute.LogicalColour
import qualified BishBosh.Model.DrawReason as Model.DrawReason
import qualified BishBosh.Model.Result as Model.Result
import qualified BishBosh.Property.Opposable as Property.Opposable
import qualified Control.DeepSeq
data GameTerminationReason
= CheckMateOf Attribute.LogicalColour.LogicalColour
| ResignationBy Attribute.LogicalColour.LogicalColour
| Draw Model.DrawReason.DrawReason
deriving (Eq, Read, Show)
instance Control.DeepSeq.NFData GameTerminationReason where
rnf (CheckMateOf logicalColour) = Control.DeepSeq.rnf logicalColour
rnf (ResignationBy logicalColour) = Control.DeepSeq.rnf logicalColour
rnf (Draw drawReason) = Control.DeepSeq.rnf drawReason
instance Property.Opposable.Opposable GameTerminationReason where
getOpposite (CheckMateOf logicalColour) = CheckMateOf $ Property.Opposable.getOpposite logicalColour
getOpposite (ResignationBy logicalColour) = ResignationBy $ Property.Opposable.getOpposite logicalColour
getOpposite draw = draw
toResult :: GameTerminationReason -> Model.Result.Result
toResult gameTerminationReason = Property.Opposable.getOpposite . Model.Result.mkResult $ case gameTerminationReason of
CheckMateOf logicalColour -> Just logicalColour
ResignationBy logicalColour -> Just logicalColour
Draw _ -> Nothing
mkCheckMate :: Attribute.LogicalColour.LogicalColour -> GameTerminationReason
mkCheckMate = CheckMateOf
mkResignation :: Attribute.LogicalColour.LogicalColour -> GameTerminationReason
mkResignation = ResignationBy
mkDraw :: Model.DrawReason.DrawReason -> GameTerminationReason
mkDraw = Draw
isCheckMateBy :: Attribute.LogicalColour.LogicalColour -> GameTerminationReason -> Bool
isCheckMateBy logicalColour (CheckMateOf logicalColour') = logicalColour /= logicalColour'
isCheckMateBy _ _ = False
isCheckMate :: GameTerminationReason -> Bool
isCheckMate (CheckMateOf _) = True
isCheckMate _ = False
isResignation :: GameTerminationReason -> Bool
isResignation (ResignationBy _) = True
isResignation _ = False
isDraw :: GameTerminationReason -> Bool
isDraw (Draw _) = True
isDraw _ = False
isDrawByInsufficientMaterial :: GameTerminationReason -> Bool
isDrawByInsufficientMaterial (Draw draw) = draw == Model.DrawReason.insufficientMaterial
isDrawByInsufficientMaterial _ = False
isStaleMate :: GameTerminationReason -> Bool
isStaleMate (Draw draw) = draw == Model.DrawReason.staleMate
isStaleMate _ = False