h2048-0.2.0.0: a haskell implementation of Game 2048

Copyright(c) 2014 Javran Cheng
LicenseMIT
MaintainerJavran.C@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Game.H2048.Core

Description

The core game logic implementation for Game 2048.

The routine for using this library would be:

  1. 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)
  2. interact with user algorithm etc., use updateBoard to update a board.
  3. use insertNewCell to insert a new cell randomly
  4. examine if the player wins loses is still alive using gameState.

Synopsis

Documentation

type Board = [[Int]] Source

represent a 4x4 board for Game 2048 each element should be either zero or 2^i where i >= 1.

type Line = [Int] Source

a list of 4 elements, stands for one column / row in the board

data Dir Source

move direction

Constructors

DUp 
DDown 
DLeft 
DRight 

Instances

data BoardUpdated Source

result after a successful updateBoard

Constructors

BoardUpdated 

Fields

brBoard :: Board

new board

brScore :: Int

score collected in this update

data GameState Source

current game state, see also gameState

Constructors

Win

win, can make no step further

WinAlive

win, and still alive

Lose

can make no step further, no cell reaches 2048

Alive

playing

gameState :: Board -> GameState Source

return current game state. Win if any cell is equal to or greater than 2048 or Lose if we can move no further otherwise, Alive.

compactLine :: Line -> Writer (Sum Int) Line Source

move each non-zero element to their leftmost possible position while preserving the order

initBoard :: Board Source

the initial board before a game started

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.