From 1f58cd2c03b52b38b7f6eecae453783485ea3a5b Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sat, 5 Oct 2019 14:20:24 -0400 Subject: [PATCH] fix coord system and class --- src/Game/Library.fs | 22 ++++++++++++---------- src/GameTest/UnitTest1.fs | 10 +++++++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Game/Library.fs b/src/Game/Library.fs index bc21dc6..aac59f6 100644 --- a/src/Game/Library.fs +++ b/src/Game/Library.fs @@ -20,13 +20,13 @@ type Env = | Floor | Trap | Player - + type Board = Env list list - + type Cast = { spell: string direction: Direction -} + } type Command = | Move of Direction | Refresh @@ -76,20 +76,22 @@ type StateJSON = } type State(board: Board) = + let mutable _playerLocation = { x = 1; y = 1 } member this.boardString = floorToString board - member this.playerLocation = { x = 1; y = 1 } + member this.playerLocation = _playerLocation member this.JSON = { board = this.boardString - playerLocation = this.playerLocation + playerLocation = _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 + // Note that the coordinate system is pinned to NW + | North -> _playerLocation <- { x = _playerLocation.x; y = _playerLocation.y - 1 } + | South -> _playerLocation <- { x = _playerLocation.x; y = _playerLocation.y + 1 } + | East -> _playerLocation <- { x = _playerLocation.x + 1; y = _playerLocation.y } + | West -> _playerLocation <- { x = _playerLocation.x - 1; y = _playerLocation.y } + | _ -> () let mutable Current = State(newBoard()) diff --git a/src/GameTest/UnitTest1.fs b/src/GameTest/UnitTest1.fs index 320d103..d6f1340 100644 --- a/src/GameTest/UnitTest1.fs +++ b/src/GameTest/UnitTest1.fs @@ -16,4 +16,12 @@ let TestFloorToString() = ] let actual = floorToString floor let expected = "+-+\n|.|\n+-+\n" - Assert.AreEqual(actual, expected) \ No newline at end of file + Assert.AreEqual(expected, actual) + +[] +let TestStateMovement() = + let current = State(newBoard()) + let expected = 2 + current.cmd (Move South) |> ignore + let actual = current.playerLocation.y + Assert.AreEqual(expected, actual) \ No newline at end of file