Portability | portable |
---|---|

Stability | experimental |

Maintainer | amy@nualeargais.ie |

Safe Haskell | Safe-Inferred |

A regular arrangement of tiles. Grids have a variety of uses, including games and self-organising maps.

NOTE: Version 3.0 changed the order of parameters for many functions. This makes it easier for the user to write mapping and folding operations.

- class Eq x => Grid g s x | g -> s, g -> x where
- indices :: g -> [x]
- distance :: g -> x -> x -> Int
- minDistance :: g -> [x] -> x -> Int
- size :: g -> s
- neighbours :: g -> x -> [x]
- numNeighbours :: g -> x -> Int
- contains :: g -> x -> Bool
- viewpoint :: g -> x -> [(x, Int)]
- tileCount :: g -> Int
- empty :: g -> Bool
- nonEmpty :: g -> Bool
- edges :: g -> [(x, x)]
- isAdjacent :: Grid g s x => g -> x -> x -> Bool
- adjacentTilesToward :: g -> x -> x -> [x]
- minimalPaths :: g -> x -> x -> [[x]]

- class Grid g s x => BoundedGrid g s x where
- data TriTriGrid
- triTriGrid :: Int -> TriTriGrid
- data ParaTriGrid
- paraTriGrid :: Int -> Int -> ParaTriGrid
- data RectSquareGrid
- rectSquareGrid :: Int -> Int -> RectSquareGrid
- data TorSquareGrid
- torSquareGrid :: Int -> Int -> TorSquareGrid
- data HexHexGrid
- hexHexGrid :: Int -> HexHexGrid
- data ParaHexGrid
- paraHexGrid :: Int -> Int -> ParaHexGrid

# The Grid class

class Eq x => Grid g s x | g -> s, g -> x whereSource

A regular arrangement of tiles.
Minimal complete definition: `indices`

, `distance`

and `size`

.

Returns the indices of all tiles in a grid.

distance :: g -> x -> x -> IntSource

returns the minimum number of moves required
to get from the tile at index `distance`

g a b`a`

to the tile at index `b`

in
grid `g`

, moving between adjacent tiles at each step. (Two tiles
are adjacent if they share an edge.) If `a`

or `b`

are not
contained within `g`

, the result is undefined.

minDistance :: g -> [x] -> x -> IntSource

returns the minimum number of moves
required to get from any of the tiles at indices `minDistance`

g bs a`bs`

to the tile
at index `a`

in grid `g`

, moving between adjacent tiles at each
step. (Two tiles are adjacent if they share an edge.) If `a`

or
any of `bs`

are not contained within `g`

, the result is
undefined.

Returns the dimensions of the grid.
For example, if `g`

is a 4x3 rectangular grid,

would
return `size`

g`(4, 3)`

, while

would return `tileCount`

g`12`

.

neighbours :: g -> x -> [x]Source

returns the indices of the tiles in the grid
`neighbours`

g x`g`

which are adjacent to the tile with index `x`

.

numNeighbours :: g -> x -> IntSource

returns the number of tiles in the grid
`numNeighbours`

g x`g`

which are adjacent to the tile with index `x`

.

contains :: g -> x -> BoolSource

`g `'contains'` x`

returns `True`

if the index `x`

is contained
within the grid `g`

, otherwise it returns false.

viewpoint :: g -> x -> [(x, Int)]Source

returns a list of pairs associating the index
of each tile in `viewpoint`

g x`g`

with its distance to the tile with index `x`

.
If `x`

is not contained within `g`

, the result is undefined.

Returns the number of tiles in a grid. Compare with

.
`size`

Returns `True`

if the number of tiles in a grid is zero, `False`

otherwise.

Returns `False`

if the number of tiles in a grid is zero, `True`

otherwise.

A list of all edges in a grid, where the edges are represented by a pair of indices of adjacent tiles.

isAdjacent :: Grid g s x => g -> x -> x -> BoolSource

returns `isAdjacent`

g a b`True`

if the tile at index `a`

is
adjacent to the tile at index `b`

in `g`

. (Two tiles are adjacent
if they share an edge.) If `a`

or `b`

are not contained within
`g`

, the result is undefined.

adjacentTilesToward :: g -> x -> x -> [x]Source

returns the indices of all tiles
which are neighbours of the tile at index `adjacentTilesToward`

g a b`a`

, and which are
closer to the tile at `b`

than `a`

is. In other words, it returns
the possible next steps on a minimal path from `a`

to `b`

. If `a`

or `b`

are not contained within `g`

, or if there is no path from
`a`

to `b`

(e.g., a disconnected grid), the result is undefined.

minimalPaths :: g -> x -> x -> [[x]]Source

returns a list of all minimal paths from
the tile at index `minimalPaths`

g a b`a`

to the tile at index `b`

in grid `g`

. A
path is a sequence of tiles where each tile in the sequence is
adjacent to the previous one. (Two tiles are adjacent if they
share an edge.) If `a`

or `b`

are not contained within `g`

, the
result is undefined.

Tip: The default implementation of this function calls

. If you want to use a custom algorithm,
consider modifying `adjacentTilesToward`

instead of
`adjacentTilesToward`

.
`minimalPaths`

class Grid g s x => BoundedGrid g s x whereSource

A regular arrangement of tiles with an edge.
Minimal complete definition: `boundary`

.

Returns a the indices of all the tiles at the boundary of a grid, including corner tiles.

isBoundary :: g -> x -> BoolSource

' returns `isBoundary`

g x`True`

if the tile with index `x`

is
on a boundary of `g`

, `False`

otherwise. (Corner tiles are also
boundary tiles.)

Returns the index of the tile(s) that require the maximum number of moves to reach the nearest boundary tile. A grid may have more than one central tile (e.g., a rectangular grid with an even number of rows and columns will have four central tiles).

isCentre :: g -> x -> BoolSource

' returns `isCentre`

g x`True`

if the tile with index `x`

is
a centre tile of `g`

, `False`

otherwise.

BoundedGrid HexHexGrid Int (Int, Int) | |

BoundedGrid TriTriGrid Int (Int, Int) | |

BoundedGrid ParaHexGrid (Int, Int) (Int, Int) | |

BoundedGrid RectSquareGrid (Int, Int) (Int, Int) | |

BoundedGrid ParaTriGrid (Int, Int) (Int, Int) |

# Grids with triangular tiles

data TriTriGrid Source

A triangular grid with triangular tiles. The grid and its indexing scheme are illustrated in the user guide, available at https://github.com/mhwombat/grid/wiki.

triTriGrid :: Int -> TriTriGridSource

returns a triangular grid with sides of
length `triTriGrid`

s`s`

, using triangular tiles. If `s`

is nonnegative, the
resulting grid will have `s^2`

tiles. Otherwise, the resulting grid
will be empty and the list of indices will be null.

data ParaTriGrid Source

A Parallelogrammatical grid with triangular tiles. The grid and its indexing scheme are illustrated in the user guide, available at https://github.com/mhwombat/grid/wiki.

Eq ParaTriGrid | |

Show ParaTriGrid | |

BoundedGrid ParaTriGrid (Int, Int) (Int, Int) | |

Grid ParaTriGrid (Int, Int) (Int, Int) |

paraTriGrid :: Int -> Int -> ParaTriGridSource

returns a grid in the shape of a
parallelogram with `paraTriGrid`

r c`r`

rows and `c`

columns, using triangular
tiles. If `r`

and `c`

are both nonnegative, the resulting grid will
have `2*r*c`

tiles. Otherwise, the resulting grid will be empty and
the list of indices will be null.

# Grids with square tiles

data RectSquareGrid Source

A rectangular grid with square tiles. The grid and its indexing scheme are illustrated in the user guide, available at https://github.com/mhwombat/grid/wiki.

Eq RectSquareGrid | |

Show RectSquareGrid | |

BoundedGrid RectSquareGrid (Int, Int) (Int, Int) | |

Grid RectSquareGrid (Int, Int) (Int, Int) |

rectSquareGrid :: Int -> Int -> RectSquareGridSource

produces a rectangular grid with `rectSquareGrid`

r c`r`

rows
and `c`

columns, using square tiles. If `r`

and `c`

are both
nonnegative, the resulting grid will have `r*c`

tiles. Otherwise,
the resulting grid will be empty and the list of indices will be
null.

data TorSquareGrid Source

A toroidal grid with square tiles. The grid and its indexing scheme are illustrated in the user guide, available at https://github.com/mhwombat/grid/wiki.

torSquareGrid :: Int -> Int -> TorSquareGridSource

returns a toroidal grid with `torSquareGrid`

r c`r`

rows and `c`

columns, using square tiles. If `r`

and `c`

are
both nonnegative, the resulting grid will have `r*c`

tiles. Otherwise,
the resulting grid will be empty and the list of indices will be null.

# Grids with hexagonal tiles

data HexHexGrid Source

A hexagonal grid with hexagonal tiles The grid and its indexing scheme are illustrated in the user guide, available at https://github.com/mhwombat/grid/wiki.

hexHexGrid :: Int -> HexHexGridSource

returns a grid of hexagonal shape, with
sides of length `hexHexGrid`

s`s`

, using hexagonal tiles. If `s`

is nonnegative, the
resulting grid will have `3*s*(s-1) + 1`

tiles. Otherwise, the resulting
grid will be empty and the list of indices will be null.

data ParaHexGrid Source

A parallelogramatical grid with hexagonal tiles The grid and its indexing scheme are illustrated in the user guide, available at https://github.com/mhwombat/grid/wiki.

Eq ParaHexGrid | |

Show ParaHexGrid | |

BoundedGrid ParaHexGrid (Int, Int) (Int, Int) | |

Grid ParaHexGrid (Int, Int) (Int, Int) |

paraHexGrid :: Int -> Int -> ParaHexGridSource

returns a grid in the shape of a
parallelogram with `paraHexGrid`

r c`r`

rows and `c`

columns, using hexagonal tiles. If
`r`

and `c`

are both nonnegative, the resulting grid will have `r*c`

tiles.
Otherwise, the resulting grid will be empty and the list of indices will
be null.

# Example

Create a grid.

ghci> let g = hexHexGrid 3 ghci> indices g [(-2,0),(-2,1),(-2,2),(-1,-1),(-1,0),(-1,1),(-1,2),(0,-2),(0,-1),(0,0),(0,1),(0,2),(1,-2),(1,-1),(1,0),(1,1),(2,-2),(2,-1),(2,0)]

Find out the minimum number of moves to go from one tile in a grid to another tile, moving between adjacent tiles at each step.

ghci> distance g (0,-2) (0,2) 4

Find out the minimum number of moves to go from one tile in a grid to any other tile, moving between adjacent tiles at each step.

ghci> viewpoint g (1,-2) [((-2,0),3),((-2,1),3),((-2,2),4),((-1,-1),2),((-1,0),2),((-1,1),3),((-1,2),4),((0,-2),1),((0,-1),1),((0,0),2),((0,1),3),((0,2),4),((1,-2),0),((1,-1),1),((1,0),2),((1,1),3),((2,-2),1),((2,-1),2),((2,0),3)]

Find out which tiles are adjacent to a particular tile.

ghci> neighbours g (-1,1) [(-2,1),(-2,2),(-1,2),(0,1),(0,0),(-1,0)]

Find out if a tile is within the grid boundary.

ghci> g `contains` (0,0) True ghci> g `contains` (0,12) False

Find out the physical dimensions of the grid.

ghci> size g 3

Find out the number of tiles in the grid.

ghci> tileCount g 19

Check if a grid is empty (contains no tiles).

ghci> empty g False ghci> nonEmpty g True

Find all of the minimal paths between two points.

ghci> let g = hexHexGrid 3 ghci> minimalPaths g (0,0) (2,-1) [[(0,0),(1,0),(2,-1)],[(0,0),(1,-1),(2,-1)]]