From 3eee55910f003f2c6576d5a771544a33d36e4f25 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sat, 5 Oct 2019 13:52:07 -0400 Subject: [PATCH] add a mutable state class --- src/Game/Library.fs | 43 ++++++++++++++++++++++++++++++++++++------- src/Server/Program.fs | 10 ++-------- src/Server/Views.fs | 1 + 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/Game/Library.fs b/src/Game/Library.fs index fb5c0d1..bc21dc6 100644 --- a/src/Game/Library.fs +++ b/src/Game/Library.fs @@ -1,6 +1,5 @@ module Game.Game - type WallType = | NS | EW @@ -21,6 +20,8 @@ type Env = | Floor | Trap | Player + +type Board = Env list list type Cast = { spell: string @@ -36,11 +37,6 @@ type Location = x: int y: int } -type State = - { - board: string - playerLocation: Location - } let envToString e = match e with @@ -63,4 +59,37 @@ let floorToString = List.fold (fun f r -> f + rowToString r + "\n") "" let rowToStringList = List.map envToString -let floorToStringLists = List.map rowToStringList \ No newline at end of file +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()) diff --git a/src/Server/Program.fs b/src/Server/Program.fs index c476c7e..bcf1621 100644 --- a/src/Server/Program.fs +++ b/src/Server/Program.fs @@ -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() + let! cmd = ctx.BindJsonAsync() return! (mapHandler()) next ctx } diff --git a/src/Server/Views.fs b/src/Server/Views.fs index 4b9492c..d1bd08d 100644 --- a/src/Server/Views.fs +++ b/src/Server/Views.fs @@ -29,6 +29,7 @@ module Views = let display() = div [ _id "app" ] [ div [ _id "game" ] [] + button [ _id "south"; _value "south" ] [ Text "South" ] ] let index() =