Add actions and implement select square action
optics!
This commit is contained in:
parent
c2df3477fd
commit
c8e43990ed
6 changed files with 39 additions and 3 deletions
|
|
@ -18,6 +18,7 @@ build-type: Simple
|
||||||
library
|
library
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
Game
|
Game
|
||||||
|
Types.BoardAction
|
||||||
Types.BoardPosition
|
Types.BoardPosition
|
||||||
Types.GameBoard
|
Types.GameBoard
|
||||||
Types.GameMode
|
Types.GameMode
|
||||||
|
|
@ -34,6 +35,9 @@ library
|
||||||
MultiParamTypeClasses
|
MultiParamTypeClasses
|
||||||
TypeFamilies
|
TypeFamilies
|
||||||
UndecidableInstances
|
UndecidableInstances
|
||||||
|
OverloadedLabels
|
||||||
|
NamedFieldPuns
|
||||||
|
TypeOperators
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
build-depends:
|
build-depends:
|
||||||
array
|
array
|
||||||
|
|
@ -61,6 +65,9 @@ executable numbersquare
|
||||||
MultiParamTypeClasses
|
MultiParamTypeClasses
|
||||||
TypeFamilies
|
TypeFamilies
|
||||||
UndecidableInstances
|
UndecidableInstances
|
||||||
|
OverloadedLabels
|
||||||
|
NamedFieldPuns
|
||||||
|
TypeOperators
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
build-depends:
|
build-depends:
|
||||||
array
|
array
|
||||||
|
|
@ -90,6 +97,9 @@ test-suite numberspec-test
|
||||||
MultiParamTypeClasses
|
MultiParamTypeClasses
|
||||||
TypeFamilies
|
TypeFamilies
|
||||||
UndecidableInstances
|
UndecidableInstances
|
||||||
|
OverloadedLabels
|
||||||
|
NamedFieldPuns
|
||||||
|
TypeOperators
|
||||||
ghc-options: -Wall -threaded -O0
|
ghc-options: -Wall -threaded -O0
|
||||||
build-depends:
|
build-depends:
|
||||||
QuickCheck
|
QuickCheck
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,9 @@ default-extensions:
|
||||||
- MultiParamTypeClasses
|
- MultiParamTypeClasses
|
||||||
- TypeFamilies
|
- TypeFamilies
|
||||||
- UndecidableInstances
|
- UndecidableInstances
|
||||||
|
- OverloadedLabels
|
||||||
|
- NamedFieldPuns
|
||||||
|
- TypeOperators
|
||||||
|
|
||||||
library:
|
library:
|
||||||
source-dirs:
|
source-dirs:
|
||||||
|
|
|
||||||
6
src/Types/BoardAction.hs
Normal file
6
src/Types/BoardAction.hs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
module Types.BoardAction where
|
||||||
|
|
||||||
|
import Types.BoardPosition
|
||||||
|
|
||||||
|
data BoardAction = SelectSquare {topLeft :: BoardPosition, bottomRight :: BoardPosition}
|
||||||
|
deriving (Show, Eq)
|
||||||
|
|
@ -8,6 +8,7 @@ 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.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,11 @@ module Types.GameState where
|
||||||
|
|
||||||
import Data.Sequence (Seq)
|
import Data.Sequence (Seq)
|
||||||
import Data.Sequence qualified as S
|
import Data.Sequence qualified as S
|
||||||
import Optics (makeFieldLabels)
|
import Optics
|
||||||
import System.Random
|
import System.Random
|
||||||
|
import Types.BoardAction
|
||||||
import Types.GameBoard
|
import Types.GameBoard
|
||||||
import Types.GameMode (GameMode)
|
import Types.GameMode
|
||||||
import Types.Player
|
import Types.Player
|
||||||
|
|
||||||
data GameState mode = MkGameState
|
data GameState mode = MkGameState
|
||||||
|
|
@ -15,7 +16,7 @@ data GameState mode = MkGameState
|
||||||
, players :: Seq PlayerState
|
, players :: Seq PlayerState
|
||||||
}
|
}
|
||||||
|
|
||||||
makeFieldLabels ''GameState
|
makeFieldLabelsNoPrefix ''GameState
|
||||||
|
|
||||||
newtype PlayerIndex = MkPlayerIndex Int
|
newtype PlayerIndex = MkPlayerIndex Int
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
@ -25,3 +26,12 @@ newGame gmode playerInfos rng =
|
||||||
let (initialBoard, nextRng) = newRandomBoard gmode 10 10 rng
|
let (initialBoard, nextRng) = newRandomBoard gmode 10 10 rng
|
||||||
playerStates = S.fromList $ map (\n -> MkPlayerState{name = n, score = 0}) playerInfos
|
playerStates = S.fromList $ map (\n -> MkPlayerState{name = n, score = 0}) playerInfos
|
||||||
in (MkGameState initialBoard playerStates, nextRng)
|
in (MkGameState initialBoard playerStates, nextRng)
|
||||||
|
|
||||||
|
makeMove :: (GameMode mode) => PlayerIndex -> BoardAction -> GameState mode -> GameState mode
|
||||||
|
makeMove (MkPlayerIndex idx) (SelectSquare topLeft bottomRight) state@MkGameState{board} =
|
||||||
|
let tiles = select topLeft bottomRight board
|
||||||
|
gMode = gameMode board
|
||||||
|
in if check gMode tiles
|
||||||
|
then
|
||||||
|
over (#players % (ix idx) % #score) (+ 1) state
|
||||||
|
else state
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
|
{-# LANGUAGE TemplateHaskell #-}
|
||||||
|
|
||||||
module Types.Player where
|
module Types.Player where
|
||||||
|
|
||||||
|
import Optics
|
||||||
|
|
||||||
import Data.Text
|
import Data.Text
|
||||||
|
|
||||||
newtype PlayerName = MkPlayerName Text
|
newtype PlayerName = MkPlayerName Text
|
||||||
|
|
@ -10,3 +14,5 @@ data PlayerState = MkPlayerState
|
||||||
, score :: Int
|
, score :: Int
|
||||||
}
|
}
|
||||||
deriving (Eq, Show)
|
deriving (Eq, Show)
|
||||||
|
|
||||||
|
makeFieldLabelsNoPrefix ''PlayerState
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue