Copyright | (c) 2014 Javran Cheng |
---|---|
License | MIT |
Maintainer | Javran.C@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
The core game logic implementation for Game 2048.
The routine for using this library would be:
- use
initGameBoard
to get a valid board to begin with. (two new cells are inserted for you, if you want to use an empty board,initBoard
is a shorthand) - interact with user algorithm etc., use
updateBoard
to update a board. - use
insertNewCell
to insert a new cell randomly - examine if the player wins loses is still alive using
gameState
.
Synopsis
- data Dir
- type BoardUpdateResult = (Board, Int)
- data Board
- mkBoard :: [[Int]] -> Board
- fromBoard :: Board -> [[Int]]
- defBoard :: Board
- data Line
- mkLine :: [Int] -> Line
- defLine :: Line
- gameState :: Board -> GameState
- data GameState = GS {}
- compactLine' :: Line -> (Sum Int, Line)
- compactLine :: MonadWriter (Sum Int) m => Line -> m Line
- initGameBoard :: (MonadRandom m, Alternative m) => m (Board, Int)
- updateBoard :: Dir -> Board -> Maybe BoardUpdateResult
- insertNewCell :: (MonadRandom r, Alternative r) => Board -> r (Maybe Board)
- nextMoves :: Board -> [(Dir, BoardUpdateResult)]
Documentation
move direction
type BoardUpdateResult = (Board, Int) Source #
result after a successful updateBoard
represent a 4x4 board for Game 2048 each element should be either zero or 2^i where i >= 1.
a list of 4 elements, stands for one column / row in the board
compactLine :: MonadWriter (Sum Int) m => Line -> m Line Source #
move each non-zero element to their leftmost possible position while preserving the order
initGameBoard :: (MonadRandom m, Alternative m) => m (Board, Int) Source #
initialize the board by puting two cells randomly
into the board.
See generateNewCell
for the cell generating rule.
updateBoard :: Dir -> Board -> Maybe BoardUpdateResult Source #
update the board taking a direction,
a BoardUpdated
is returned on success,
if this update does nothing, that means a failure (Nothing)
note that here "update" does not include adding one random cell of 2 or 4
into the board
insertNewCell :: (MonadRandom r, Alternative r) => Board -> r (Maybe Board) Source #
try to insert a new cell randomly