snengame/board.go

150 lines
2.3 KiB
Go
Raw Normal View History

2021-07-15 19:26:57 -04:00
package main
type Board struct {
Sentinal [4]Card
Scourge [4]Card
}
func NewBoard() *Board {
return &Board{
2021-07-15 20:58:21 -04:00
Sentinal: [4]Card{nil, nil, nil, nil},
Scourge: [4]Card{nil, nil, nil, nil},
2021-07-15 19:26:57 -04:00
}
}
func (b *Board) GetRow(id int) []Card {
if id == 1 {
return b.Sentinal[:]
} else {
return b.Scourge[:]
}
}
func (b *Board) Move(id, src, dest int) bool {
var brd [4]Card
if id == 1 {
brd = b.Sentinal
} else {
brd = b.Scourge
}
2021-07-15 20:58:21 -04:00
if brd[src] == nil {
2021-07-15 19:26:57 -04:00
return false
}
2021-07-15 20:58:21 -04:00
if brd[dest] != nil {
2021-07-15 19:26:57 -04:00
return false
}
brd[dest] = brd[src]
2021-07-15 20:58:21 -04:00
brd[src] = nil
2021-07-15 19:26:57 -04:00
if id == 1 {
b.Sentinal = brd
} else {
b.Scourge = brd
}
return true
}
2021-07-15 21:44:45 -04:00
func (b *Board) CanPlay(id int, c *Card, dest int) bool {
2021-07-15 19:26:57 -04:00
var brd [4]Card
if id == 1 {
brd = b.Sentinal
} else {
brd = b.Scourge
}
2021-07-15 20:58:21 -04:00
if brd[dest] != nil {
2021-07-15 19:26:57 -04:00
return false
}
2021-07-15 21:44:45 -04:00
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] != nil {
return false
}
brd[dest] = *c
2021-07-15 19:26:57 -04:00
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
}
2021-07-15 20:58:21 -04:00
if aBrd[atk] == nil || !aBrd[atk].CanAttack(atk, def) {
2021-07-15 19:26:57 -04:00
return -1
}
2021-07-15 20:58:21 -04:00
aBrd[atk].Acted()
if dBrd[def] == nil {
2021-07-15 19:26:57 -04:00
//health damage
if id == 1 {
b.Sentinal = aBrd
b.Scourge = dBrd
} else {
b.Scourge = aBrd
b.Sentinal = dBrd
}
return 1
}
//Do actual battle math
2021-07-15 20:58:21 -04:00
attacker := aBrd[atk].Value()
defender := dBrd[def].Value()
dBrd[def].Acted()
2021-07-15 19:26:57 -04:00
//check atk buffs
for i, v := range aBrd {
2021-07-15 20:58:21 -04:00
if v.Value() == Two {
2021-07-15 19:26:57 -04:00
attacker = attacker + 1
}
2021-07-15 20:58:21 -04:00
if v.Value() == Three && (i == atk-1 || i == atk+1) {
2021-07-15 19:26:57 -04:00
attacker = attacker + 1
}
}
for i, v := range dBrd {
2021-07-15 20:58:21 -04:00
if v.Value() == Two {
2021-07-15 19:26:57 -04:00
defender = defender + 1
}
2021-07-15 20:58:21 -04:00
if v.Value() == Three && (i == def-1 || i == def+1) {
2021-07-15 19:26:57 -04:00
defender = defender + 1
}
}
if attacker > defender {
2021-07-15 20:58:21 -04:00
dBrd[def] = nil
2021-07-15 19:26:57 -04:00
if id == 1 {
b.Sentinal = aBrd
b.Scourge = dBrd
} else {
b.Scourge = aBrd
b.Sentinal = dBrd
}
return 0
}
if attacker == defender {
2021-07-15 20:58:21 -04:00
aBrd[atk] = nil
dBrd[def] = nil
2021-07-15 19:26:57 -04:00
if id == 1 {
b.Sentinal = aBrd
b.Scourge = dBrd
} else {
b.Scourge = aBrd
b.Sentinal = dBrd
}
return 0
}
return -1
}