h2048-0.3.0.0: An 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

data Dir Source #

move direction

Constructors

DUp 
DDown 
DLeft 
DRight 
Instances
Bounded Dir Source # 
Instance details

Defined in Game.H2048.Core

Methods

minBound :: Dir #

maxBound :: Dir #

Enum Dir Source # 
Instance details

Defined in Game.H2048.Core

Methods

succ :: Dir -> Dir #

pred :: Dir -> Dir #

toEnum :: Int -> Dir #

fromEnum :: Dir -> Int #

enumFrom :: Dir -> [Dir] #

enumFromThen :: Dir -> Dir -> [Dir] #

enumFromTo :: Dir -> Dir -> [Dir] #

enumFromThenTo :: Dir -> Dir -> Dir -> [Dir] #

Eq Dir Source # 
Instance details

Defined in Game.H2048.Core

Methods

(==) :: Dir -> Dir -> Bool #

(/=) :: Dir -> Dir -> Bool #

Ord Dir Source # 
Instance details

Defined in Game.H2048.Core

Methods

compare :: Dir -> Dir -> Ordering #

(<) :: Dir -> Dir -> Bool #

(<=) :: Dir -> Dir -> Bool #

(>) :: Dir -> Dir -> Bool #

(>=) :: Dir -> Dir -> Bool #

max :: Dir -> Dir -> Dir #

min :: Dir -> Dir -> Dir #

Show Dir Source # 
Instance details

Defined in Game.H2048.Core

Methods

showsPrec :: Int -> Dir -> ShowS #

show :: Dir -> String #

showList :: [Dir] -> ShowS #

type BoardUpdateResult = (Board, Int) Source #

result after a successful updateBoard

data Board Source #

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

defBoard :: Board Source #

the initial board before a game started

data Line Source #

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

Instances
Eq Line Source # 
Instance details

Defined in Game.H2048.Core

Methods

(==) :: Line -> Line -> Bool #

(/=) :: Line -> Line -> Bool #

Show Line Source # 
Instance details

Defined in Game.H2048.Core

Methods

showsPrec :: Int -> Line -> ShowS #

show :: Line -> String #

showList :: [Line] -> ShowS #

data GameState Source #

Constructors

GS 

Fields

Instances
Eq GameState Source # 
Instance details

Defined in Game.H2048.Core

Show GameState Source # 
Instance details

Defined in Game.H2048.Core

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