hs2048-0.1.0: A 2048 clone in Haskell.

Safe HaskellSafe-Inferred
LanguageHaskell2010

Hs2048.Board

Description

Types and functions for manipulating boards.

Synopsis

Documentation

type Board = [Vector] Source

Represents the game board. By convention, it is row-major.

canMove :: Board -> Direction -> Bool Source

Determines if the board can be moved in the given direction. See move.

>>> canMove [[Nothing, Just 2]] D.West
True

canShift :: Board -> Bool Source

Determines if the board can be shifted. See shift.

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

empty Source

Arguments

:: Int

Width

-> Int

Height

-> Board 

Returns an empty board of the given size.

>>> empty 2 1
[[Nothing,Nothing]]

emptyPoints :: Board -> [Point] Source

Returns the points that don't contain tiles.

>>> emptyPoints [[Nothing, Just 2]]
[(0,0)]

move :: Board -> Direction -> Board Source

Moves the board in the given direction.

>>> move [[Nothing, Just 2]] D.West
[[Just 2,Nothing]]

parse :: String -> Board Source

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

>>> parse "- 2\n4 -\n"
[[Nothing,Just 2],[Just 4,Nothing]]

render :: Board -> String Source

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

>>> render [[Nothing, Just 2], [Just 4, Nothing]]
"- 2\n4 -\n"

rotate :: Board -> Board Source

Rotate the board 90 degrees clockwise.

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

rotateFrom :: Board -> Direction -> Board Source

Rotates the board so that West is at the left, assuming the given direction is currently at the left. This is the inverse of rotateTo.

>>> rotateFrom [[Just 4, Nothing], [Nothing, Just 2]] D.South
[[Nothing,Just 2],[Just 4,Nothing]]

rotateTo :: Board -> Direction -> Board Source

Rotates the board so that the given direction is at the left. This is the inverse of rotateFrom

>>> rotateTo [[Nothing, Just 2], [Just 4, Nothing]] D.South
[[Just 4,Nothing],[Nothing,Just 2]]

score :: Board -> Int Source

Calculates the score of a board.

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

set :: Board -> Tile -> Point -> Board Source

Sets a tile at the given point in the board.

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

shift :: Board -> Board Source

Shifts a board toward the head. See shift.

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