Skip to content

Commit

Permalink
Extract matrixMapWithIndex function
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 2d2fb5a commit cd01b83
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
18 changes: 12 additions & 6 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ gameMap : Int -> Player -> GameMap
gameMap size p =
newBoardWithBarriersAt size (randomizeLocationsWithin size 11) p

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

defaultGame : Game
defaultGame =
let
Expand All @@ -63,12 +72,9 @@ currentPlayerLocation : GameMap -> Maybe Location
currentPlayerLocation gameMap =
let
matrixOfLocAndHasPlayer =
Array.indexedMap (
\rowNum row ->
Array.indexedMap (
\colNum cell -> ((rowNum,colNum), doesContainPlayer cell)
) row
) gameMap
matrixMapWithIndex (
\loc cell -> (loc, doesContainPlayer cell)
) gameMap
in
Array.map toList matrixOfLocAndHasPlayer
|> toList
Expand Down
8 changes: 3 additions & 5 deletions src/Rogue/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ updateGameMap {dir} p gameMap =

updateBoard : Player -> GameMap -> Location -> GameMap
updateBoard p gameMap newPlayerLoc =
Array.indexedMap (
\rowNum row -> Array.indexedMap (
\colNum cell ->
matrixMapWithIndex (
\(rowNum,colNum) cell ->
if | (rowNum, colNum) == newPlayerLoc -> insertPerson p cell
| otherwise -> clearCell cell
) row
) gameMap
) gameMap

insertPerson : Player -> Cell -> Cell
insertPerson p c =
Expand Down

0 comments on commit cd01b83

Please sign in to comment.