snengame/game.go
2021-07-15 20:58:21 -04:00

156 lines
3.0 KiB
Go

package main
import (
"strconv"
"strings"
)
type Game struct {
GameBoard *Board
SentinalPlayer *Player
ScourgePlayer *Player
sentinalDeck *Deck
scourgeDeck *Deck
CurrentTurn int
cardBuffer []Card
CanDraw bool
}
func NewGame() *Game {
deckA := NewDeck()
deckB := NewDeck()
deckA.Shuffle()
deckB.Shuffle()
return &Game{
GameBoard: NewBoard(),
SentinalPlayer: NewPlayer("Sentinal", 1),
ScourgePlayer: NewPlayer("Scourge", 2),
sentinalDeck: deckA,
scourgeDeck: deckB,
CurrentTurn: 0, //start with no turn
cardBuffer: []Card{},
CanDraw: false,
}
}
func (g *Game) PlayerStateAct(id int, cmd string) *Game {
switch cmd {
case "i":
//info
return g
case "s":
//start turn
if id != g.CurrentTurn {
return nil
}
g.CanDraw = true
return g
case "e":
//end turn and clean up
g.cardBuffer = []Card{}
if g.CurrentTurn == 1 {
g.CurrentTurn = 2
} else {
g.CurrentTurn = 1
}
return g
}
return nil
}
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
currD = g.sentinalDeck
} else {
curr = g.ScourgePlayer
opp = g.SentinalPlayer
currD = g.scourgeDeck
}
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 {
return nil
}
g.cardBuffer = currD.Scry(curr.Life)
return g.cardBuffer
case "d":
//draw: return player hand
if len(cmd_s) != 2 || !g.CanDraw {
return nil
}
x_i, err := strconv.Atoi(cmd_s[1])
if err != nil {
panic(err)
}
x := g.cardBuffer[x_i]
buf := g.cardBuffer
for i, v := range buf {
if v == x {
buf = append(buf[:i], buf[i+1:]...)
}
}
currD.Bottom(buf)
return []Card{x}
case "m":
//move: return player board or [] if invalid
if len(cmd_s) != 3 {
return nil
}
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
}
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 {
return []Card{}
}
case "p":
//play: return player boad or [] if invalid
if len(cmd_s) != 2 {
return nil
}
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
res := g.GameBoard.Play(g.CurrentTurn, curr.Hand[x_i], y_i)
if res {
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
return g.GameBoard.GetRow(g.CurrentTurn)
} else {
return []Card{}
}
default:
return nil
}
return nil
}