From cd01b838b2f3619675582046d318be9e4d721617 Mon Sep 17 00:00:00 2001 From: Chris Hendrix and Sean Brady Date: Fri, 19 Jun 2015 14:13:06 -0700 Subject: [PATCH] Extract matrixMapWithIndex function --- src/Rogue/Model.elm | 18 ++++++++++++------ src/Rogue/Update.elm | 8 +++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Rogue/Model.elm b/src/Rogue/Model.elm index b603a9b..b607cd5 100644 --- a/src/Rogue/Model.elm +++ b/src/Rogue/Model.elm @@ -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 @@ -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 diff --git a/src/Rogue/Update.elm b/src/Rogue/Update.elm index 96f1339..66aa01e 100644 --- a/src/Rogue/Update.elm +++ b/src/Rogue/Update.elm @@ -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 =