hs2048-0.1.0: A 2048 clone in Haskell.

Safe HaskellSafe-Inferred
LanguageHaskell2010

Hs2048.Vector

Description

Types and functions for manipulating vectors.

Synopsis

Documentation

type Vector = [Tile] Source

Represents a row or column on the game board. By convention, a vector has 4 tiles.

canShift :: Vector -> Bool Source

Determines if the vector can be shifted.

>>> canShift [Nothing, Just 2, Nothing, Nothing]
True

empty :: Int -> Vector Source

Returns an empty vector of the given size.

>>> empty 4
[Nothing,Nothing,Nothing,Nothing]

emptyIndexes :: Vector -> [Int] Source

Returns the indexes that don't contain tiles.

>>> emptyIndexes [Nothing, Just 2, Nothing, Nothing]
[0,2,3]

parse :: String -> Vector Source

Parses a string as a vector. This is the inverse of render.

>>> parse "- 2 - -"
[Nothing,Just 2,Nothing,Nothing]

render :: Vector -> String Source

Renders a vector as a string. This is the inverse of parse.

>>> render [Nothing, Just 2, Nothing, Nothing]
"- 2 - -"

score :: Vector -> Int Source

Calculates the score of a vector.

>>> score [Nothing, Just 2, Just 4, Just 8]
20

set :: Vector -> Tile -> Int -> Vector Source

Sets a tile at the given index in the vector.

>>> set [Nothing, Nothing, Nothing, Nothing] (Just 2) 1
[Nothing,Just 2,Nothing,Nothing]

shift :: Vector -> Vector Source

Shifts a vector toward the head. The output vector will be the same size as the input vector, padded with Nothing.

>>> shift [Nothing, Just 2, Nothing, Nothing]
[Just 2,Nothing,Nothing,Nothing]

Like tiles will be combined.

>>> shift [Just 2, Nothing, Just 2, Just 2]
[Just 4,Just 2,Nothing,Nothing]

Any number of tiles can be combined in one shift.

>>> shift [Just 2, Just 2, Just 4, Just 4]
[Just 4,Just 8,Nothing,Nothing]