snengame/internal/game/board.go
2021-07-22 15:37:25 -04:00

178 lines
3.0 KiB
Go

package game
import "fmt"
type Board struct {
Sentinal [4]Card `json:"sentinal"`
Scourge [4]Card `json:"scourge"`
}
func NewBoard() *Board {
return &Board{
Sentinal: [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)},
Scourge: [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)},
}
}
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 == 1 {
return b.Sentinal[:]
} else {
return b.Scourge[:]
}
}
func (b *Board) Empty(id int) bool {
res := true
if id == SentinalID {
for _, v := range b.Sentinal {
if !v.Empty() {
res = false
}
}
} else {
for _, v := range b.Scourge {
if !v.Empty() {
res = false
}
}
}
return res
}
func (b *Board) Move(id, src, dest int) bool {
var brd [4]Card
if id == 1 {
brd = b.Sentinal
} else {
brd = b.Scourge
}
if brd[src].Empty() {
return false
}
if !brd[dest].Empty() {
return false
}
if brd[src].Acted() {
return false
}
brd[dest].Act()
brd[dest] = brd[src]
brd[src] = NewCard(-1)
if id == 1 {
b.Sentinal = brd
} else {
b.Scourge = brd
}
return true
}
func (b *Board) CanPlay(id int, c *Card, dest int) bool {
var brd [4]Card
if id == 1 {
brd = b.Sentinal
} else {
brd = b.Scourge
}
if !brd[dest].Empty() {
return false
}
return true
}
func (b *Board) Play(id int, c *Card, dest int) bool {
var brd [4]Card
if id == 1 {
brd = b.Sentinal
} else {
brd = b.Scourge
}
if !brd[dest].Empty() {
return false
}
brd[dest] = *c
if id == 1 {
b.Sentinal = brd
} else {
b.Scourge = brd
}
return true
}
func (b *Board) Attack(id int, atk, def int) int {
var aBrd [4]Card
var dBrd [4]Card
if id == 1 {
aBrd = b.Sentinal
dBrd = b.Scourge
} else {
aBrd = b.Scourge
dBrd = b.Sentinal
}
if aBrd[atk].Empty() || !aBrd[atk].CanAttack(atk, def) {
return -1
}
aBrd[atk].Act()
if dBrd[def].Empty() {
//health damage
if id == 1 {
b.Sentinal = aBrd
b.Scourge = dBrd
} else {
b.Scourge = aBrd
b.Sentinal = dBrd
}
return 1
}
//Do actual battle math
attacker := aBrd[atk].Value()
defender := dBrd[def].Value()
dBrd[def].Acted()
//check atk buffs
for i, v := range aBrd {
if v.Value() == Two {
attacker = attacker + 1
}
if v.Value() == Three && (i == atk-1 || i == atk+1) {
attacker = attacker + 1
}
}
for i, v := range dBrd {
if v.Value() == Two {
defender = defender + 1
}
if v.Value() == Three && (i == def-1 || i == def+1) {
defender = defender + 1
}
}
if attacker > defender {
dBrd[def] = NewCard(-1)
if id == 1 {
b.Sentinal = aBrd
b.Scourge = dBrd
} else {
b.Scourge = aBrd
b.Sentinal = dBrd
}
return 0
}
if attacker == defender {
aBrd[atk] = NewCard(-1)
dBrd[def] = NewCard(-1)
if id == 1 {
b.Sentinal = aBrd
b.Scourge = dBrd
} else {
b.Scourge = aBrd
b.Sentinal = dBrd
}
return 0
}
return -1
}