actually use GetRow since I can, implement 2 and 3, cards have owners and positions

This commit is contained in:
stryan 2021-07-25 13:27:12 -04:00
parent 22a3e0c8e8
commit d1845760fe
5 changed files with 129 additions and 80 deletions

View File

@ -2,8 +2,6 @@ package game
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
) )
type Board struct { type Board struct {
@ -13,8 +11,8 @@ type Board struct {
func NewBoard() *Board { func NewBoard() *Board {
return &Board{ return &Board{
Sentinal: [4]*Card{NewCard(-1, uuid.Nil), NewCard(-1, uuid.Nil), NewCard(-1, uuid.Nil), NewCard(-1, uuid.Nil)}, Sentinal: [4]*Card{NewEmpty(0), NewEmpty(1), NewEmpty(2), NewEmpty(3)},
Scourge: [4]*Card{NewCard(-1, uuid.Nil), NewCard(-1, uuid.Nil), NewCard(-1, uuid.Nil), NewCard(-1, uuid.Nil)}, Scourge: [4]*Card{NewEmpty(0), NewEmpty(1), NewEmpty(2), NewEmpty(3)},
} }
} }
@ -23,7 +21,7 @@ func (b *Board) String() string {
} }
func (b *Board) GetRow(id int) []*Card { func (b *Board) GetRow(id int) []*Card {
if id == 1 { if id == SentinalID {
return b.Sentinal[:] return b.Sentinal[:]
} else { } else {
return b.Scourge[:] return b.Scourge[:]
@ -31,51 +29,36 @@ func (b *Board) GetRow(id int) []*Card {
} }
func (b *Board) GetCard(id int, pos int) *Card { func (b *Board) GetCard(id int, pos int) *Card {
if id == SentinalID { return b.GetRow(id)[pos]
return b.Sentinal[pos]
} else {
return b.Scourge[pos]
}
} }
func (b *Board) Remove(c *Card) { func (b *Board) Remove(c *Card) {
for k, v := range b.Sentinal { for k, v := range b.Sentinal {
if v.Id == c.Id { if v.Id == c.Id {
b.Sentinal[k] = NewCard(-1, uuid.Nil) b.Sentinal[k] = NewEmpty(k)
} }
} }
for k, v := range b.Scourge { for k, v := range b.Scourge {
if v.Id == c.Id { if v.Id == c.Id {
b.Scourge[k] = NewCard(-1, uuid.Nil) b.Scourge[k] = NewEmpty(k)
} }
} }
} }
func (b *Board) Empty(id int) bool { func (b *Board) Empty(id int) bool {
res := true res := true
if id == SentinalID { row := b.GetRow(id)
for _, v := range b.Sentinal { for _, v := range row {
if !v.Empty() { if !v.Empty() {
res = false res = false
} }
} }
} else {
for _, v := range b.Scourge {
if !v.Empty() {
res = false
}
}
}
return res return res
} }
func (b *Board) CanMove(id, src, dest int) bool { func (b *Board) CanMove(id, src, dest int) bool {
var brd [4]*Card brd := b.GetRow(id)
if id == SentinalID {
brd = b.Sentinal
} else {
brd = b.Scourge
}
if brd[src].Empty() { if brd[src].Empty() {
return false return false
} }
@ -105,7 +88,7 @@ func (b *Board) Move(id, src, dest int) bool {
return false return false
} }
brd[dest] = brd[src] brd[dest] = brd[src]
brd[src] = NewCard(-1, uuid.Nil) brd[src] = NewEmpty(src)
if id == SentinalID { if id == SentinalID {
b.Sentinal = brd b.Sentinal = brd
} else { } else {
@ -115,12 +98,7 @@ func (b *Board) Move(id, src, dest int) bool {
} }
func (b *Board) CanPlay(id int, c *Card, dest int) bool { func (b *Board) CanPlay(id int, c *Card, dest int) bool {
var brd [4]*Card brd := b.GetRow(id)
if id == SentinalID {
brd = b.Sentinal
} else {
brd = b.Scourge
}
if !brd[dest].Empty() { if !brd[dest].Empty() {
return false return false
} }
@ -128,33 +106,18 @@ func (b *Board) CanPlay(id int, c *Card, dest int) bool {
} }
func (b *Board) Play(id int, c *Card, dest int, should bool) bool { func (b *Board) Play(id int, c *Card, dest int, should bool) bool {
var brd [4]*Card brd := b.GetRow(id)
if id == SentinalID {
brd = b.Sentinal
} else {
brd = b.Scourge
}
if !brd[dest].Empty() {
return false
}
if should { if should {
brd[dest] = c brd[dest] = c
} c.Position = dest
if id == SentinalID {
b.Sentinal = brd
} else { } else {
b.Scourge = brd return false
} }
return true return true
} }
func (b *Board) CanAttack(id, atk, def int) bool { func (b *Board) CanAttack(id, atk, def int) bool {
var aBrd [4]*Card aBrd := b.GetRow(id)
if id == SentinalID {
aBrd = b.Sentinal
} else {
aBrd = b.Scourge
}
if aBrd[atk].Empty() || aBrd[atk].Sick || atk != def { if aBrd[atk].Empty() || aBrd[atk].Sick || atk != def {
return false return false
} }
@ -186,7 +149,7 @@ func (b *Board) Attack(id int, atk, def int) int {
attacker := aBrd[atk].Power attacker := aBrd[atk].Power
defender := dBrd[def].Power defender := dBrd[def].Power
if attacker > defender { if attacker > defender {
dBrd[def] = NewCard(-1, uuid.Nil) dBrd[def] = NewEmpty(def)
if id == SentinalID { if id == SentinalID {
b.Sentinal = aBrd b.Sentinal = aBrd
b.Scourge = dBrd b.Scourge = dBrd
@ -197,8 +160,8 @@ func (b *Board) Attack(id int, atk, def int) int {
return 0 return 0
} }
if attacker == defender { if attacker == defender {
aBrd[atk] = NewCard(-1, uuid.Nil) aBrd[atk] = NewEmpty(atk)
dBrd[def] = NewCard(-1, uuid.Nil) dBrd[def] = NewEmpty(def)
if id == 1 { if id == 1 {
b.Sentinal = aBrd b.Sentinal = aBrd
b.Scourge = dBrd b.Scourge = dBrd

View File

@ -8,10 +8,13 @@ import (
type Card struct { type Card struct {
Type CardType `json:"type"` Type CardType `json:"type"`
BasePower int `json:"base_power"`
Power int `json:"power"` Power int `json:"power"`
Id uuid.UUID `json:"id"` Id uuid.UUID `json:"id"`
Sick bool `json:"sick"` Sick bool `json:"sick"`
Counters int `json:"counters"` Counters int `json:"counters"`
Owner int `json:"owner"`
Position int `json:"position"`
} }
type CardType int type CardType int
@ -38,16 +41,32 @@ func (c CardType) String() string {
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, id uuid.UUID) *Card { func NewCard(v, o, p int, id uuid.UUID) *Card {
if id == uuid.Nil { if id == uuid.Nil {
id = uuid.New() id = uuid.New()
} }
return &Card{ return &Card{
Type: CardType(v), Type: CardType(v),
BasePower: OraclePower(CardType(v), nil),
Power: OraclePower(CardType(v), nil), Power: OraclePower(CardType(v), nil),
Id: id, Id: id,
Sick: false, Sick: false,
Counters: 0, Counters: 0,
Owner: o,
Position: p,
}
}
func NewEmpty(p int) *Card {
return &Card{
Type: EmptyValue,
BasePower: -1,
Power: -1,
Id: uuid.New(),
Sick: false,
Counters: 0,
Owner: -1,
Position: p,
} }
} }

View File

@ -11,11 +11,11 @@ type Deck struct {
Cards []*Card `json:"cards"` Cards []*Card `json:"cards"`
} }
func NewDeck() *Deck { func NewDeck(owner int) *Deck {
cards := []*Card{NewCard(0, uuid.Nil)} cards := []*Card{NewCard(0, owner, -1, uuid.Nil)}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
for j := 1; j < 9; j++ { for j := 1; j < 9; j++ {
cards = append(cards, NewCard(j, uuid.Nil)) cards = append(cards, NewCard(j, owner, -1, uuid.Nil))
} }
} }
return &Deck{ return &Deck{

View File

@ -42,8 +42,8 @@ type Game struct {
} }
func NewGame() *Game { func NewGame() *Game {
deckA := NewDeck() deckA := NewDeck(SentinalID)
deckB := NewDeck() deckB := NewDeck(ScourgeID)
deckA.Shuffle() deckA.Shuffle()
deckB.Shuffle() deckB.Shuffle()
return &Game{ return &Game{
@ -258,7 +258,7 @@ func (g *Game) PlayerAct(id int, cmd string) *Deck {
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])
if g.GameBoard.CanMove(g.CurrentTurn, x_i, y_i) { if g.GameBoard.CanMove(g.CurrentTurn, x_i, y_i) {
OracleMove(g.GameBoard.GetCard(g.CurrentTurn, x_i), g) OracleMove(g.GameBoard.GetCard(g.CurrentTurn, x_i), x_i, y_i, g)
g.GameBoard.Move(g.CurrentTurn, x_i, y_i) g.GameBoard.Move(g.CurrentTurn, x_i, y_i)
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn)) return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
} else { } else {
@ -302,8 +302,10 @@ func (g *Game) PlayerAct(id int, cmd string) *Deck {
shouldPlace := true shouldPlace := true
if g.GameBoard.CanPlay(g.CurrentTurn, card, y_i) { if g.GameBoard.CanPlay(g.CurrentTurn, card, y_i) {
shouldPlace = OracleCast(curr.Hand[x_i], g) shouldPlace = OracleCast(curr.Hand[x_i], g)
_ = g.GameBoard.Play(g.CurrentTurn, card, y_i, shouldPlace) placed := g.GameBoard.Play(g.CurrentTurn, card, y_i, shouldPlace)
if placed {
OracleEnters(curr.Hand[x_i], g) OracleEnters(curr.Hand[x_i], g)
}
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...) curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn)) return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
} else { } else {

View File

@ -1,5 +1,7 @@
package game package game
import "log"
func OracleUpkeep(c *Card, g *Game) { func OracleUpkeep(c *Card, g *Game) {
switch c.Type { switch c.Type {
case Eight: case Eight:
@ -28,22 +30,77 @@ func OracleEnters(c *Card, g *Game) {
switch c.Type { switch c.Type {
case Two: case Two:
//+1 to all //+1 to all
if g.CurrentTurn == SentinalID { if c.Owner == SentinalID {
for _, v := range g.GameBoard.Sentinal { for _, v := range g.GameBoard.Sentinal {
v.Power = v.Power + 1 v.Power = v.Power + 1
} }
} else { } else if c.Owner == ScourgeID {
for _, v := range g.GameBoard.Scourge { for _, v := range g.GameBoard.Scourge {
v.Power = v.Power + 1 v.Power = v.Power + 1
} }
} else {
log.Println("Trying to lookup an ETB when card not on field")
} }
case Three: case Three:
//+1 around it //+1 around it
if c.Owner == SentinalID {
if c.Position-1 >= 0 {
g.GameBoard.Sentinal[c.Position-1].Power = g.GameBoard.Sentinal[c.Position-1].Power + 1
}
if c.Position+1 <= 3 {
g.GameBoard.Sentinal[c.Position+1].Power = g.GameBoard.Sentinal[c.Position+1].Power + 1
}
}
if c.Owner == ScourgeID {
if c.Position-1 >= 0 {
g.GameBoard.Scourge[c.Position-1].Power = g.GameBoard.Scourge[c.Position-1].Power + 1
}
if c.Position+1 <= 3 {
g.GameBoard.Scourge[c.Position+1].Power = g.GameBoard.Scourge[c.Position+1].Power + 1
}
}
} }
return return
} }
func OracleLeaves(c *Card, g *Game) { func OracleLeaves(c *Card, g *Game) {
switch c.Type {
case Two:
//remove +1 to all
if c.Owner == SentinalID {
for _, v := range g.GameBoard.Sentinal {
v.Power = v.Power - 1
}
} else if c.Owner == ScourgeID {
for _, v := range g.GameBoard.Scourge {
v.Power = v.Power - 1
}
} else {
log.Println("Trying to lookup an LTB when card not on field")
}
case Three:
//+1 around it
if c.Owner == SentinalID {
if c.Position-1 >= 0 {
g.GameBoard.Sentinal[c.Position-1].Power = g.GameBoard.Sentinal[c.Position-1].Power - 1
}
if c.Position+1 <= 3 {
g.GameBoard.Sentinal[c.Position+1].Power = g.GameBoard.Sentinal[c.Position+1].Power - 1
}
}
if c.Owner == ScourgeID {
if c.Position-1 >= 0 {
g.GameBoard.Scourge[c.Position-1].Power = g.GameBoard.Scourge[c.Position-1].Power - 1
}
if c.Position+1 <= 3 {
g.GameBoard.Scourge[c.Position+1].Power = g.GameBoard.Scourge[c.Position+1].Power - 1
}
}
}
return return
} }
@ -59,8 +116,16 @@ func OraclePower(c CardType, g *Game) int {
return int(c) return int(c)
} }
func OracleMove(c *Card, g *Game) { func OracleMove(c *Card, src, dest int, g *Game) {
c.Sick = true c.Sick = true
switch c.Type {
case Three:
row := g.GameBoard.GetRow(c.Owner)
row[src-1].Power = row[src-1].Power - 1
row[src+1].Power = row[src+1].Power - 1
row[dest-1].Power = row[dest-1].Power + 1
row[dest+1].Power = row[dest+1].Power + 1
}
} }
func OracleAttack(c *Card, g *Game) { func OracleAttack(c *Card, g *Game) {