Skip to content

Commit

Permalink
Extract Matrix.elm library functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hendrix and Sean Brady committed Jun 19, 2015
1 parent 9d04728 commit 647606f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
41 changes: 41 additions & 0 deletions src/Matrix.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Matrix where

import Array
import List
import Maybe exposing (..)

type alias Matrix a = Array.Array (Array.Array a)
type alias Location = (Int, Int)

initialize : Int -> (Location -> a) -> Matrix a
initialize size f =
Array.initialize size (
\row -> Array.initialize size (
\col -> f (row,col)))

map : (a -> b) -> Matrix a -> Matrix b
map f m =
Array.map (Array.map f) m

mapWithLocation : (Location -> a -> b) -> Matrix a -> Matrix b
mapWithLocation f m =
Array.indexedMap (
\rowNum row -> Array.indexedMap (
\colNum cell ->
f (rowNum, colNum) cell
) row
) m

toList : Matrix a -> List (List a)
toList m =
Array.map Array.toList m
|> Array.toList

flatten : Matrix a -> List a
flatten m =
List.concat <| toList m


cellAt : Matrix a -> Location -> Maybe a
cellAt m (rowNum, colNum) =
Array.get rowNum m `andThen` Array.get colNum
12 changes: 3 additions & 9 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Rogue.Model where

import Array exposing (..)
import List exposing (..)
import Maybe exposing (..)
import Random exposing (..)
Expand Down Expand Up @@ -45,7 +44,7 @@ gameMap size p =
let
barrierLocations = randomizeLocationsWithin size 11
in
generateMatrix size (
Matrix.initialize size (
\loc -> if | loc == (0,0) -> Open { player = Just p }
| loc `member` barrierLocations -> Barrier {}
| otherwise -> Open { player = Nothing }
Expand All @@ -69,9 +68,8 @@ currentPlayerLocation gameMap =
\loc cell -> (loc, doesContainPlayer cell)
) gameMap
in
Array.map toList matrixOfLocAndHasPlayer
|> toList
|> concat
matrixOfLocAndHasPlayer
|> flatten
|> List.filter snd
|> List.map fst
|> head
Expand All @@ -91,7 +89,3 @@ isJust m =
translate : Dir -> Location -> Location
translate dir (row,col)=
(row - dir.y, col + dir.x)

cellAt : GameMap -> Location -> Maybe Cell
cellAt gameMap (rowNum, colNum) =
Array.get rowNum gameMap `andThen` get colNum
17 changes: 5 additions & 12 deletions src/Rogue/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import String exposing (join)
import Text
import Array exposing (..)
import Matrix exposing (..)

import Rogue.Model exposing (..)

Expand All @@ -14,17 +14,10 @@ view g = viewGameMap g.gameMap

viewGameMap : GameMap -> Element
viewGameMap gameMap =
let
rowifier =
(\row ->
Array.map viewCell row
|> toList
|> flow right
)
in
Array.map rowifier gameMap
|> toList
|> flow down
Matrix.map viewCell gameMap
|> Matrix.toList
|> List.map (flow right)
|> flow down

viewCell : Cell -> Element
viewCell c =
Expand Down

0 comments on commit 647606f

Please sign in to comment.