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() { if !brd[dest].Empty() {
return false return false
} }
if brd[src].Acted() {
return false
}
brd[dest].Act()
brd[dest] = brd[src] brd[dest] = brd[src]
brd[src] = NewCard(-1) brd[src] = NewCard(-1)
if id == 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) { if aBrd[atk].Empty() || !aBrd[atk].CanAttack(atk, def) {
return -1 return -1
} }
aBrd[atk].Acted() aBrd[atk].Act()
if dBrd[def].Empty() { if dBrd[def].Empty() {
//health damage //health damage
if id == 1 { if id == 1 {

18
card.go
View File

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

11
game.go
View File

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

43
main.go
View File

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