tome_lib/board.go

178 lines
3.3 KiB
Go
Raw Normal View History

2021-10-01 12:43:55 -04:00
package tome_lib
import (
"fmt"
"log"
)
type Board struct {
2021-11-08 15:33:55 -05:00
Sentinal [4]*Card `json:"sentinal"`
SentinalSpells [2]int
Scourge [4]*Card `json:"scourge"`
ScourgeSpells [2]int
2021-10-01 12:43:55 -04:00
}
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 {
if id == SentinalID {
return b.Sentinal[:]
} else if id == ScourgeID {
return b.Scourge[:]
} else {
log.Println("asked for invalid row")
panic("bad row")
2021-10-01 12:43:55 -04:00
return nil
}
}
func (b *Board) GetCard(id int, pos int) *Card {
2021-10-07 13:21:46 -04:00
if pos >= 0 && pos <= 3 {
return b.GetRow(id)[pos]
} else {
return nil
}
2021-10-01 12:43:55 -04:00
}
func (b *Board) Remove(c *Card) {
for k, v := range b.Sentinal {
if v.Id == c.Id {
b.Sentinal[k] = NewEmpty(c.Owner, k)
2021-10-01 12:43:55 -04:00
}
}
for k, v := range b.Scourge {
if v.Id == c.Id {
b.Scourge[k] = NewEmpty(c.Owner, k)
2021-10-01 12:43:55 -04:00
}
}
}
func (b *Board) Replace(old *Card, neo *Card) {
if old.Owner == SentinalID {
b.Sentinal[old.Position] = neo
} else if old.Owner == ScourgeID {
b.Scourge[old.Position] = neo
} else {
log.Printf("can't replace a card with bad owner:%v", old.Owner)
return
}
}
2021-10-01 12:43:55 -04:00
func (b *Board) Empty(id int) bool {
res := true
row := b.GetRow(id)
for _, v := range row {
if !v.Empty() {
res = false
}
}
return res
}
func (b *Board) CanMove(id, src, dest int) bool {
brd := b.GetRow(id)
if src < 0 || src > 3 || dest < 0 || dest > 3 {
return false
}
if brd[src].Empty() {
return false
}
if !brd[dest].Empty() {
return false
}
if brd[src].Sick {
return false
}
return true
}
func (b *Board) Move(id, src, dest int) bool {
brd := b.GetRow(id)
brd[dest] = brd[src]
brd[dest].Position = dest
brd[src] = NewEmpty(brd[dest].Owner, src)
2021-10-01 12:43:55 -04:00
return true
}
func (b *Board) CanPlay(id int, c *Card, dest int) bool {
brd := b.GetRow(id)
if dest < 0 || dest > 3 {
return false
}
if !brd[dest].Empty() && !c.Spell {
return false
}
return true
}
func (b *Board) Play(id int, c *Card, dest int, should bool) bool {
brd := b.GetRow(id)
if should {
c.Position = dest
c.Owner = id
brd[dest] = c
} else {
return false
}
return true
}
func (b *Board) CanAttack(id, atk, def int) bool {
aBrd := b.GetRow(id)
2021-11-16 15:51:09 -05:00
dBrd := b.GetRow(flipID(id))
2021-10-01 12:43:55 -04:00
if atk < 0 || atk > 3 || def < 0 || def > 3 {
return false
}
if aBrd[atk].Empty() || aBrd[atk].Sick || atk != def {
return false
}
2021-11-16 15:51:09 -05:00
if aBrd[atk].Phased || dBrd[def].Phased {
return false
}
2021-10-01 12:43:55 -04:00
return true
}
func (b *Board) Attack(id, atk, def int) int {
var aid, did int
if id == SentinalID {
aid = SentinalID
did = ScourgeID
} else {
aid = ScourgeID
did = SentinalID
}
attacker := b.GetRow(aid)[atk]
defender := b.GetRow(did)[def]
if defender.Empty() {
//health damage
return 1
}
//Do actual battle math
atkpower := attacker.Power
defpower := defender.Power
log.Printf("Atk:%v v Def: %v", atkpower, defpower)
if atkpower > defpower {
return 2
}
if atkpower == defpower {
return 4
}
if atkpower < defpower {
return 3
}
return -1
}
func (b *Board) ResetCards() {
for _, v := range b.Sentinal {
v.Power = v.BasePower
}
for _, v := range b.Scourge {
v.Power = v.BasePower
}
}