diff --git a/elm-package.json b/elm-package.json index 44108a9..e62db2b 100644 --- a/elm-package.json +++ b/elm-package.json @@ -8,6 +8,7 @@ "test" ], "exposed-modules": [], + "native-modules": true, "dependencies": { "elm-lang/core": "2.0.1 <= v < 3.0.0", "deadfoxygrandpa/Elm-Test" : "1.0.4 <= v < 2.0.0", diff --git a/src/Native/Now.js b/src/Native/Now.js new file mode 100644 index 0000000..caf5978 --- /dev/null +++ b/src/Native/Now.js @@ -0,0 +1,20 @@ +Elm.Native.Now = {}; + +Elm.Native.Now.make = function(localRuntime) { + + localRuntime.Native = localRuntime.Native || {}; + + + localRuntime.Native.Now = localRuntime.Native.Now || {}; + + if (localRuntime.Native.Now.values) { + return localRuntime.Native.Now.values; + } + + var Result = Elm.Result.make(localRuntime); + + return localRuntime.Native.Now.values = { + loadTime: (new window.Date).getTime() + }; + +}; \ No newline at end of file diff --git a/src/Now.elm b/src/Now.elm new file mode 100644 index 0000000..373c4ee --- /dev/null +++ b/src/Now.elm @@ -0,0 +1,6 @@ +module Now where + +import Native.Now + +loadTime : Float +loadTime = Native.Now.loadTime \ No newline at end of file diff --git a/src/Rogue/Model.elm b/src/Rogue/Model.elm index 990139e..b2d0b64 100644 --- a/src/Rogue/Model.elm +++ b/src/Rogue/Model.elm @@ -1,6 +1,11 @@ module Rogue.Model where -import List exposing (..) +import Array exposing (..) +import List exposing (member) +import Maybe exposing (..) +import Random exposing (..) + +import Now type alias Location = (Int, Int) @@ -10,7 +15,7 @@ type alias GameMap = , currentPlayerLocation : Location } -type alias Board = List (List Cell) +type alias Board = Array (Array Cell) type Player = Player @@ -19,22 +24,48 @@ type alias Game = , player : Player } -type Cell = Open Location +type Cell = Open Location | Barrier Location -isAt : Location -> Cell -> Bool -isAt queried (Open current) = queried == current +isOpen : Cell -> Bool +isOpen cell = + case cell of + (Open _) -> True + (Barrier _) -> False + +cellAt : Location -> Board -> Maybe Cell +cellAt (rowNum,colNum) board = + get rowNum board `andThen` (\row -> get colNum row) +isAt : Location -> Cell -> Bool +isAt queried cell = queried == (loc cell) + +loc : Cell -> Location +loc c = + case c of + Open l -> l + Barrier l -> l + +newBoardWithBarriersAt : Int -> List Location -> Board +newBoardWithBarriersAt size barrierLocations = + initialize size ( + \row -> initialize size ( + \col -> if (row,col) `member` barrierLocations then Barrier (row, col) else Open (row, col))) + newBoard : Int -> Board newBoard size = - map (\row -> - map - (\col -> Open (row, col) - ) [0..(size - 1)]) [0..(size - 1)] + newBoardWithBarriersAt size [] + +randomizeLocationsWithin : Int -> Int -> List Location +randomizeLocationsWithin size numLocations = + let + locationGenerator = list numLocations (pair (int 0 size) (int 0 size)) + in + generate locationGenerator (initialSeed (round Now.loadTime)) |> fst gameMap : Int -> GameMap -gameMap size = +gameMap size = let startLoc = (0,0) in - { board = newBoard size + { board = newBoardWithBarriersAt size (randomizeLocationsWithin size 11) , start = startLoc , currentPlayerLocation = startLoc } diff --git a/src/Rogue/Update.elm b/src/Rogue/Update.elm index 44c9772..1ec5b71 100644 --- a/src/Rogue/Update.elm +++ b/src/Rogue/Update.elm @@ -1,6 +1,7 @@ module Rogue.Update where import Rogue.Model exposing (..) +import Maybe update : Input -> Game -> Game update i game = @@ -10,10 +11,8 @@ updateGameMap : Input -> GameMap -> GameMap updateGameMap {dir} ({board,start,currentPlayerLocation} as gameMap) = let newLocation = translate currentPlayerLocation dir + newCell = cellAt newLocation board + openness = Maybe.withDefault False (Maybe.map isOpen newCell) in - if within board newLocation - then - { gameMap | currentPlayerLocation <- newLocation - } - else - gameMap \ No newline at end of file + if | within board newLocation && openness -> { gameMap | currentPlayerLocation <- newLocation } + | otherwise -> gameMap \ No newline at end of file diff --git a/src/Rogue/View.elm b/src/Rogue/View.elm index 14f6a88..16fd9c6 100644 --- a/src/Rogue/View.elm +++ b/src/Rogue/View.elm @@ -5,7 +5,7 @@ import Graphics.Collage exposing (..) import Graphics.Element exposing (..) import String exposing (join) import Text -import List exposing (..) +import Array exposing (..) import Rogue.Model exposing (..) @@ -15,17 +15,19 @@ view g = leftAligned (Text.monospace (Text.fromString (toString g.gameMap))) toString : GameMap -> String toString {board,start,currentPlayerLocation} = let - rowifier = - (\row -> - map - (\cell -> - if | isAt currentPlayerLocation cell -> "@" - | isAt start cell -> "☐" - | otherwise -> "." - ) - row - |> join "" - ) + rowifier = + (\row -> + Array.map + (\cell -> + if | isAt currentPlayerLocation cell -> "@" + | not <| isOpen cell -> "#" + | otherwise -> "." + ) + row + |> toList + |> join "" + ) in - map rowifier board + Array.map rowifier board + |> toList |> join "\n" \ No newline at end of file diff --git a/test/RogueTest.elm b/test/RogueTest.elm index 9cb0a34..d3db317 100644 --- a/test/RogueTest.elm +++ b/test/RogueTest.elm @@ -7,10 +7,13 @@ import Rogue.Model exposing (..) import Rogue.Update exposing (..) import String + + tests : Test tests = suite "A Test Suite" [ test_updateGameMap_does_not_move_outside_of_bounds , test_update_moves_currentPlayerLocation + , test_updateGameMap_does_not_move_into_barriers ] test_update_moves_currentPlayerLocation : Test @@ -50,4 +53,16 @@ test_updateGameMap_does_not_move_outside_of_bounds = , test "Down" ( assertEqual gameMapAtBottomRight.currentPlayerLocation (updateGameMap down gameMapAtBottomRight).currentPlayerLocation ) - ] \ No newline at end of file + ] + +test_updateGameMap_does_not_move_into_barriers : Test +test_updateGameMap_does_not_move_into_barriers = + let + board = newBoardWithBarriersAt 2 [(0,1)] + game_map = GameMap board (0,0) (0,0) + right = Input { x =1, y = 0 } + in + test "Update does not move into barriers" ( + assertEqual game_map.currentPlayerLocation (updateGameMap right game_map).currentPlayerLocation + ) + \ No newline at end of file