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
.
- type Board = [[Int]]
- type Line = [Int]
- data Dir
- data BoardUpdated = BoardUpdated {}
- data GameState
- gameState :: Board -> GameState
- compactLine :: Line -> Writer (Sum Int) Line
- initBoard :: Board
- initGameBoard :: MonadRandom r => r (Board, Int)
- updateBoard :: Dir -> Board -> Maybe BoardUpdated
- insertNewCell :: MonadRandom r => Board -> r (Maybe Board)
- generateNewCell :: MonadRandom r => r Int
Documentation
represent a 4x4 board for Game 2048 each element should be either zero or 2^i where i >= 1.
current game state, see also gameState
compactLine :: Line -> Writer (Sum Int) Line Source
move each non-zero element to their leftmost possible position while preserving the order
initGameBoard :: MonadRandom r => r (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 BoardUpdated Source
update the board taking a direction,
a BoardUpdated
is returned on success,
if this update does nothing, that means a failure (Nothing)
insertNewCell :: MonadRandom r => Board -> r (Maybe Board) Source
try to insert a new cell randomly
generateNewCell :: MonadRandom r => r Int Source
generate a new cell according to the game rule we have 90% probability of getting a cell of value 2, and 10% probability of getting a cell of value 4.