Skip to content

Commit

Permalink
Add barriers, randomly placed on screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hendrix and Yulia Tolskaya committed Jun 18, 2015
1 parent 938f104 commit e53fb2f
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 31 deletions.
1 change: 1 addition & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/Native/Now.js
Original file line number Diff line number Diff line change
@@ -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()
};

};
6 changes: 6 additions & 0 deletions src/Now.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Now where

import Native.Now

loadTime : Float
loadTime = Native.Now.loadTime
53 changes: 42 additions & 11 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -10,7 +15,7 @@ type alias GameMap =
, currentPlayerLocation : Location
}

type alias Board = List (List Cell)
type alias Board = Array (Array Cell)

type Player = Player

Expand All @@ -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
}
Expand Down
11 changes: 5 additions & 6 deletions src/Rogue/Update.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Rogue.Update where

import Rogue.Model exposing (..)
import Maybe

update : Input -> Game -> Game
update i game =
Expand All @@ -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
if | within board newLocation && openness -> { gameMap | currentPlayerLocation <- newLocation }
| otherwise -> gameMap
28 changes: 15 additions & 13 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 List exposing (..)
import Array exposing (..)

import Rogue.Model exposing (..)

Expand All @@ -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"
17 changes: 16 additions & 1 deletion test/RogueTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,4 +53,16 @@ test_updateGameMap_does_not_move_outside_of_bounds =
, test "Down" (
assertEqual gameMapAtBottomRight.currentPlayerLocation (updateGameMap down gameMapAtBottomRight).currentPlayerLocation
)
]
]

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
)

0 comments on commit e53fb2f

Please sign in to comment.