h2048-0.4.0.0: An Implementation of Game 2048

Safe HaskellNone
LanguageHaskell2010

Game.H2048.Core

Description

This module is considered internal. Clients should use Game.H2048.Gameplay instead.

Synopsis

Documentation

type Coord = (Int, Int) Source #

Zero-based (rowIndex, colIndex).

type Coords = [Coord] Source #

List of Coord. This list is usually a complete row or column in the game board.

type CoordsGroup = [Coords] Source #

List of Coords, expected to exact-cover the game board.

data Dir Source #

Moves that a user could do.

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 CellTier = Int Source #

A CellTier is simply a positive Int. Every time two cell merges, the tier of the resulting cell increases by one relative to cell tier prior to the merge.

newtype Cell Source #

An obscure data type that wraps CellTier.

Constructors

Cell 

Fields

Instances
Eq Cell Source # 
Instance details

Defined in Game.H2048.Core

Methods

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

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

Ord Cell Source # 
Instance details

Defined in Game.H2048.Core

Methods

compare :: Cell -> Cell -> Ordering #

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

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

(>) :: Cell -> Cell -> Bool #

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

max :: Cell -> Cell -> Cell #

min :: Cell -> Cell -> Cell #

Show Cell Source # 
Instance details

Defined in Game.H2048.Core

Methods

showsPrec :: Int -> Cell -> ShowS #

show :: Cell -> String #

showList :: [Cell] -> ShowS #

type Distrib' a = Vector (a, Int) Source #

The same as Distrib except parameterized on the value type.

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.

data GameRule Source #

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

type GameBoard = Map Coord Cell Source #

A GameBoard is a map from coordinates to Cells for a game.

Note that the map could be empty to indicate that a new game is not started yet.

randomPick :: Distrib' a -> TFGen -> (a, TFGen) Source #

Pick a value randomly following the distribution as specified by the argument.

allCoords :: GameRule -> Coords Source #

Return a unique, sorted list of all coordinations of a board.

applyMove :: GameRule -> Dir -> GameBoard -> Maybe (GameBoard, Int) Source #

Apply a game move on a board. This operation fails if and only if the move results in no change to the game board.

possibleMoves :: GameRule -> GameBoard -> [(Dir, (GameBoard, Int))] Source #

Return possible moves that can be performed on current board.

unsafeIntToCell :: Int -> Cell Source #

Convert an integer to Cell, the input is expected to be a power of 2 but no check is enforced.

Given that standard game is based on powers of 2, it makes sense that we implement some direct support for it.

intToCell :: Int -> Maybe Cell Source #

Safely convert a power of two into Cell.

cellToInt :: Cell -> Int Source #

Convert Cell back into a power of 2.

standardGameRule :: GameRule Source #

The standard game rule. This value can be used as a base for customized game rules.

merge :: Cell -> Cell -> Maybe Cell Source #

Attempt to merge two Cell s.

Only successful when two Cells are equal, resulting in a new Cell with tier increased by 1.

mergeWithScore :: GameRule -> Cell -> Cell -> Maybe (Cell, Int) Source #

Merge two cells with a reward as specified by GameRule.

mergeLine :: GameRule -> [Cell] -> ([Cell], Int) Source #

Merge a single line of cells, return the resulting line and scores awarded according to the GameRule.

dirToCoordsGroups :: GameRule -> Dir -> CoordsGroup Source #

Given a game move, return rows or columns of Coords that forms the complete board.

computeDistrib :: IntMap Int -> Distrib Source #

Computes Distrib for weighted random cell tier spawns.

The input must be a non-empty map from cell tiers to their corresponding weight. All weights must be positive.

testDistrib :: Int -> [(Int, Int)] -> IO () Source #

Repeat the process of randomly picking elements following a distribution in IO.

This function is exported just for manual testing.

isAlive :: GameRule -> GameBoard -> Bool Source #

A current game is consider "alive" when there are at least one valid move for the current board.

Note that since a GameBoard can be newly initiate as empty Map, it is not "alive" by definition.