snengame/game.go

255 lines
5.5 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
)
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
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", 1),
ScourgePlayer: NewPlayer("Scourge", 2),
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,
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-15 20:58:21 -04:00
func (g *Game) PlayerStateAct(id int, cmd string) *Game {
switch cmd {
2021-07-16 14:53:45 -04:00
case "d":
//debug game state
2021-07-15 20:58:21 -04:00
return g
2021-07-16 14:53:45 -04:00
case "g":
//game state
if id == 1 {
return &Game{
GameBoard: g.GameBoard,
SentinalPlayer: g.SentinalPlayer,
ScourgePlayer: nil,
SentinalDeck: g.SentinalDeck,
ScourgeDeck: nil,
CurrentTurn: g.CurrentTurn,
CardBuffer: nil,
CanDraw: g.CanDraw,
Status: g.Status,
}
} else {
return &Game{
GameBoard: g.GameBoard,
SentinalPlayer: nil,
ScourgePlayer: g.ScourgePlayer,
SentinalDeck: nil,
ScourgeDeck: g.ScourgeDeck,
CurrentTurn: g.CurrentTurn,
CardBuffer: nil,
CanDraw: g.CanDraw,
Status: g.Status,
}
}
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]
return g
2021-07-16 14:53:45 -04:00
2021-07-15 20:58:21 -04:00
case "s":
//start turn
2021-07-16 14:53:45 -04:00
if g.Status == StatusReady {
g.Status = StatusPlaying
g.CurrentTurn = id
g.CanDraw = false
}
2021-07-15 20:58:21 -04:00
if id != g.CurrentTurn {
return nil
}
g.CanDraw = true
2021-07-15 21:44:45 -04:00
if id == 1 {
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
return g
case "e":
//end turn and clean up
2021-07-15 21:44:45 -04:00
if id != g.CurrentTurn {
return nil
}
2021-07-16 14:53:45 -04:00
g.CardBuffer = []Card{}
2021-07-15 21:44:45 -04:00
if id == 1 {
for _, v := range g.GameBoard.Sentinal {
v.Endstep(g)
}
} else {
for _, v := range g.GameBoard.Scourge {
v.Endstep(g)
}
}
2021-07-15 20:58:21 -04:00
if g.CurrentTurn == 1 {
g.CurrentTurn = 2
} else {
g.CurrentTurn = 1
}
return g
}
fmt.Println("invalid state command")
2021-07-15 20:58:21 -04:00
return nil
}
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
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
}
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 {
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
}
fmt.Println("playing")
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
}