snengame/internal/game/board.go

161 lines
2.9 KiB
Go

package game
import (
"fmt"
"log"
)
type Board struct {
Sentinal [4]*Card `json:"sentinal"`
Scourge [4]*Card `json:"scourge"`
}
func NewBoard() *Board {
return &Board{
Sentinal: [4]*Card{NewEmpty(0), NewEmpty(1), NewEmpty(2), NewEmpty(3)},
Scourge: [4]*Card{NewEmpty(0), NewEmpty(1), NewEmpty(2), NewEmpty(3)},
}
}
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 {
return b.Scourge[:]
}
}
func (b *Board) GetCard(id int, pos int) *Card {
return b.GetRow(id)[pos]
}
func (b *Board) Remove(c *Card) {
for k, v := range b.Sentinal {
if v.Id == c.Id {
b.Sentinal[k] = NewEmpty(k)
}
}
for k, v := range b.Scourge {
if v.Id == c.Id {
b.Scourge[k] = NewEmpty(k)
}
}
}
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(src)
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
brd[dest] = c
} else {
return false
}
return true
}
func (b *Board) CanAttack(id, atk, def int) bool {
aBrd := b.GetRow(id)
if atk < 0 || atk > 3 || def < 0 || def > 3 {
return false
}
if aBrd[atk].Empty() || aBrd[atk].Sick || atk != def {
return false
}
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
v.Effects = []Effect{}
}
for _, v := range b.Scourge {
v.Power = v.BasePower
v.Effects = []Effect{}
}
}