check should return score or nothing if invalid
This commit is contained in:
parent
57579fa5e4
commit
38838c47b9
4 changed files with 11 additions and 13 deletions
|
|
@ -11,10 +11,9 @@ makeMove (MkPlayerIndex idx) (SelectSquare topLeft bottomRight) state@MkGameStat
|
|||
let tiles = select topLeft bottomRight board
|
||||
gMode = gameMode board
|
||||
transform =
|
||||
if check gMode tiles
|
||||
-- add one to player score
|
||||
-- TODO: add the proper score here
|
||||
then over (#players % (ix idx) % #score) (+ 1)
|
||||
case check gMode tiles of
|
||||
Just score ->
|
||||
over (#players % (ix idx) % #score) (+ score)
|
||||
-- invalid move, do nothing
|
||||
else id
|
||||
Nothing -> id
|
||||
in transform state
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ clear topLeft bottomRight gBoard@MkGameBoard{cells} = gBoard{cells = newCells}
|
|||
where
|
||||
newCells = cells // map (,Nothing) (range (topLeft, bottomRight))
|
||||
|
||||
selectCheck :: (GameMode mode) => BoardPosition -> BoardPosition -> GameBoard mode -> Bool
|
||||
selectCheck :: (GameMode mode) => BoardPosition -> BoardPosition -> GameBoard mode -> Maybe Int
|
||||
selectCheck topLeft bottomRight board@MkGameBoard{gameMode} =
|
||||
check gameMode (select topLeft bottomRight board)
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import Data.Bifunctor (Bifunctor (first))
|
|||
import Data.Kind (Type)
|
||||
import Display
|
||||
import System.Random.Stateful (Random (randomR), RandomGen)
|
||||
import Types.BoardPosition
|
||||
|
||||
{- | The type of game that is being played.
|
||||
|
||||
|
|
@ -19,7 +18,7 @@ that sums up to 'n'.
|
|||
-}
|
||||
class (Display (Tile t), Show (Tile t), Eq (Tile t), Eq t, Show t) => GameMode t where
|
||||
data Tile t :: Type
|
||||
check :: t -> [Tile t] -> Bool
|
||||
check :: t -> [Tile t] -> Maybe Int
|
||||
gen :: (RandomGen g) => t -> g -> (Tile t, g)
|
||||
|
||||
data SumTo = MkSumTo Int
|
||||
|
|
@ -28,8 +27,8 @@ data SumTo = MkSumTo Int
|
|||
instance GameMode SumTo where
|
||||
data Tile SumTo = IntTile Int deriving (Eq, Show)
|
||||
|
||||
check (MkSumTo n) [] = n == 0
|
||||
check (MkSumTo n) lst = n == sum (map toInt lst)
|
||||
check (MkSumTo n) [] = if n == 0 then Just 0 else Nothing
|
||||
check (MkSumTo n) lst = if n == sum (map toInt lst) then Just (length lst) else Nothing
|
||||
where
|
||||
toInt (IntTile a) = a
|
||||
gen (MkSumTo n) = first IntTile . randomR (1, n)
|
||||
|
|
|
|||
|
|
@ -32,8 +32,8 @@ spec = do
|
|||
displayText gameBoard `shouldBe` "1 2 \n3 4 \n"
|
||||
|
||||
it "validates a move" do
|
||||
selectCheck (0, 0) (1, 1) gameBoard `shouldBe` True
|
||||
selectCheck (0, 0) (0, 1) gameBoard `shouldBe` False
|
||||
selectCheck (0, 0) (1, 1) gameBoard `shouldBe` Just 4
|
||||
selectCheck (0, 0) (0, 1) gameBoard `shouldBe` Nothing
|
||||
|
||||
it "clears properly" do
|
||||
let cleared = clear (0, 0) (0, 1) gameBoard
|
||||
|
|
@ -55,7 +55,7 @@ spec = do
|
|||
let action = SelectSquare (0, 0) (0, 1)
|
||||
newState = makeMove (MkPlayerIndex 0) action initialState
|
||||
|
||||
preview (#players % ix 0 % #score) newState `shouldBe` Just 1
|
||||
preview (#players % ix 0 % #score) newState `shouldBe` Just 2
|
||||
|
||||
it "does not increase score if sum does not equal target" do
|
||||
let invalidAction = SelectSquare (0, 0) (1, 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue