{-# LANGUAGE CPP #-}
{-
	Copyright (C) 2018 Dr. Alistair Ward

	This file is part of BishBosh.

	BishBosh is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	BishBosh is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Maintains a census of the /piece/s on the board, without regard to their location.
-}

module BishBosh.StateProperty.Censor(
-- * Types
-- ** Type-synonyms
	NPiecesByRank,
-- * Type-classes
	Censor(..),
-- * Functions
	findInvalidity
) where

import			Control.Arrow((&&&), (***))
import qualified	BishBosh.Attribute.Rank			as Attribute.Rank
import qualified	BishBosh.Component.Piece		as Component.Piece
import qualified	BishBosh.Property.SelfValidating	as Property.SelfValidating
import qualified	BishBosh.Type.Count			as Type.Count

-- | The difference in the number of /piece/s of each /rank/ held by either side.
type NPiecesByRank	=
#ifdef UNBOX_TYPECOUNT_ARRAYS
	Attribute.Rank.UArrayByRank
#else
	Attribute.Rank.ArrayByRank
#endif
		Type.Count.NPieces

-- | An interface which may be implemented by data which can perform a census of the /piece/s on the /board/.
class Censor censor where
	countPiecesByLogicalColour	:: censor -> (Type.Count.NPieces, Type.Count.NPieces)	-- ^ The total number of /piece/s, partitioned into @Black@ & @White@ respectively.

	countPieces			:: censor -> Type.Count.NPieces				-- ^ The total number of /piece/s on the board, regardless of logical colour.
	countPieces	= (NPieces -> NPieces -> NPieces) -> (NPieces, NPieces) -> NPieces
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry NPieces -> NPieces -> NPieces
forall a. Num a => a -> a -> a
(+) ((NPieces, NPieces) -> NPieces)
-> (censor -> (NPieces, NPieces)) -> censor -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. censor -> (NPieces, NPieces)
forall censor. Censor censor => censor -> (NPieces, NPieces)
countPiecesByLogicalColour	-- Default implementation.

	countPieceDifferenceByRank	:: censor -> NPiecesByRank				-- ^ Finds the difference between the number of /piece/s of each /rank/ held by each side. N.B. for this purpose, @White@ is arbitrarily considered positive & @Black@ negative.

	hasInsufficientMaterial		:: censor -> Bool					-- ^ Whether insufficient material remains on the board, to force check-mate; <https://en.wikipedia.org/wiki/Draw_(chess)>.

	hasBothKings			:: censor -> Bool					-- ^ Whether there's exactly one @King@ of each /logical colour/.

-- | Self-validate.
findInvalidity :: Censor censor => censor -> [String]
findInvalidity :: censor -> [String]
findInvalidity	= [(censor -> Bool, String)] -> censor -> [String]
forall selfValidator.
[(selfValidator -> Bool, String)] -> selfValidator -> [String]
Property.SelfValidating.findErrors [
	(
		Bool -> Bool
not (Bool -> Bool) -> (censor -> Bool) -> censor -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. censor -> Bool
forall censor. Censor censor => censor -> Bool
hasBothKings,
		String
"there must be exactly one King of each logical colour."
	), (
		(Bool -> Bool -> Bool) -> (Bool, Bool) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> Bool -> Bool
(||) ((Bool, Bool) -> Bool)
-> (censor -> (Bool, Bool)) -> censor -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((NPieces -> Bool)
 -> (NPieces -> Bool) -> (NPieces, NPieces) -> (Bool, Bool))
-> (NPieces -> Bool, NPieces -> Bool)
-> (NPieces, NPieces)
-> (Bool, Bool)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (NPieces -> Bool)
-> (NPieces -> Bool) -> (NPieces, NPieces) -> (Bool, Bool)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
(***) (
			(NPieces -> Bool) -> NPieces -> Bool
forall a. a -> a
id ((NPieces -> Bool) -> NPieces -> Bool)
-> ((NPieces -> Bool) -> NPieces -> Bool)
-> (NPieces -> Bool)
-> (NPieces -> Bool, NPieces -> Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& (NPieces -> Bool) -> NPieces -> Bool
forall a. a -> a
id ((NPieces -> Bool) -> (NPieces -> Bool, NPieces -> Bool))
-> (NPieces -> Bool) -> (NPieces -> Bool, NPieces -> Bool)
forall a b. (a -> b) -> a -> b
$ (NPieces -> NPieces -> Bool
forall a. Ord a => a -> a -> Bool
> NPieces
Component.Piece.nPiecesPerSide)
		) ((NPieces, NPieces) -> (Bool, Bool))
-> (censor -> (NPieces, NPieces)) -> censor -> (Bool, Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. censor -> (NPieces, NPieces)
forall censor. Censor censor => censor -> (NPieces, NPieces)
countPiecesByLogicalColour,
		String
"there are too many pieces of at least one logical colour."
	)
 ]