use card objects

This commit is contained in:
stryan 2021-07-15 20:58:21 -04:00
parent a51bfc6ae8
commit 279af42056
4 changed files with 206 additions and 79 deletions

View File

@ -2,17 +2,13 @@ package main
type Board struct { type Board struct {
Sentinal [4]Card Sentinal [4]Card
SenBuf [4]bool
Scourge [4]Card Scourge [4]Card
ScoBuf [4]bool
} }
func NewBoard() *Board { func NewBoard() *Board {
return &Board{ return &Board{
Sentinal: [4]Card{-1, -1, -1, -1}, Sentinal: [4]Card{nil, nil, nil, nil},
SenBuf: [4]bool{true, true, true, true}, Scourge: [4]Card{nil, nil, nil, nil},
Scourge: [4]Card{-1, -1, -1, -1},
ScoBuf: [4]bool{true, true, true, true},
} }
} }
@ -31,20 +27,18 @@ func (b *Board) Move(id, src, dest int) bool {
} else { } else {
brd = b.Scourge brd = b.Scourge
} }
if brd[src] == -1 { if brd[src] == nil {
return false return false
} }
if brd[dest] != -1 { if brd[dest] != nil {
return false return false
} }
brd[dest] = brd[src] brd[dest] = brd[src]
brd[src] = -1 brd[src] = nil
if id == 1 { if id == 1 {
b.Sentinal = brd b.Sentinal = brd
b.SenBuf[dest] = false
} else { } else {
b.Scourge = brd b.Scourge = brd
b.ScoBuf[dest] = false
} }
return true return true
} }
@ -56,16 +50,14 @@ func (b *Board) Play(id int, c Card, dest int) bool {
} else { } else {
brd = b.Scourge brd = b.Scourge
} }
if brd[dest] != -1 { if brd[dest] != nil {
return false return false
} }
brd[dest] = c brd[dest] = c
if id == 1 { if id == 1 {
b.Sentinal = brd b.Sentinal = brd
b.SenBuf[dest] = false
} else { } else {
b.Scourge = brd b.Scourge = brd
b.ScoBuf[dest] = false
} }
return true return true
} }
@ -73,86 +65,69 @@ func (b *Board) Play(id int, c Card, dest int) bool {
func (b *Board) Attack(id int, atk, def int) int { func (b *Board) Attack(id int, atk, def int) int {
var aBrd [4]Card var aBrd [4]Card
var dBrd [4]Card var dBrd [4]Card
var aBuf [4]bool
var dBuf [4]bool
if id == 1 { if id == 1 {
aBrd = b.Sentinal aBrd = b.Sentinal
aBuf = b.SenBuf
dBrd = b.Scourge dBrd = b.Scourge
dBuf = b.ScoBuf
} else { } else {
aBrd = b.Scourge aBrd = b.Scourge
aBuf = b.ScoBuf
dBrd = b.Sentinal dBrd = b.Sentinal
dBuf = b.SenBuf
} }
if aBrd[atk] == -1 || !aBuf[atk] || !aBrd[atk].CanAttack(atk, def) { if aBrd[atk] == nil || !aBrd[atk].CanAttack(atk, def) {
return -1 return -1
} }
aBuf[atk] = false aBrd[atk].Acted()
if dBrd[def] == -1 { if dBrd[def] == nil {
//health damage //health damage
if id == 1 { if id == 1 {
b.Sentinal = aBrd b.Sentinal = aBrd
b.SenBuf = aBuf
b.Scourge = dBrd b.Scourge = dBrd
b.ScoBuf = dBuf
} else { } else {
b.Scourge = aBrd b.Scourge = aBrd
b.ScoBuf = aBuf
b.Sentinal = dBrd b.Sentinal = dBrd
b.SenBuf = dBuf
} }
return 1 return 1
} }
//Do actual battle math //Do actual battle math
attacker := aBrd[atk] attacker := aBrd[atk].Value()
defender := dBrd[def] defender := dBrd[def].Value()
dBrd[def].Acted()
//check atk buffs //check atk buffs
for i, v := range aBrd { for i, v := range aBrd {
if v == Two { if v.Value() == Two {
attacker = attacker + 1 attacker = attacker + 1
} }
if v == Three && (i == atk-1 || i == atk+1) { if v.Value() == Three && (i == atk-1 || i == atk+1) {
attacker = attacker + 1 attacker = attacker + 1
} }
} }
for i, v := range dBrd { for i, v := range dBrd {
if v == Two { if v.Value() == Two {
defender = defender + 1 defender = defender + 1
} }
if v == Three && (i == def-1 || i == def+1) { if v.Value() == Three && (i == def-1 || i == def+1) {
defender = defender + 1 defender = defender + 1
} }
} }
if attacker > defender { if attacker > defender {
dBrd[def] = -1 dBrd[def] = nil
if id == 1 { if id == 1 {
b.Sentinal = aBrd b.Sentinal = aBrd
b.SenBuf = aBuf
b.Scourge = dBrd b.Scourge = dBrd
b.ScoBuf = dBuf
} else { } else {
b.Scourge = aBrd b.Scourge = aBrd
b.ScoBuf = aBuf
b.Sentinal = dBrd b.Sentinal = dBrd
b.SenBuf = dBuf
} }
return 0 return 0
} }
if attacker == defender { if attacker == defender {
aBrd[atk] = -1 aBrd[atk] = nil
dBrd[def] = -1 dBrd[def] = nil
if id == 1 { if id == 1 {
b.Sentinal = aBrd b.Sentinal = aBrd
b.SenBuf = aBuf
b.Scourge = dBrd b.Scourge = dBrd
b.ScoBuf = dBuf
} else { } else {
b.Scourge = aBrd b.Scourge = aBrd
b.ScoBuf = aBuf
b.Sentinal = dBrd b.Sentinal = dBrd
b.SenBuf = dBuf
} }
return 0 return 0
} }

154
card.go
View File

@ -1,9 +1,54 @@
package main package main
type Card int type Card interface {
Cast(g *Game) *Game
Upkeep(g *Game) *Game
Endstep(g *Game) *Game
Enters(g *Game) *Game
Value() CardValue
Acted()
CanAttack(int, int) bool
}
type GenericCard struct {
Val CardValue
Sick bool
}
func (g *GenericCard) Cast(game *Game) *Game {
return nil
}
func (g *GenericCard) Enters(game *Game) *Game {
return nil
}
func (g *GenericCard) Upkeep(game *Game) *Game {
g.Sick = false
return nil
}
func (g *GenericCard) Endstep(game *Game) *Game {
return nil
}
func (g *GenericCard) Value() CardValue {
return g.Val
}
func (g *GenericCard) Acted() {
g.Sick = true
}
func (g *GenericCard) CanAttack(x, y int) bool {
if x == y && !g.Sick {
return true
}
return false
}
type CardValue int
const ( const (
Valk Card = iota Valk CardValue = iota
Ace Ace
Two Two
Three Three
@ -12,20 +57,103 @@ const (
Six Six
Seven Seven
Eight Eight
Nine
Ten
Jack
Queen
King
) )
func (c Card) String() string { func (c CardValue) String() string {
return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"}[c] return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8"}[c]
} }
func (c Card) CanAttack(src, dest int) bool { func NewCard(v int) Card {
if c == Eight || src != dest { switch v {
return false case 1:
return &AceCard{
&GenericCard{
Val: Ace,
Sick: false,
},
}
case 4:
return &FourCard{
&GenericCard{
Val: Four,
Sick: true,
},
}
case 8:
return &EightCard{
&GenericCard{
Val: Eight,
Sick: true,
},
0,
}
case 0:
return &Valkyrie{
&GenericCard{
Val: Valk,
Sick: false,
},
}
default:
return &GenericCard{
Val: CardValue(v),
Sick: true,
}
} }
return true }
type AceCard struct {
*GenericCard
}
func (a *AceCard) CanAttack(x, y int) bool {
if x == y {
return true
}
return false
}
type FourCard struct {
*GenericCard
}
func (f *FourCard) Enters(g *Game) *Game {
g.CanDraw = true
return g
}
type EightCard struct {
*GenericCard
Counters int
}
func (e *EightCard) CanAttack(x, y int) bool {
return false
}
func (e *EightCard) Upkeep(g *Game) *Game {
if e.Counters > 2 {
e = nil
}
return g
}
func (e *EightCard) Endstep(g *Game) *Game {
e.Counters = e.Counters + 1
return g
}
type Valkyrie struct {
*GenericCard
}
func (v *Valkyrie) Cast(g *Game) *Game {
g.GameBoard.Sentinal = [4]Card{nil, nil, nil, nil}
g.GameBoard.Scourge = [4]Card{nil, nil, nil, nil}
return g
}
func (v *Valkyrie) Enters(g *Game) *Game {
v = nil
return g
} }

View File

@ -7,10 +7,10 @@ type Deck struct {
} }
func NewDeck() *Deck { func NewDeck() *Deck {
cards := []Card{Valk} cards := []Card{NewCard(0)}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
for j := 1; j < 14; j++ { for j := 1; j < 10; j++ {
cards = append(cards, Card(j)) cards = append(cards, Card(NewCard(j)))
} }
} }
return &Deck{ return &Deck{

62
game.go
View File

@ -9,10 +9,10 @@ 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
} }
@ -25,14 +25,39 @@ 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,
} }
} }
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 { func (g *Game) PlayerAct(id int, cmd string) []Card {
if id != g.CurrentTurn { if id != g.CurrentTurn {
return nil return nil
@ -43,11 +68,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 {
@ -56,19 +81,22 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
switch cmd_s[0] { switch cmd_s[0] {
case "s": case "s":
//scry: return scry options off top of deck //scry: return scry options off top of deck
g.CardBuffer = currD.Scry(curr.Life) if !g.CanDraw {
return g.CardBuffer return nil
}
g.cardBuffer = currD.Scry(curr.Life)
return g.cardBuffer
case "d": case "d":
//draw: return player hand //draw: return player hand
if len(cmd_s) != 2 { if len(cmd_s) != 2 || !g.CanDraw {
return nil return nil
} }
x_i, err := strconv.Atoi(cmd_s[1]) x_i, err := strconv.Atoi(cmd_s[1])
if err != nil { if err != nil {
panic(err) panic(err)
} }
x := Card(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:]...)
@ -113,13 +141,9 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
} }
x_i, _ := strconv.Atoi(cmd_s[1]) x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2]) y_i, _ := strconv.Atoi(cmd_s[2])
res := g.GameBoard.Play(g.CurrentTurn, Card(x_i), y_i) res := g.GameBoard.Play(g.CurrentTurn, curr.Hand[x_i], y_i)
if res { if res {
for i, v := range curr.Hand { curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
if v == Card(x_i) {
curr.Hand = append(curr.Hand[:i], curr.Hand[i+1:]...)
}
}
return g.GameBoard.GetRow(g.CurrentTurn) return g.GameBoard.GetRow(g.CurrentTurn)
} else { } else {
return []Card{} return []Card{}