diff --git a/src/Game.hs b/src/Game.hs index 7898976..712dc29 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -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 diff --git a/src/Types/GameBoard.hs b/src/Types/GameBoard.hs index 8e158fc..da81c41 100644 --- a/src/Types/GameBoard.hs +++ b/src/Types/GameBoard.hs @@ -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) diff --git a/src/Types/GameMode.hs b/src/Types/GameMode.hs index 994837b..976b1e5 100644 --- a/src/Types/GameMode.hs +++ b/src/Types/GameMode.hs @@ -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) diff --git a/test/Spec.hs b/test/Spec.hs index c28518f..78a9381 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -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)