From 89c69d60375954b9f24799236932c57ca8c38620 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 22 Jul 2021 13:43:28 -0400 Subject: [PATCH] you can win the game now --- board.go | 18 ++++++++++++++++++ game.go | 40 ++++++++++++++++++++++++++++++++++++++++ main.go | 10 +++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/board.go b/board.go index aeaec99..efe42d4 100644 --- a/board.go +++ b/board.go @@ -26,6 +26,24 @@ func (b *Board) GetRow(id int) []Card { } } +func (b *Board) Empty(id int) bool { + res := true + if id == SentinalID { + for _, v := range b.Sentinal { + if !v.Empty() { + res = false + } + } + } else { + for _, v := range b.Scourge { + if !v.Empty() { + res = false + } + } + } + return res +} + func (b *Board) Move(id, src, dest int) bool { var brd [4]Card if id == 1 { diff --git a/game.go b/game.go index ef42617..55c31dd 100644 --- a/game.go +++ b/game.go @@ -15,6 +15,7 @@ const ( StatusStop StatusSentinalWin StatusScourgeWin + StatusDraw ) const ( @@ -63,6 +64,17 @@ func (g *Game) String() string { } func (g *Game) Parse(cmd *Command) *CommandResult { + + if g.Status != StatusLobby || g.Status != StatusReady || g.Status != StatusPlaying { + return &CommandResult{ + PlayerID: cmd.PlayerID, + ResultType: cmd.Type, + StateResult: NewView(cmd.PlayerID, g), + ActionResult: nil, + DebugResult: g, + } + } + var state_res *GameView var action_res []Card var debug_res *Game @@ -78,6 +90,7 @@ func (g *Game) Parse(cmd *Command) *CommandResult { debug_res = g res_type = DebugCmd } + g.StateChanges() return &CommandResult{ PlayerID: cmd.PlayerID, ResultType: res_type, @@ -87,6 +100,33 @@ func (g *Game) Parse(cmd *Command) *CommandResult { } } +func (g *Game) StateChanges() { + //check for no actions first + if len(g.SentinalPlayer.Hand) == 0 && g.SentinalDeck.Size() == 0 { + if g.GameBoard.Empty(SentinalID) { + g.Status = StatusScourgeWin + } + } + + if len(g.ScourgePlayer.Hand) == 0 && g.ScourgeDeck.Size() == 0 { + if g.GameBoard.Empty(ScourgeID) { + g.Status = StatusSentinalWin + } + } + //check life + //this is second on purpose so that it shadows deck out win conditions + //if you use your last action to win the game, you should actually win + if g.SentinalPlayer.Life < 0 { + g.Status = StatusScourgeWin + } + if g.ScourgePlayer.Life < 0 { + g.Status = StatusSentinalWin + } + if g.ScourgePlayer.Life < 0 && g.SentinalPlayer.Life < 0 { + g.Status = StatusDraw + } +} + func (g *Game) PlayerStateAct(id int, cmd string) *GameView { switch cmd { case "g": diff --git a/main.go b/main.go index 4af21fb..eb5ca83 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,16 @@ import ( "strings" ) +const DEBUG = true + func main() { + if DEBUG { + local() + } + +} + +func local() { reader := bufio.NewReader(os.Stdin) g := NewGame() @@ -54,5 +63,4 @@ func main() { fmt.Println(err) } } - }