add a mutable state class
This commit is contained in:
parent
73ce115ea8
commit
3eee55910f
|
@ -1,6 +1,5 @@
|
|||
module Game.Game
|
||||
|
||||
|
||||
type WallType =
|
||||
| NS
|
||||
| EW
|
||||
|
@ -22,6 +21,8 @@ type Env =
|
|||
| Trap
|
||||
| Player
|
||||
|
||||
type Board = Env list list
|
||||
|
||||
type Cast = {
|
||||
spell: string
|
||||
direction: Direction
|
||||
|
@ -36,11 +37,6 @@ type Location =
|
|||
x: int
|
||||
y: int
|
||||
}
|
||||
type State =
|
||||
{
|
||||
board: string
|
||||
playerLocation: Location
|
||||
}
|
||||
|
||||
let envToString e =
|
||||
match e with
|
||||
|
@ -64,3 +60,36 @@ let floorToString = List.fold (fun f r -> f + rowToString r + "\n") ""
|
|||
let rowToStringList = List.map envToString
|
||||
|
||||
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())
|
||||
|
|
|
@ -31,17 +31,11 @@ let mapHandler() =
|
|||
[ Wall NS; Floor; Floor; Floor; Wall NS ];
|
||||
[ Wall Corner; Wall EW; Wall EW; Wall EW; Wall Corner ]
|
||||
]
|
||||
let state = {
|
||||
board = floorToString floor
|
||||
playerLocation = { x = 1; y = 1 }
|
||||
}
|
||||
json state
|
||||
|
||||
type Direction = string
|
||||
json Current.JSON
|
||||
|
||||
let moveHandler (next: HttpFunc) (ctx: HttpContext) =
|
||||
task {
|
||||
let! direction = ctx.BindJsonAsync<Direction>()
|
||||
let! cmd = ctx.BindJsonAsync<Command>()
|
||||
return! (mapHandler()) next ctx
|
||||
}
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@ module Views =
|
|||
let display() =
|
||||
div [ _id "app" ] [
|
||||
div [ _id "game" ] []
|
||||
button [ _id "south"; _value "south" ] [ Text "South" ]
|
||||
]
|
||||
|
||||
let index() =
|
||||
|
|
Loading…
Reference in New Issue