add a mutable state class

This commit is contained in:
Chris Sexton 2019-10-05 13:52:07 -04:00
parent 73ce115ea8
commit 3eee55910f
3 changed files with 39 additions and 15 deletions

View File

@ -1,6 +1,5 @@
module Game.Game module Game.Game
type WallType = type WallType =
| NS | NS
| EW | EW
@ -21,6 +20,8 @@ type Env =
| Floor | Floor
| Trap | Trap
| Player | Player
type Board = Env list list
type Cast = { type Cast = {
spell: string spell: string
@ -36,11 +37,6 @@ type Location =
x: int x: int
y: int y: int
} }
type State =
{
board: string
playerLocation: Location
}
let envToString e = let envToString e =
match e with match e with
@ -63,4 +59,37 @@ let floorToString = List.fold (fun f r -> f + rowToString r + "\n") ""
let rowToStringList = List.map envToString let rowToStringList = List.map envToString
let floorToStringLists = List.map rowToStringList let floorToStringLists = List.map rowToStringList
let newBoard() = [
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ];
[ Wall NS; Floor; Floor; Floor; Wall NS ];
[ Wall NS; Floor; Floor; Floor; Wall NS ];
[ Wall NS; Floor; Floor; Floor; Wall NS ];
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ]
]
type StateJSON =
{
board: string
playerLocation: Location
}
type State(board: Board) =
member this.boardString = floorToString board
member this.playerLocation = { x = 1; y = 1 }
member this.JSON = {
board = this.boardString
playerLocation = this.playerLocation
}
member this.cmd cmd =
match cmd with
| Move d ->
match d with
| North -> this.playerLocation = { x = this.playerLocation.x + 1; y = this.playerLocation.y }
| South -> this.playerLocation = { x = this.playerLocation.x - 1; y = this.playerLocation.y }
| East -> this.playerLocation = { x = this.playerLocation.x; y = this.playerLocation.y + 1 }
| West -> this.playerLocation = { x = this.playerLocation.x; y = this.playerLocation.y - 1 }
| _ -> true
let mutable Current = State(newBoard())

View File

@ -31,17 +31,11 @@ let mapHandler() =
[ Wall NS; Floor; Floor; Floor; Wall NS ]; [ Wall NS; Floor; Floor; Floor; Wall NS ];
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ] [ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ]
] ]
let state = { json Current.JSON
board = floorToString floor
playerLocation = { x = 1; y = 1 }
}
json state
type Direction = string
let moveHandler (next: HttpFunc) (ctx: HttpContext) = let moveHandler (next: HttpFunc) (ctx: HttpContext) =
task { task {
let! direction = ctx.BindJsonAsync<Direction>() let! cmd = ctx.BindJsonAsync<Command>()
return! (mapHandler()) next ctx return! (mapHandler()) next ctx
} }

View File

@ -29,6 +29,7 @@ module Views =
let display() = let display() =
div [ _id "app" ] [ div [ _id "app" ] [
div [ _id "game" ] [] div [ _id "game" ] []
button [ _id "south"; _value "south" ] [ Text "South" ]
] ]
let index() = let index() =