-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up modules for Model/Update/View and add test
- Loading branch information
Chris Hendrix and Yulia Tolskaya
committed
Jun 18, 2015
1 parent
d99f9a3
commit fcc6fc6
Showing
5 changed files
with
156 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module Rogue.Model where | ||
|
||
import List exposing (..) | ||
|
||
type alias Location = (Int, Int) | ||
|
||
type alias GameMap = | ||
{ board : Board | ||
, start : Location | ||
, currentPlayerLocation : Location | ||
} | ||
|
||
type alias Board = List (List Cell) | ||
|
||
type Player = Player | ||
|
||
type alias Game = | ||
{ gameMap : GameMap | ||
, player : Player | ||
} | ||
|
||
type Cell = Open Location | ||
|
||
isAt : Location -> Cell -> Bool | ||
isAt queried (Open current) = queried == current | ||
|
||
newBoard : Int -> Board | ||
newBoard size = | ||
map (\row -> | ||
map | ||
(\col -> Open (row, col) | ||
) [0..(size - 1)]) [0..(size - 1)] | ||
|
||
gameMap : Int -> GameMap | ||
gameMap size = | ||
let startLoc = (0,0) in | ||
{ board = newBoard size | ||
, start = startLoc | ||
, currentPlayerLocation = startLoc | ||
} | ||
|
||
defaultGame : Game | ||
defaultGame = | ||
let | ||
g = gameMap 10 | ||
in | ||
{ gameMap = g | ||
, player = Player | ||
} | ||
|
||
type alias Dir = | ||
{ x : Int | ||
, y : Int | ||
} | ||
|
||
type alias Input = | ||
{ dir : Dir | ||
} | ||
|
||
numRows : Board -> Int | ||
numRows b = length b | ||
|
||
numCols : Board -> Int | ||
numCols = numRows | ||
|
||
within : Board -> Location -> Bool | ||
within board (row, col) = | ||
row >= 0 && row < numRows board && col >= 0 && col < numCols board | ||
|
||
translate : Location -> Dir -> Location | ||
translate (row,col) dir = | ||
(row - dir.y, col + dir.x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Rogue.Update where | ||
|
||
import Rogue.Model exposing (..) | ||
|
||
update : Input -> Game -> Game | ||
update i game = | ||
{ game | gameMap <- updateGameMap i game.gameMap } | ||
|
||
updateGameMap : Input -> GameMap -> GameMap | ||
updateGameMap {dir} ({board,start,currentPlayerLocation} as gameMap) = | ||
let | ||
newLocation = translate currentPlayerLocation dir | ||
in | ||
if within board newLocation | ||
then | ||
{ gameMap | currentPlayerLocation <- newLocation | ||
} | ||
else | ||
gameMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Rogue.View where | ||
|
||
import Color exposing (..) | ||
import Graphics.Collage exposing (..) | ||
import Graphics.Element exposing (..) | ||
import String exposing (join) | ||
import Text | ||
import List exposing (..) | ||
|
||
import Rogue.Model exposing (..) | ||
|
||
view : Game -> Element | ||
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 "" | ||
) | ||
in | ||
map rowifier board | ||
|> join "\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters