snengame/game.go

269 lines
5.8 KiB
Go
Raw Normal View History

2021-07-15 19:26:57 -04:00
package main
import (
2021-07-16 14:53:45 -04:00
"fmt"
2021-07-15 19:26:57 -04:00
"strconv"
"strings"
)
2021-07-16 14:53:45 -04:00
type GameStatus int
const (
StatusLobby = iota
StatusReady
StatusPlaying
StatusStop
StatusSentinalWin
StatusScourgeWin
)
const (
SentinalID = 1
ScourgeID = 2
2021-07-16 14:53:45 -04:00
)
func (g GameStatus) String() string {
return []string{"Lobby", "Ready", "Playing", "Stopped"}[g]
}
2021-07-15 19:26:57 -04:00
type Game struct {
GameBoard *Board
SentinalPlayer *Player
ScourgePlayer *Player
2021-07-16 14:53:45 -04:00
SentinalDeck *Deck
ScourgeDeck *Deck
2021-07-15 19:26:57 -04:00
CurrentTurn int
2021-07-16 14:53:45 -04:00
CardBuffer []Card
2021-07-15 19:26:57 -04:00
CanDraw bool
HasDrawn bool
2021-07-16 14:53:45 -04:00
Status GameStatus
2021-07-15 19:26:57 -04:00
}
func NewGame() *Game {
deckA := NewDeck()
deckB := NewDeck()
deckA.Shuffle()
deckB.Shuffle()
return &Game{
GameBoard: NewBoard(),
SentinalPlayer: NewPlayer("Sentinal", SentinalID),
ScourgePlayer: NewPlayer("Scourge", ScourgeID),
2021-07-16 14:53:45 -04:00
SentinalDeck: deckA,
ScourgeDeck: deckB,
2021-07-15 19:26:57 -04:00
CurrentTurn: 0, //start with no turn
2021-07-16 14:53:45 -04:00
CardBuffer: []Card{},
2021-07-15 19:26:57 -04:00
CanDraw: false,
HasDrawn: false,
2021-07-16 14:53:45 -04:00
Status: StatusLobby,
2021-07-15 19:26:57 -04:00
}
}
2021-07-16 14:53:45 -04:00
func (g *Game) String() string {
return fmt.Sprintf("Sen(%v): %v\n\n%v\n\nSco(%v): %v\nStatus:%v Draw:%v Turn:%v\n%v\n", g.SentinalPlayer.Life, g.SentinalPlayer.Hand, g.GameBoard, g.ScourgePlayer.Life, g.ScourgePlayer.Hand, g.Status, g.CanDraw, g.CurrentTurn, g.CardBuffer)
2021-07-16 14:53:45 -04:00
}
2021-07-22 13:09:56 -04:00
func (g *Game) Parse(cmd *Command) *CommandResult {
var state_res *GameView
var action_res []Card
var debug_res *Game
var res_type CmdType
if cmd.Type == ActCmd {
action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd)
res_type = ActCmd
} else if cmd.Type == StateCmd {
state_res, debug_res = g.PlayerStateAct(cmd.PlayerID, cmd.Cmd)
res_type = StateCmd
}
return &CommandResult{
PlayerID: cmd.PlayerID,
ResultType: res_type,
StateResult: state_res,
ActionResult: action_res,
DebugResult: debug_res,
}
}
func (g *Game) PlayerStateAct(id int, cmd string) (*GameView, *Game) {
2021-07-15 20:58:21 -04:00
switch cmd {
2021-07-16 14:53:45 -04:00
case "d":
//debug game state
2021-07-22 13:09:56 -04:00
return nil, g
2021-07-16 14:53:45 -04:00
case "g":
//game state
2021-07-22 13:09:56 -04:00
return NewView(id, g), nil
2021-07-16 14:53:45 -04:00
case "b":
//begin game
g.Status = StatusReady
//TODO check for ready on both accounts first
g.SentinalPlayer.Hand = g.SentinalDeck.Cards[len(g.SentinalDeck.Cards)-5 : len(g.SentinalDeck.Cards)]
g.SentinalDeck.Cards = g.SentinalDeck.Cards[0 : len(g.SentinalDeck.Cards)-5]
g.ScourgePlayer.Hand = g.ScourgeDeck.Cards[len(g.ScourgeDeck.Cards)-5 : len(g.ScourgeDeck.Cards)]
g.SentinalDeck.Cards = g.ScourgeDeck.Cards[0 : len(g.ScourgeDeck.Cards)-5]
2021-07-15 20:58:21 -04:00
case "s":
//start turn
if g.Status == StatusReady { //first turn
2021-07-16 14:53:45 -04:00
g.Status = StatusPlaying
g.CurrentTurn = id
g.CanDraw = false
2021-07-22 13:09:56 -04:00
g.HasDrawn = true
2021-07-16 15:53:02 -04:00
} else {
g.CanDraw = true
g.HasDrawn = false
2021-07-16 14:53:45 -04:00
}
2021-07-15 20:58:21 -04:00
if id != g.CurrentTurn {
2021-07-22 13:09:56 -04:00
return nil, nil
2021-07-15 20:58:21 -04:00
}
if id == SentinalID {
2021-07-15 21:44:45 -04:00
for _, v := range g.GameBoard.Sentinal {
v.Upkeep(g)
}
} else {
for _, v := range g.GameBoard.Scourge {
v.Upkeep(g)
}
}
2021-07-15 20:58:21 -04:00
case "e":
//end turn and clean up
2021-07-15 21:44:45 -04:00
if id != g.CurrentTurn {
2021-07-22 13:09:56 -04:00
return nil, nil
2021-07-15 21:44:45 -04:00
}
2021-07-16 14:53:45 -04:00
g.CardBuffer = []Card{}
if id == SentinalID {
2021-07-15 21:44:45 -04:00
for _, v := range g.GameBoard.Sentinal {
v.Endstep(g)
}
} else {
for _, v := range g.GameBoard.Scourge {
v.Endstep(g)
}
}
if g.CurrentTurn == SentinalID {
g.CurrentTurn = ScourgeID
2021-07-15 20:58:21 -04:00
} else {
g.CurrentTurn = SentinalID
2021-07-15 20:58:21 -04:00
}
}
2021-07-22 13:09:56 -04:00
return NewView(id, g), nil
2021-07-15 20:58:21 -04:00
}
2021-07-15 19:26:57 -04:00
func (g *Game) PlayerAct(id int, cmd string) []Card {
if id != g.CurrentTurn {
return nil
}
var curr *Player
var opp *Player
var currD *Deck
if id == g.SentinalPlayer.Id {
curr = g.SentinalPlayer
opp = g.ScourgePlayer
2021-07-16 14:53:45 -04:00
currD = g.SentinalDeck
2021-07-15 19:26:57 -04:00
} else {
curr = g.ScourgePlayer
opp = g.SentinalPlayer
2021-07-16 14:53:45 -04:00
currD = g.ScourgeDeck
2021-07-15 19:26:57 -04:00
}
cmd_s := strings.Split(cmd, " ")
if len(cmd_s) < 1 {
return nil
}
switch cmd_s[0] {
case "s":
//scry: return scry options off top of deck
if !g.CanDraw || len(g.CardBuffer) > 0 {
2021-07-15 20:58:21 -04:00
return nil
}
2021-07-16 14:53:45 -04:00
g.CardBuffer = currD.Scry(curr.Life)
return g.CardBuffer
2021-07-15 19:26:57 -04:00
case "d":
//draw: return player hand
2021-07-16 14:53:45 -04:00
if !g.CanDraw {
return nil
}
2021-07-15 20:58:21 -04:00
if len(cmd_s) != 2 || !g.CanDraw {
2021-07-15 19:26:57 -04:00
return nil
}
x_i, err := strconv.Atoi(cmd_s[1])
if err != nil {
panic(err)
}
2021-07-16 14:53:45 -04:00
x := g.CardBuffer[x_i]
buf := g.CardBuffer
2021-07-15 19:26:57 -04:00
for i, v := range buf {
if v == x {
buf = append(buf[:i], buf[i+1:]...)
}
}
currD.Bottom(buf)
2021-07-16 14:53:45 -04:00
curr.Hand = append(curr.Hand, x)
g.CanDraw = false
g.HasDrawn = true
2021-07-16 14:53:45 -04:00
return curr.Hand
2021-07-15 19:26:57 -04:00
case "m":
//move: return player board or [] if invalid
if len(cmd_s) != 3 {
return nil
}
if !g.HasDrawn {
return nil
}
2021-07-15 19:26:57 -04:00
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
res := g.GameBoard.Move(g.CurrentTurn, x_i, y_i)
if res {
return g.GameBoard.GetRow(g.CurrentTurn)
} else {
return []Card{}
}
case "a":
//attack
if len(cmd_s) != 3 {
return nil
}
if !g.HasDrawn {
return nil
}
2021-07-15 19:26:57 -04:00
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
res := g.GameBoard.Attack(g.CurrentTurn, x_i, y_i)
if res == 1 {
opp.Life = opp.Life - 1
return g.GameBoard.GetRow(g.CurrentTurn)
} else if res == 0 {
return g.GameBoard.GetRow(g.CurrentTurn)
} else {
fmt.Println("can't attack")
2021-07-15 19:26:57 -04:00
return []Card{}
}
case "p":
//play: return player boad or [] if invalid
2021-07-16 14:53:45 -04:00
if len(cmd_s) != 3 {
fmt.Println("not enough arguments")
2021-07-15 19:26:57 -04:00
return nil
}
if !g.HasDrawn {
return nil
}
2021-07-15 19:26:57 -04:00
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
2021-07-15 21:44:45 -04:00
card := curr.Hand[x_i]
if g.GameBoard.CanPlay(g.CurrentTurn, &card, y_i) {
curr.Hand[x_i].Cast(g)
}
res := g.GameBoard.Play(g.CurrentTurn, &card, y_i)
2021-07-15 19:26:57 -04:00
if res {
2021-07-15 21:44:45 -04:00
curr.Hand[x_i].Enters(g)
2021-07-15 20:58:21 -04:00
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
2021-07-15 19:26:57 -04:00
return g.GameBoard.GetRow(g.CurrentTurn)
} else {
fmt.Println("couldn't play")
2021-07-15 19:26:57 -04:00
return []Card{}
}
default:
fmt.Println("Invalid act command")
2021-07-15 19:26:57 -04:00
return nil
}
return nil
}