command loop active, fix some obvious bugs

This commit is contained in:
stryan 2021-07-16 15:35:29 -04:00
parent c111e09ab5
commit 7a00469634
4 changed files with 48 additions and 30 deletions

View File

@ -39,6 +39,10 @@ func (b *Board) Move(id, src, dest int) bool {
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 {
@ -94,7 +98,7 @@ func (b *Board) Attack(id int, atk, def int) int {
if aBrd[atk].Empty() || !aBrd[atk].CanAttack(atk, def) {
return -1
}
aBrd[atk].Acted()
aBrd[atk].Act()
if dBrd[def].Empty() {
//health damage
if id == 1 {

18
card.go
View File

@ -8,7 +8,8 @@ type Card interface {
Endstep(g *Game) *Game
Enters(g *Game) *Game
Value() CardValue
Acted()
Act()
Acted() bool
Empty() bool
String() string
CanAttack(int, int) bool
@ -38,7 +39,11 @@ func (g *GenericCard) Value() CardValue {
return g.Val
}
func (g *GenericCard) Acted() {
func (g *GenericCard) Acted() bool {
return g.Sick
}
func (g *GenericCard) Act() {
g.Sick = true
}
@ -87,7 +92,7 @@ func NewCard(v int) Card {
return &EmptyCard{
&GenericCard{
Val: EmptyValue,
Sick: false,
Sick: true,
},
}
case 1:
@ -139,13 +144,6 @@ type AceCard struct {
*GenericCard
}
func (a *AceCard) CanAttack(x, y int) bool {
if x == y {
return true
}
return false
}
type FourCard struct {
*GenericCard
}

11
game.go
View File

@ -50,7 +50,7 @@ func NewGame() *Game {
}
func (g *Game) String() string {
return fmt.Sprintf("Sen(%v): %v\n\n%v\n\nSco(%v): %v\n%v %v\n%v\n", g.SentinalPlayer.Life, g.SentinalPlayer.Hand, g.GameBoard, g.ScourgePlayer.Life, g.ScourgePlayer.Hand, g.Status, g.CanDraw, g.CardBuffer)
return fmt.Sprintf("Sen(%v): %v\n\n%v\n\nSco(%v): %v\nStatus:%v Draw:%v Turn:%v\n%v\n", g.SentinalPlayer.Life, g.SentinalPlayer.Hand, g.GameBoard, g.ScourgePlayer.Life, g.ScourgePlayer.Hand, g.Status, g.CanDraw, g.CurrentTurn, g.CardBuffer)
}
func (g *Game) PlayerStateAct(id int, cmd string) *Game {
@ -93,6 +93,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game {
g.SentinalDeck.Cards = g.SentinalDeck.Cards[0 : len(g.SentinalDeck.Cards)-5]
g.ScourgePlayer.Hand = g.ScourgeDeck.Cards[len(g.ScourgeDeck.Cards)-5 : len(g.ScourgeDeck.Cards)]
g.SentinalDeck.Cards = g.ScourgeDeck.Cards[0 : len(g.ScourgeDeck.Cards)-5]
return g
case "s":
//start turn
@ -137,6 +138,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game {
}
return g
}
fmt.Println("invalid state command")
return nil
}
@ -163,7 +165,7 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
switch cmd_s[0] {
case "s":
//scry: return scry options off top of deck
if !g.CanDraw {
if !g.CanDraw || len(g.CardBuffer) > 0 {
return nil
}
g.CardBuffer = currD.Scry(curr.Life)
@ -219,13 +221,16 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
} else if res == 0 {
return g.GameBoard.GetRow(g.CurrentTurn)
} else {
fmt.Println("can't attack")
return []Card{}
}
case "p":
//play: return player boad or [] if invalid
if len(cmd_s) != 3 {
fmt.Println("not enough arguments")
return nil
}
fmt.Println("playing")
x_i, _ := strconv.Atoi(cmd_s[1])
y_i, _ := strconv.Atoi(cmd_s[2])
card := curr.Hand[x_i]
@ -238,9 +243,11 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
return g.GameBoard.GetRow(g.CurrentTurn)
} else {
fmt.Println("couldn't play")
return []Card{}
}
default:
fmt.Println("Invalid act command")
return nil
}
return nil

43
main.go
View File

@ -1,25 +1,34 @@
package main
import "fmt"
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
g := NewGame()
g.PlayerStateAct(1, "b")
g.PlayerStateAct(1, "s")
tmp := g.PlayerStateAct(1, "d")
fmt.Println("begin")
fmt.Println(tmp)
fmt.Println("scry")
scry := g.PlayerAct(1, "s")
fmt.Println(scry)
fmt.Println("draw")
g.PlayerAct(1, "d 1")
h2 := g.PlayerStateAct(1, "d")
fmt.Println(h2)
fmt.Println("play")
g.PlayerAct(1, "p 1 3")
b2 := g.PlayerStateAct(1, "d")
fmt.Println(b2)
for {
var t, cmd string
var i int
fmt.Print("> ")
_, err := fmt.Scanf("%s %d", &t, &i)
if t == "x" {
return
}
cmd_raw, _ := reader.ReadString('\n')
cmd = strings.TrimSpace(cmd_raw)
if t == "s" {
fmt.Println(g.PlayerStateAct(i, cmd))
} else if t == "a" {
fmt.Println(g.PlayerAct(i, cmd))
} else {
fmt.Println("error parsing")
fmt.Println(err)
}
}
}