check should return score or nothing if invalid

This commit is contained in:
Sidharth Kulkarni 2026-05-09 20:32:17 -07:00
parent 57579fa5e4
commit 38838c47b9
Signed by: skulk
SSH key fingerprint: SHA256:Jby+S9d1WmwqnXIrngHgccYNHz+cYquxN1zm3ym3Kbg
4 changed files with 11 additions and 13 deletions

View file

@ -11,10 +11,9 @@ makeMove (MkPlayerIndex idx) (SelectSquare topLeft bottomRight) state@MkGameStat
let tiles = select topLeft bottomRight board let tiles = select topLeft bottomRight board
gMode = gameMode board gMode = gameMode board
transform = transform =
if check gMode tiles case check gMode tiles of
-- add one to player score Just score ->
-- TODO: add the proper score here over (#players % (ix idx) % #score) (+ score)
then over (#players % (ix idx) % #score) (+ 1)
-- invalid move, do nothing -- invalid move, do nothing
else id Nothing -> id
in transform state in transform state

View file

@ -27,7 +27,7 @@ clear topLeft bottomRight gBoard@MkGameBoard{cells} = gBoard{cells = newCells}
where where
newCells = cells // map (,Nothing) (range (topLeft, bottomRight)) 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} = selectCheck topLeft bottomRight board@MkGameBoard{gameMode} =
check gameMode (select topLeft bottomRight board) check gameMode (select topLeft bottomRight board)

View file

@ -8,7 +8,6 @@ import Data.Bifunctor (Bifunctor (first))
import Data.Kind (Type) import Data.Kind (Type)
import Display import Display
import System.Random.Stateful (Random (randomR), RandomGen) import System.Random.Stateful (Random (randomR), RandomGen)
import Types.BoardPosition
{- | The type of game that is being played. {- | 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 class (Display (Tile t), Show (Tile t), Eq (Tile t), Eq t, Show t) => GameMode t where
data Tile t :: Type data Tile t :: Type
check :: t -> [Tile t] -> Bool check :: t -> [Tile t] -> Maybe Int
gen :: (RandomGen g) => t -> g -> (Tile t, g) gen :: (RandomGen g) => t -> g -> (Tile t, g)
data SumTo = MkSumTo Int data SumTo = MkSumTo Int
@ -28,8 +27,8 @@ data SumTo = MkSumTo Int
instance GameMode SumTo where instance GameMode SumTo where
data Tile SumTo = IntTile Int deriving (Eq, Show) data Tile SumTo = IntTile Int deriving (Eq, Show)
check (MkSumTo n) [] = n == 0 check (MkSumTo n) [] = if n == 0 then Just 0 else Nothing
check (MkSumTo n) lst = n == sum (map toInt lst) check (MkSumTo n) lst = if n == sum (map toInt lst) then Just (length lst) else Nothing
where where
toInt (IntTile a) = a toInt (IntTile a) = a
gen (MkSumTo n) = first IntTile . randomR (1, n) gen (MkSumTo n) = first IntTile . randomR (1, n)

View file

@ -32,8 +32,8 @@ spec = do
displayText gameBoard `shouldBe` "1 2 \n3 4 \n" displayText gameBoard `shouldBe` "1 2 \n3 4 \n"
it "validates a move" do it "validates a move" do
selectCheck (0, 0) (1, 1) gameBoard `shouldBe` True selectCheck (0, 0) (1, 1) gameBoard `shouldBe` Just 4
selectCheck (0, 0) (0, 1) gameBoard `shouldBe` False selectCheck (0, 0) (0, 1) gameBoard `shouldBe` Nothing
it "clears properly" do it "clears properly" do
let cleared = clear (0, 0) (0, 1) gameBoard let cleared = clear (0, 0) (0, 1) gameBoard
@ -55,7 +55,7 @@ spec = do
let action = SelectSquare (0, 0) (0, 1) let action = SelectSquare (0, 0) (0, 1)
newState = makeMove (MkPlayerIndex 0) action initialState 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 it "does not increase score if sum does not equal target" do
let invalidAction = SelectSquare (0, 0) (1, 0) let invalidAction = SelectSquare (0, 0) (1, 0)