Orasund / elm-game-essentials / Grid.Bordered

DEPRECATED. Use Grid instead.

A grid with a hard border around the edges. you cant read or write from squares outside the border. Here is an example where such a grid is used: Space Invaders.

Error


type Error
    = OutOfBounds
    | NotSuccessful

Possible Errors.

ignoringErrors : (Grid a -> Result Error (Grid a)) -> Grid a -> Grid a

Trys modifying a grid and if its fails it returns the original grid.

Please be aware, that by using this function you might introduce errors.

Grids


type Grid a

A grid of values.

It has a fixed amount of columns and rows.

If case of a invalid position, it returns an error.

grid |> Grid.insert ( -1, 0 ) virus == Err OutOfBounds

Build

fill : (( Basics.Int, Basics.Int ) -> Maybe a) -> { rows : Basics.Int, columns : Basics.Int } -> Grid a

Create a grid

empty : { rows : Basics.Int, columns : Basics.Int } -> Grid a

Create an empty grid

insert : ( Basics.Int, Basics.Int ) -> a -> Grid a -> Result Error (Grid a)

Insert a value at a position in a grid. Returns a NotSuccessful error if the position is occupied.

update : ( Basics.Int, Basics.Int ) -> (Maybe a -> Result () (Maybe a)) -> Grid a -> Result Error (Grid a)

Update the value of a grid for a specific position with a given function.

remove : ( Basics.Int, Basics.Int ) -> Grid a -> Result Error (Grid a)

Remove a vlaue from a grid. If the position is empty, it returs a NotSuccessful error.

Query

isEmpty : Grid a -> Basics.Bool

Determine if a grid is empty.

member : ( Basics.Int, Basics.Int ) -> Grid a -> Result Error Basics.Bool

Determine if a position is empty. Returns an OutOfBounds Error if the position is invalid.

get : ( Basics.Int, Basics.Int ) -> Grid a -> Result Error (Maybe a)

Get the value associated with a position. If the position is empty, return Nothing. Returns an OutOfBounds Error if the position is invalid.

isValid : ( Basics.Int, Basics.Int ) -> Grid a -> Basics.Bool

checks if a position is valid.

grid : Grid a
grid =
    empty
        { columns=42
        , rows=3
        }


grid |> isValid (-1,0) --> False
grid |> isValid (41,0) --> True
grid |> isValid (42,0) --> False

size : Grid a -> Basics.Int

Determine the number of values in the grid.

dimensions : Grid a -> { columns : Basics.Int, rows : Basics.Int }

return the dimensions of the grid

List

positions : Grid a -> List ( Basics.Int, Basics.Int )

Get all non empty positions in a grid, sorted from lowest to highest.

emptyPositions : Grid a -> List ( Basics.Int, Basics.Int )

Get all empty positions in a grid, sorted from lowest to highest.

values : Grid a -> List a

Get all of the values in a grid, in the order of their keys.

toList : Grid a -> List ( ( Basics.Int, Basics.Int ), a )

Convert a grid into an association list of position-value pairs, sorted by the position.

fromList : { rows : Basics.Int, columns : Basics.Int } -> List ( ( Basics.Int, Basics.Int ), a ) -> Grid a

Convert an association list into a grid.

Dict

toDict : Grid a -> Dict ( Basics.Int, Basics.Int ) a

Convert a grid into an associated dictionary

fromDict : { rows : Basics.Int, columns : Basics.Int } -> Dict ( Basics.Int, Basics.Int ) a -> Grid a

Convert an dictionary to a grid

Transform

map : (( Basics.Int, Basics.Int ) -> Maybe a -> Maybe b) -> Grid a -> Grid b

Apply a function to all positions in a grid.

foldl : (( Basics.Int, Basics.Int ) -> Maybe v -> b -> b) -> b -> Grid v -> b

Fold over all positions in a grid, row by row, from top down.

foldr : (( Basics.Int, Basics.Int ) -> Maybe v -> b -> b) -> b -> Grid v -> b

Fold over all positions in a grid, row by row, from bottum up.

filter : (( Basics.Int, Basics.Int ) -> a -> Basics.Bool) -> Grid a -> Grid a

Keep only the values that pass the given test.

partition : (( Basics.Int, Basics.Int ) -> a -> Basics.Bool) -> Grid a -> ( Grid a, Grid a )

Partition a grid according to some test.

The first grid contains all values which passed the test, and the second contains the values that did not.

find : (( Basics.Int, Basics.Int ) -> a -> Basics.Bool) -> Grid a -> Maybe ( ( Basics.Int, Basics.Int ), a )

Find the first value that passes a given test.

Combine

union : Grid a -> Grid a -> Grid a

Combine two grids. If there is a collision, preference is given to the first grid.

intersect : Grid a -> Grid a -> Grid a

Keep a value when its position appears in the second grid. Preference is given to values in the first grid.

diff : Grid a -> Grid a -> Grid a

Keep a value when its position does not appear in the second grid.