| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Game.H2048.Gameplay
Description
Game implementation on top of Game.H2048.Core. This module is formally the API for this package, please avoid using Game.H2048.Core directly if possible.
Synopsis
- data Gameplay
- _gpRule :: Gameplay -> GameRule
- _gpScore :: Gameplay -> Int
- _gpBoard :: Gameplay -> GameBoard
- _gpGen :: Gameplay -> TFGen
- randomOp :: (TFGen -> (a, TFGen)) -> Gameplay -> (a, Gameplay)
- mkGameplay :: TFGen -> GameRule -> Gameplay
- spawnNewCell :: Gameplay -> Set Coord -> Maybe (((Coord, Cell), Set Coord), Gameplay)
- type GameBoard = Map Coord Cell
- type CellTier = Int
- data Cell
- _cTier :: Cell -> CellTier
- data Dir
- type Distrib = Distrib' Int
- data GameRule = GameRule {
- _grDim :: (Int, Int)
- _grMergeAward :: CellTier -> Int
- _grNewCellDistrib :: Distrib
- _grInitSpawn :: Int
- _grHasWon :: Int -> GameBoard -> Bool
- newGame :: Gameplay -> Gameplay
- stepGame :: Dir -> Gameplay -> Maybe Gameplay
- standardGameRule :: GameRule
- hasWon :: Gameplay -> Bool
- isAlive :: Gameplay -> Bool
- cellToInt :: Cell -> Int
- intToCell :: Int -> Maybe Cell
- computeDistrib :: IntMap Int -> Distrib
Documentation
A Gameplay is an obscure data type to keep track of information necessary
for a single game play. Its fields can be accessed through functions
with _gp prefix.
_gpRule :: Gameplay -> GameRule Source #
Encodes rule of this game. This field must not change after creation.
_gpBoard :: Gameplay -> GameBoard Source #
The Game board. If this field is an empty map, that means the game is not yet started.
mkGameplay :: TFGen -> GameRule -> Gameplay Source #
Create a Gameplay. Note that the return value must be passed to newGame
before it can accept any game moves.
The purpose of this two-step approach (i.e. mkGameplay then newGame) is
to separate data type creation from the effect of mutating random generator,
which is required at the beginning of a game.
spawnNewCell :: Gameplay -> Set Coord -> Maybe (((Coord, Cell), Set Coord), Gameplay) Source #
spawnNewCell gameplay emptyCells picks an empty cell from emptyCells,
and assign it with a cell value. The operation will fail if and only if
emptyCells is empty.
Upon successful return, the value wrapped in Just is
(sepResult, gameplay') where sepResult indicates coordinate and cell value
chosen, and remaining part of emptyCells.
The reason for explicitly passing emptyCells on this operation
is to make it easier to pick multiple cells while not touching most parts of Gameplay.
In fact you can expect this operation to only mutate the TFGen inside Gameplay.
An obscure data type that wraps CellTier.
Moves that a user could do.
type Distrib = Distrib' Int Source #
A Distrib is a non-empty Vector whose each element (a,b) satisfies:
a, when taken in sequence, is positive and strictly increasing.b, when taken in sequence, is strictly increasing.
Think this data type as a precomputation result for making weighted random choice.
You can use computeDistrib to generate a value of this.
A data type for encoding game rules that do not necessarily needs to be hard-coded into core logic.
You can use standardGameRule for a standard game rule,
or make changes using it as the base.
Constructors
| GameRule | |
Fields
| |
stepGame :: Dir -> Gameplay -> Maybe Gameplay Source #
stepGame d gp tries to apply move d on current state of the game gp, returns:
Nothingif this move is invalid (failed to apply the move).Just movesif this move is valid, also returns all possible moves after the board is fully updated (meaning new cell has been spawned).
standardGameRule :: GameRule Source #
The standard game rule. This value can be used as a base for customized game rules.