hs2048-0.1.0: A 2048 clone in Haskell.

Safe HaskellSafe-Inferred
LanguageHaskell2010

Hs2048.Renderer

Description

Functions for pretty-printing games to the console.

Synopsis

Documentation

center :: Int -> String -> String Source

Centers a string in the given number of characters, using spaces for padding.

>>> center 3 "x"
" x "
>>> center 2 "x"
" x"

color :: Tile -> Int Source

Calculates the color code for a tile. See http://en.wikipedia.org/wiki/ANSI_escape_code#Colors.

>>> color Nothing
30
>>> color (Just 2)
31
>>> color (Just 2048)
45

renderBoard :: Board -> String Source

Renders a board with colorized, centered tiles. See renderTile and center.

>>> renderBoard [[Nothing, Just 2]]
"\ESC[30m-\ESC[0m \ESC[31m2\ESC[0m\n"
>>> renderBoard [[Nothing, Just 16]]
" \ESC[30m-\ESC[0m \ESC[34m16\ESC[0m\n"

renderGame :: Board -> String Source

Renders a board along with its score. See renderBoard.

>>> renderGame [[Nothing, Just 2]]
"Score: 0\n\ESC[30m-\ESC[0m \ESC[31m2\ESC[0m\n"

renderTile :: Tile -> String Source

Renders a colorized tile. See color.

>>> renderTile Nothing
"\ESC[30m-\ESC[0m"
>>> renderTile (Just 2)
"\ESC[31m2\ESC[0m"