Skip to content

Commit

Permalink
Extract Matrix type alias and some helper methods
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 cd01b83 commit 9d04728
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
41 changes: 17 additions & 24 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import Array exposing (..)
import List exposing (..)
import Maybe exposing (..)
import Random exposing (..)
import Matrix exposing (..)

import Now

type alias Location = (Int, Int)

type alias GameMap = Array (Array Cell)
type alias GameMap = Matrix Cell

type Player = Player

Expand All @@ -18,7 +17,12 @@ type alias Game =
, player : Player
}

type Cell = Open { player : Maybe Player } | Barrier {}
type Item = Item

type Cell
= Open { player : Maybe Player
}
| Barrier {}

type alias Dir =
{ x : Int
Expand All @@ -29,15 +33,6 @@ type alias Input =
{ dir : Dir
}

newBoardWithBarriersAt : Int -> List Location -> Player -> GameMap
newBoardWithBarriersAt size barrierLocations p =
initialize size (
\row -> initialize size (
\col -> if | (row,col) == (0,0) -> Open { player = Just p }
| (row,col) `member` barrierLocations -> Barrier {}
| otherwise -> Open { player = Nothing }))


randomizeLocationsWithin : Int -> Int -> List Location
randomizeLocationsWithin size numLocations =
let
Expand All @@ -47,16 +42,14 @@ randomizeLocationsWithin size numLocations =

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
let
barrierLocations = randomizeLocationsWithin size 11
in
generateMatrix size (
\loc -> if | loc == (0,0) -> Open { player = Just p }
| loc `member` barrierLocations -> Barrier {}
| otherwise -> Open { player = Nothing }
)

defaultGame : Game
defaultGame =
Expand All @@ -72,7 +65,7 @@ currentPlayerLocation : GameMap -> Maybe Location
currentPlayerLocation gameMap =
let
matrixOfLocAndHasPlayer =
matrixMapWithIndex (
mapWithLocation (
\loc cell -> (loc, doesContainPlayer cell)
) gameMap
in
Expand Down
4 changes: 2 additions & 2 deletions src/Rogue/Update.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Rogue.Update where

import Rogue.Model exposing (..)
import Array
import Matrix exposing (..)
import Maybe
import Maybe exposing (andThen)

Expand All @@ -21,7 +21,7 @@ updateGameMap {dir} p gameMap =

updateBoard : Player -> GameMap -> Location -> GameMap
updateBoard p gameMap newPlayerLoc =
matrixMapWithIndex (
mapWithLocation (
\(rowNum,colNum) cell ->
if | (rowNum, colNum) == newPlayerLoc -> insertPerson p cell
| otherwise -> clearCell cell
Expand Down

0 comments on commit 9d04728

Please sign in to comment.