snengame/internal/game/board.go

158 lines
2.8 KiB
Go
Raw Normal View History

2021-07-22 15:37:25 -04:00
package game
2021-07-15 19:26:57 -04:00
import (
"fmt"
2021-07-25 15:33:47 -04:00
"log"
)
2021-07-16 14:53:45 -04:00
2021-07-15 19:26:57 -04:00
type Board struct {
2021-07-23 18:33:17 -04:00
Sentinal [4]*Card `json:"sentinal"`
Scourge [4]*Card `json:"scourge"`
2021-07-15 19:26:57 -04:00
}
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)},
2021-07-15 19:26:57 -04:00
}
}
2021-07-16 14:53:45 -04:00
func (b *Board) String() string {
2021-07-25 15:33:47 -04:00
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])
2021-07-16 14:53:45 -04:00
}
2021-07-23 18:33:17 -04:00
func (b *Board) GetRow(id int) []*Card {
if id == SentinalID {
2021-07-15 19:26:57 -04:00
return b.Sentinal[:]
} else {
return b.Scourge[:]
}
}
2021-07-23 18:33:17 -04:00
func (b *Board) GetCard(id int, pos int) *Card {
return b.GetRow(id)[pos]
2021-07-23 18:33:17 -04:00
}
2021-07-23 18:57:39 -04:00
func (b *Board) Remove(c *Card) {
for k, v := range b.Sentinal {
if v.Id == c.Id {
b.Sentinal[k] = NewEmpty(k)
2021-07-23 18:57:39 -04:00
}
}
for k, v := range b.Scourge {
if v.Id == c.Id {
b.Scourge[k] = NewEmpty(k)
2021-07-23 18:57:39 -04:00
}
}
}
2021-07-22 13:43:28 -04:00
func (b *Board) Empty(id int) bool {
res := true
row := b.GetRow(id)
for _, v := range row {
if !v.Empty() {
res = false
2021-07-22 13:43:28 -04:00
}
}
2021-07-22 13:43:28 -04:00
return res
}
2021-07-23 18:33:17 -04:00
func (b *Board) CanMove(id, src, dest int) bool {
brd := b.GetRow(id)
2021-07-25 15:33:47 -04:00
if src < 0 || src > 3 || dest < 0 || dest > 3 {
return false
}
2021-07-23 18:33:17 -04:00
if brd[src].Empty() {
return false
}
if !brd[dest].Empty() {
return false
}
if brd[src].Sick {
return false
}
return true
}
2021-07-15 19:26:57 -04:00
func (b *Board) Move(id, src, dest int) bool {
2021-07-25 15:33:47 -04:00
brd := b.GetRow(id)
2021-07-15 19:26:57 -04:00
brd[dest] = brd[src]
brd[src] = NewEmpty(src)
2021-07-15 19:26:57 -04:00
return true
}
2021-07-15 21:44:45 -04:00
func (b *Board) CanPlay(id int, c *Card, dest int) bool {
brd := b.GetRow(id)
2021-07-25 15:33:47 -04:00
if dest < 0 || dest > 3 {
return false
}
if !brd[dest].Empty() && !c.Spell {
2021-07-15 19:26:57 -04:00
return false
}
2021-07-15 21:44:45 -04:00
return true
}
2021-07-23 18:33:17 -04:00
func (b *Board) Play(id int, c *Card, dest int, should bool) bool {
brd := b.GetRow(id)
2021-07-23 18:33:17 -04:00
if should {
brd[dest] = c
c.Position = dest
2021-07-15 19:26:57 -04:00
} else {
return false
2021-07-15 19:26:57 -04:00
}
return true
}
2021-07-23 18:33:17 -04:00
func (b *Board) CanAttack(id, atk, def int) bool {
aBrd := b.GetRow(id)
2021-07-25 15:33:47 -04:00
if atk < 0 || atk > 3 || def < 0 || def > 3 {
return false
}
2021-07-23 18:33:17 -04:00
if aBrd[atk].Empty() || aBrd[atk].Sick || atk != def {
return false
}
return true
}
2021-07-25 15:33:47 -04:00
func (b *Board) Attack(id, atk, def int) int {
var aid, did int
2021-07-23 18:33:17 -04:00
if id == SentinalID {
2021-07-25 15:33:47 -04:00
aid = SentinalID
did = ScourgeID
2021-07-15 19:26:57 -04:00
} else {
2021-07-25 15:33:47 -04:00
aid = ScourgeID
did = SentinalID
2021-07-15 19:26:57 -04:00
}
2021-07-25 15:33:47 -04:00
attacker := b.GetRow(aid)[atk]
defender := b.GetRow(did)[def]
if defender.Empty() {
2021-07-15 19:26:57 -04:00
//health damage
return 1
}
//Do actual battle math
2021-07-25 15:33:47 -04:00
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
2021-07-15 19:26:57 -04:00
}
return -1
}
2021-07-25 15:33:47 -04:00
func (b *Board) ResetCards() {
for _, v := range b.Sentinal {
v.Power = v.BasePower
}
for _, v := range b.Scourge {
v.Power = v.BasePower
}
}