68 lines
2.6 KiB
Haskell
68 lines
2.6 KiB
Haskell
{-# LANGUAGE BlockArguments #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Spec where
|
|
|
|
import Data.Array (array, elems)
|
|
import Data.Sequence qualified as S
|
|
import Display (displayText)
|
|
import Game
|
|
import Optics
|
|
import Test.Hspec
|
|
import Types.BoardAction
|
|
import Types.GameBoard
|
|
import Types.GameMode (SumTo (MkSumTo), Tile (IntTile))
|
|
import Types.GameState
|
|
import Types.Player
|
|
|
|
spec :: Spec
|
|
spec = do
|
|
describe "gameboard" do
|
|
let tiles =
|
|
array
|
|
((0, 0), (1, 1))
|
|
[ ((0, 0), Just $ IntTile 1)
|
|
, ((0, 1), Just $ IntTile 2)
|
|
, ((1, 0), Just $ IntTile 3)
|
|
, ((1, 1), Just $ IntTile 4)
|
|
]
|
|
gameBoard = MkGameBoard{width = 2, height = 2, gameMode = MkSumTo 10, cells = tiles}
|
|
|
|
it "displays correctly" 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
|
|
|
|
it "clears properly" do
|
|
let cleared = clear (0, 0) (0, 1) gameBoard
|
|
elems (cells cleared) `shouldBe` [Nothing, Nothing, Just $ IntTile 3, Just $ IntTile 4]
|
|
|
|
describe "player moves" do
|
|
let tiles =
|
|
array
|
|
((0, 0), (1, 1))
|
|
[ ((0, 0), Just $ IntTile 5)
|
|
, ((0, 1), Just $ IntTile 5)
|
|
, ((1, 0), Just $ IntTile 1)
|
|
, ((1, 1), Just $ IntTile 2)
|
|
]
|
|
gameBoard = MkGameBoard{width = 2, height = 2, gameMode = MkSumTo 10, cells = tiles}
|
|
initialState = MkGameState{board = gameBoard, players = S.fromList [MkPlayerState (MkPlayerName "P1") 0]}
|
|
|
|
it "increases score if sum equals target" do
|
|
let action = SelectSquare (0, 0) (0, 1)
|
|
newState = makeMove (MkPlayerIndex 0) action initialState
|
|
|
|
preview (#players % ix 0 % #score) newState `shouldBe` Just 1
|
|
|
|
it "does not increase score if sum does not equal target" do
|
|
let invalidAction = SelectSquare (0, 0) (1, 0)
|
|
newState = makeMove (MkPlayerIndex 0) invalidAction initialState
|
|
preview (#players % ix 0 % #score) newState `shouldBe` Just 0
|
|
|
|
it "does not change anything if invalid player index is provided" do
|
|
let action = SelectSquare (0, 0) (0, 1)
|
|
newState = makeMove (MkPlayerIndex 99) action initialState
|
|
preview (#players % ix 99 % #score) newState `shouldBe` Nothing
|