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

152
card.go
View File

@ -1,9 +1,54 @@
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 (
Valk Card = iota
Valk CardValue = iota
Ace
Two
Three
@ -12,20 +57,103 @@ const (
Six
Seven
Eight
Nine
Ten
Jack
Queen
King
)
func (c Card) String() string {
return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"}[c]
func (c CardValue) String() string {
return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8"}[c]
}
func (c Card) CanAttack(src, dest int) bool {
if c == Eight || src != dest {
return false
func NewCard(v int) Card {
switch v {
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,
}
}
}
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 {
cards := []Card{Valk}
cards := []Card{NewCard(0)}
for i := 0; i < 3; i++ {
for j := 1; j < 14; j++ {
cards = append(cards, Card(j))
for j := 1; j < 10; j++ {
cards = append(cards, Card(NewCard(j)))
}
}
return &Deck{

62
game.go
View File

@ -9,10 +9,10 @@ type Game struct {
GameBoard *Board
SentinalPlayer *Player
ScourgePlayer *Player
SentinalDeck *Deck
ScourgeDeck *Deck
sentinalDeck *Deck
scourgeDeck *Deck
CurrentTurn int
CardBuffer []Card
cardBuffer []Card
CanDraw bool
}
@ -25,14 +25,39 @@ func NewGame() *Game {
GameBoard: NewBoard(),
SentinalPlayer: NewPlayer("Sentinal", 1),
ScourgePlayer: NewPlayer("Scourge", 2),
SentinalDeck: deckA,
ScourgeDeck: deckB,
sentinalDeck: deckA,
scourgeDeck: deckB,
CurrentTurn: 0, //start with no turn
CardBuffer: []Card{},
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
@ -43,11 +68,11 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
if id == g.SentinalPlayer.Id {
curr = g.SentinalPlayer
opp = g.ScourgePlayer
currD = g.SentinalDeck
currD = g.sentinalDeck
} else {
curr = g.ScourgePlayer
opp = g.SentinalPlayer
currD = g.ScourgeDeck
currD = g.scourgeDeck
}
cmd_s := strings.Split(cmd, " ")
if len(cmd_s) < 1 {
@ -56,19 +81,22 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
switch cmd_s[0] {
case "s":
//scry: return scry options off top of deck
g.CardBuffer = currD.Scry(curr.Life)
return g.CardBuffer
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 {
if len(cmd_s) != 2 || !g.CanDraw {
return nil
}
x_i, err := strconv.Atoi(cmd_s[1])
if err != nil {
panic(err)
}
x := Card(x_i)
buf := g.CardBuffer
x := g.cardBuffer[x_i]
buf := g.cardBuffer
for i, v := range buf {
if v == x {
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])
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 {
for i, v := range curr.Hand {
if v == Card(x_i) {
curr.Hand = append(curr.Hand[:i], curr.Hand[i+1:]...)
}
}
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
return g.GameBoard.GetRow(g.CurrentTurn)
} else {
return []Card{}