just use full debug info for now

This commit is contained in:
stryan 2021-07-16 14:53:45 -04:00
parent 8062c99ee6
commit c111e09ab5
5 changed files with 155 additions and 35 deletions

View File

@ -1,5 +1,7 @@
package main package main
import "fmt"
type Board struct { type Board struct {
Sentinal [4]Card Sentinal [4]Card
Scourge [4]Card Scourge [4]Card
@ -7,11 +9,15 @@ type Board struct {
func NewBoard() *Board { func NewBoard() *Board {
return &Board{ return &Board{
Sentinal: [4]Card{nil, nil, nil, nil}, Sentinal: [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)},
Scourge: [4]Card{nil, nil, nil, nil}, Scourge: [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)},
} }
} }
func (b *Board) String() string {
return fmt.Sprintf("---------\n|%v|%v|%v|%v|\n---------\n|%v|%v|%v|%v|\n---------\n", b.Sentinal[0], b.Sentinal[1], b.Sentinal[2], b.Sentinal[3], b.Scourge[0], b.Scourge[1], b.Scourge[2], b.Scourge[3])
}
func (b *Board) GetRow(id int) []Card { func (b *Board) GetRow(id int) []Card {
if id == 1 { if id == 1 {
return b.Sentinal[:] return b.Sentinal[:]
@ -27,14 +33,14 @@ func (b *Board) Move(id, src, dest int) bool {
} else { } else {
brd = b.Scourge brd = b.Scourge
} }
if brd[src] == nil { if brd[src].Empty() {
return false return false
} }
if brd[dest] != nil { if !brd[dest].Empty() {
return false return false
} }
brd[dest] = brd[src] brd[dest] = brd[src]
brd[src] = nil brd[src] = NewCard(-1)
if id == 1 { if id == 1 {
b.Sentinal = brd b.Sentinal = brd
} else { } else {
@ -50,7 +56,7 @@ func (b *Board) CanPlay(id int, c *Card, dest int) bool {
} else { } else {
brd = b.Scourge brd = b.Scourge
} }
if brd[dest] != nil { if !brd[dest].Empty() {
return false return false
} }
return true return true
@ -63,7 +69,7 @@ func (b *Board) Play(id int, c *Card, dest int) bool {
} else { } else {
brd = b.Scourge brd = b.Scourge
} }
if brd[dest] != nil { if !brd[dest].Empty() {
return false return false
} }
brd[dest] = *c brd[dest] = *c
@ -85,11 +91,11 @@ func (b *Board) Attack(id int, atk, def int) int {
aBrd = b.Scourge aBrd = b.Scourge
dBrd = b.Sentinal dBrd = b.Sentinal
} }
if aBrd[atk] == nil || !aBrd[atk].CanAttack(atk, def) { if aBrd[atk].Empty() || !aBrd[atk].CanAttack(atk, def) {
return -1 return -1
} }
aBrd[atk].Acted() aBrd[atk].Acted()
if dBrd[def] == nil { if dBrd[def].Empty() {
//health damage //health damage
if id == 1 { if id == 1 {
b.Sentinal = aBrd b.Sentinal = aBrd
@ -122,7 +128,7 @@ func (b *Board) Attack(id int, atk, def int) int {
} }
} }
if attacker > defender { if attacker > defender {
dBrd[def] = nil dBrd[def] = NewCard(-1)
if id == 1 { if id == 1 {
b.Sentinal = aBrd b.Sentinal = aBrd
b.Scourge = dBrd b.Scourge = dBrd
@ -133,8 +139,8 @@ func (b *Board) Attack(id int, atk, def int) int {
return 0 return 0
} }
if attacker == defender { if attacker == defender {
aBrd[atk] = nil aBrd[atk] = NewCard(-1)
dBrd[def] = nil dBrd[def] = NewCard(-1)
if id == 1 { if id == 1 {
b.Sentinal = aBrd b.Sentinal = aBrd
b.Scourge = dBrd b.Scourge = dBrd

37
card.go
View File

@ -1,5 +1,7 @@
package main package main
import "fmt"
type Card interface { type Card interface {
Cast(g *Game) *Game Cast(g *Game) *Game
Upkeep(g *Game) *Game Upkeep(g *Game) *Game
@ -7,6 +9,8 @@ type Card interface {
Enters(g *Game) *Game Enters(g *Game) *Game
Value() CardValue Value() CardValue
Acted() Acted()
Empty() bool
String() string
CanAttack(int, int) bool CanAttack(int, int) bool
} }
@ -38,6 +42,14 @@ func (g *GenericCard) Acted() {
g.Sick = true g.Sick = true
} }
func (g *GenericCard) Empty() bool {
return false
}
func (g *GenericCard) String() string {
return fmt.Sprintf("%v", g.Val)
}
func (g *GenericCard) CanAttack(x, y int) bool { func (g *GenericCard) CanAttack(x, y int) bool {
if x == y && !g.Sick { if x == y && !g.Sick {
return true return true
@ -58,13 +70,26 @@ const (
Seven Seven
Eight Eight
) )
const (
EmptyValue CardValue = -1
)
func (c CardValue) String() string { func (c CardValue) String() string {
if c == -1 {
return " "
}
return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8"}[c] return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8"}[c]
} }
func NewCard(v int) Card { func NewCard(v int) Card {
switch v { switch v {
case -1:
return &EmptyCard{
&GenericCard{
Val: EmptyValue,
Sick: false,
},
}
case 1: case 1:
return &AceCard{ return &AceCard{
&GenericCard{ &GenericCard{
@ -102,6 +127,14 @@ func NewCard(v int) Card {
} }
} }
type EmptyCard struct {
*GenericCard
}
func (e *EmptyCard) Empty() bool {
return true
}
type AceCard struct { type AceCard struct {
*GenericCard *GenericCard
} }
@ -148,8 +181,8 @@ type Valkyrie struct {
} }
func (v *Valkyrie) Cast(g *Game) *Game { func (v *Valkyrie) Cast(g *Game) *Game {
g.GameBoard.Sentinal = [4]Card{nil, nil, nil, nil} g.GameBoard.Sentinal = [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)}
g.GameBoard.Scourge = [4]Card{nil, nil, nil, nil} g.GameBoard.Scourge = [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)}
return g return g
} }

10
deck.go
View File

@ -1,6 +1,9 @@
package main package main
import "math/rand" import (
"math/rand"
"time"
)
type Deck struct { type Deck struct {
Cards []Card Cards []Card
@ -9,7 +12,7 @@ type Deck struct {
func NewDeck() *Deck { func NewDeck() *Deck {
cards := []Card{NewCard(0)} cards := []Card{NewCard(0)}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
for j := 1; j < 10; j++ { for j := 1; j < 9; j++ {
cards = append(cards, Card(NewCard(j))) cards = append(cards, Card(NewCard(j)))
} }
} }
@ -20,8 +23,9 @@ func NewDeck() *Deck {
func (d *Deck) Shuffle() { func (d *Deck) Shuffle() {
cards := d.Cards cards := d.Cards
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range cards { for i := range cards {
j := rand.Intn(i + 1) j := r.Intn(i + 1)
cards[i], cards[j] = cards[j], cards[i] cards[i], cards[j] = cards[j], cards[i]
} }
d.Cards = cards d.Cards = cards

100
game.go
View File

@ -1,19 +1,34 @@
package main package main
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
) )
type GameStatus int
const (
StatusLobby = iota
StatusReady
StatusPlaying
StatusStop
)
func (g GameStatus) String() string {
return []string{"Lobby", "Ready", "Playing", "Stopped"}[g]
}
type Game struct { type Game struct {
GameBoard *Board GameBoard *Board
SentinalPlayer *Player SentinalPlayer *Player
ScourgePlayer *Player ScourgePlayer *Player
sentinalDeck *Deck SentinalDeck *Deck
scourgeDeck *Deck ScourgeDeck *Deck
CurrentTurn int CurrentTurn int
cardBuffer []Card CardBuffer []Card
CanDraw bool CanDraw bool
Status GameStatus
} }
func NewGame() *Game { func NewGame() *Game {
@ -25,21 +40,67 @@ func NewGame() *Game {
GameBoard: NewBoard(), GameBoard: NewBoard(),
SentinalPlayer: NewPlayer("Sentinal", 1), SentinalPlayer: NewPlayer("Sentinal", 1),
ScourgePlayer: NewPlayer("Scourge", 2), ScourgePlayer: NewPlayer("Scourge", 2),
sentinalDeck: deckA, SentinalDeck: deckA,
scourgeDeck: deckB, ScourgeDeck: deckB,
CurrentTurn: 0, //start with no turn CurrentTurn: 0, //start with no turn
cardBuffer: []Card{}, CardBuffer: []Card{},
CanDraw: false, CanDraw: false,
Status: StatusLobby,
} }
} }
func (g *Game) String() string {
return fmt.Sprintf("Sen(%v): %v\n\n%v\n\nSco(%v): %v\n%v %v\n%v\n", g.SentinalPlayer.Life, g.SentinalPlayer.Hand, g.GameBoard, g.ScourgePlayer.Life, g.ScourgePlayer.Hand, g.Status, g.CanDraw, g.CardBuffer)
}
func (g *Game) PlayerStateAct(id int, cmd string) *Game { func (g *Game) PlayerStateAct(id int, cmd string) *Game {
switch cmd { switch cmd {
case "i": case "d":
//info //debug game state
return g return g
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]
case "s": case "s":
//start turn //start turn
if g.Status == StatusReady {
g.Status = StatusPlaying
g.CurrentTurn = id
g.CanDraw = false
}
if id != g.CurrentTurn { if id != g.CurrentTurn {
return nil return nil
} }
@ -59,7 +120,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game {
if id != g.CurrentTurn { if id != g.CurrentTurn {
return nil return nil
} }
g.cardBuffer = []Card{} g.CardBuffer = []Card{}
if id == 1 { if id == 1 {
for _, v := range g.GameBoard.Sentinal { for _, v := range g.GameBoard.Sentinal {
v.Endstep(g) v.Endstep(g)
@ -89,11 +150,11 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
if id == g.SentinalPlayer.Id { if id == g.SentinalPlayer.Id {
curr = g.SentinalPlayer curr = g.SentinalPlayer
opp = g.ScourgePlayer opp = g.ScourgePlayer
currD = g.sentinalDeck currD = g.SentinalDeck
} else { } else {
curr = g.ScourgePlayer curr = g.ScourgePlayer
opp = g.SentinalPlayer opp = g.SentinalPlayer
currD = g.scourgeDeck currD = g.ScourgeDeck
} }
cmd_s := strings.Split(cmd, " ") cmd_s := strings.Split(cmd, " ")
if len(cmd_s) < 1 { if len(cmd_s) < 1 {
@ -105,10 +166,13 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
if !g.CanDraw { if !g.CanDraw {
return nil return nil
} }
g.cardBuffer = currD.Scry(curr.Life) g.CardBuffer = currD.Scry(curr.Life)
return g.cardBuffer return g.CardBuffer
case "d": case "d":
//draw: return player hand //draw: return player hand
if !g.CanDraw {
return nil
}
if len(cmd_s) != 2 || !g.CanDraw { if len(cmd_s) != 2 || !g.CanDraw {
return nil return nil
} }
@ -116,15 +180,17 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
if err != nil { if err != nil {
panic(err) panic(err)
} }
x := g.cardBuffer[x_i] x := g.CardBuffer[x_i]
buf := g.cardBuffer buf := g.CardBuffer
for i, v := range buf { for i, v := range buf {
if v == x { if v == x {
buf = append(buf[:i], buf[i+1:]...) buf = append(buf[:i], buf[i+1:]...)
} }
} }
currD.Bottom(buf) currD.Bottom(buf)
return []Card{x} curr.Hand = append(curr.Hand, x)
g.CanDraw = false
return curr.Hand
case "m": case "m":
//move: return player board or [] if invalid //move: return player board or [] if invalid
@ -157,7 +223,7 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
} }
case "p": case "p":
//play: return player boad or [] if invalid //play: return player boad or [] if invalid
if len(cmd_s) != 2 { if len(cmd_s) != 3 {
return nil return nil
} }
x_i, _ := strconv.Atoi(cmd_s[1]) x_i, _ := strconv.Atoi(cmd_s[1])

13
main.go
View File

@ -5,10 +5,21 @@ import "fmt"
func main() { func main() {
g := NewGame() g := NewGame()
fmt.Println(g.GameBoard)
g.PlayerStateAct(1, "b") g.PlayerStateAct(1, "b")
g.PlayerStateAct(1, "s") g.PlayerStateAct(1, "s")
tmp := g.PlayerStateAct(1, "d")
fmt.Println("begin")
fmt.Println(tmp)
fmt.Println("scry")
scry := g.PlayerAct(1, "s") scry := g.PlayerAct(1, "s")
fmt.Println(scry) fmt.Println(scry)
fmt.Println("draw")
g.PlayerAct(1, "d 1")
h2 := g.PlayerStateAct(1, "d")
fmt.Println(h2)
fmt.Println("play")
g.PlayerAct(1, "p 1 3")
b2 := g.PlayerStateAct(1, "d")
fmt.Println(b2)
} }