export gamestate
This commit is contained in:
parent
a501f1ec53
commit
9765cadedf
24
game.go
24
game.go
@ -32,14 +32,14 @@ const (
|
|||||||
//Game represents general game state
|
//Game represents general game state
|
||||||
type Game struct {
|
type Game struct {
|
||||||
Board *Board
|
Board *Board
|
||||||
state GameState
|
State GameState
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewGame creates a new game and sets the state to lobby
|
//NewGame creates a new game and sets the state to lobby
|
||||||
func NewGame() *Game {
|
func NewGame() *Game {
|
||||||
return &Game{
|
return &Game{
|
||||||
Board: NewBoard(8),
|
Board: NewBoard(8),
|
||||||
state: gameLobby,
|
State: gameLobby,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,18 +53,18 @@ func (g *Game) String() string {
|
|||||||
board = board + "|\n"
|
board = board + "|\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("Status: %v\n%v", g.state, board)
|
return fmt.Sprintf("Status: %v\n%v", g.State, board)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Parse a given RawCommand, return a ParsedCommand or an error
|
//Parse a given RawCommand, return a ParsedCommand or an error
|
||||||
func (g *Game) Parse(p Player, cmd *RawCommand) (*ParsedCommand, error) {
|
func (g *Game) Parse(p Player, cmd *RawCommand) (*ParsedCommand, error) {
|
||||||
if g.state != gameTurnRed && g.state != gameTurnBlue {
|
if g.State != gameTurnRed && g.State != gameTurnBlue {
|
||||||
return nil, errors.New("game has not started")
|
return nil, errors.New("game has not started")
|
||||||
}
|
}
|
||||||
if !g.Board.validatePoint(cmd.srcX, cmd.srcY) || !g.Board.validatePoint(cmd.dstX, cmd.dstY) {
|
if !g.Board.validatePoint(cmd.srcX, cmd.srcY) || !g.Board.validatePoint(cmd.dstX, cmd.dstY) {
|
||||||
return nil, errors.New("invalid location in command")
|
return nil, errors.New("invalid location in command")
|
||||||
}
|
}
|
||||||
if (p.Colour() == Red && g.state != gameTurnRed) || (p.Colour() == Blue && g.state != gameTurnBlue) {
|
if (p.Colour() == Red && g.State != gameTurnRed) || (p.Colour() == Blue && g.State != gameTurnBlue) {
|
||||||
return nil, errors.New("not your turn")
|
return nil, errors.New("not your turn")
|
||||||
}
|
}
|
||||||
start, err := g.Board.GetPiece(cmd.srcX, cmd.srcY)
|
start, err := g.Board.GetPiece(cmd.srcX, cmd.srcY)
|
||||||
@ -105,10 +105,10 @@ func (g *Game) Mutate(cmd *ParsedCommand) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
if res {
|
if res {
|
||||||
if g.state == gameTurnRed {
|
if g.State == gameTurnRed {
|
||||||
g.state = gameTurnBlue
|
g.State = gameTurnBlue
|
||||||
} else if g.state == gameTurnBlue {
|
} else if g.State == gameTurnBlue {
|
||||||
g.state = gameTurnRed
|
g.State = gameTurnRed
|
||||||
} else {
|
} else {
|
||||||
return false, errors.New("bad game state")
|
return false, errors.New("bad game state")
|
||||||
}
|
}
|
||||||
@ -118,7 +118,7 @@ func (g *Game) Mutate(cmd *ParsedCommand) (bool, error) {
|
|||||||
|
|
||||||
//SetupPiece adds a piece to the board during setup
|
//SetupPiece adds a piece to the board during setup
|
||||||
func (g *Game) SetupPiece(x, y int, p *Piece) (bool, error) {
|
func (g *Game) SetupPiece(x, y int, p *Piece) (bool, error) {
|
||||||
if g.state != gameSetup {
|
if g.State != gameSetup {
|
||||||
return false, errors.New("Trying to setup piece when not in setup")
|
return false, errors.New("Trying to setup piece when not in setup")
|
||||||
}
|
}
|
||||||
if p == nil {
|
if p == nil {
|
||||||
@ -135,8 +135,8 @@ func (g *Game) SetupPiece(x, y int, p *Piece) (bool, error) {
|
|||||||
|
|
||||||
//Start start the game
|
//Start start the game
|
||||||
func (g *Game) Start() bool {
|
func (g *Game) Start() bool {
|
||||||
if g.state == gameSetup {
|
if g.State == gameSetup {
|
||||||
g.state = gameTurnRed
|
g.State = gameTurnRed
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
14
game_test.go
14
game_test.go
@ -9,7 +9,7 @@ import (
|
|||||||
func dummyMiniGame() (*Game, error) {
|
func dummyMiniGame() (*Game, error) {
|
||||||
g := &Game{
|
g := &Game{
|
||||||
Board: NewBoard(4),
|
Board: NewBoard(4),
|
||||||
state: gameSetup,
|
State: gameSetup,
|
||||||
}
|
}
|
||||||
//Setup terrain
|
//Setup terrain
|
||||||
terrain := []struct {
|
terrain := []struct {
|
||||||
@ -65,7 +65,7 @@ func TestNewGame(t *testing.T) {
|
|||||||
if g == nil {
|
if g == nil {
|
||||||
t.Fatal("couldn't create game")
|
t.Fatal("couldn't create game")
|
||||||
}
|
}
|
||||||
if g.state != gameLobby {
|
if g.State != gameLobby {
|
||||||
t.Error("Game created with weird state")
|
t.Error("Game created with weird state")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ func TestSetupPiece(t *testing.T) {
|
|||||||
if err == nil || res == true {
|
if err == nil || res == true {
|
||||||
t.Errorf("Expected to fail setup piece but didn't")
|
t.Errorf("Expected to fail setup piece but didn't")
|
||||||
}
|
}
|
||||||
g.state = gameSetup
|
g.State = gameSetup
|
||||||
res, err = g.SetupPiece(0, 0, p2)
|
res, err = g.SetupPiece(0, 0, p2)
|
||||||
if err == nil || res == true {
|
if err == nil || res == true {
|
||||||
t.Error("Expected to fail putting red piece on blue board")
|
t.Error("Expected to fail putting red piece on blue board")
|
||||||
@ -103,7 +103,7 @@ func TestStart(t *testing.T) {
|
|||||||
if res {
|
if res {
|
||||||
t.Fatal("expected to fail starting game due to state")
|
t.Fatal("expected to fail starting game due to state")
|
||||||
}
|
}
|
||||||
g.state = gameSetup
|
g.State = gameSetup
|
||||||
res = g.Start()
|
res = g.Start()
|
||||||
if !res {
|
if !res {
|
||||||
t.Fatal("expected game to start")
|
t.Fatal("expected game to start")
|
||||||
@ -115,7 +115,7 @@ func TestMiniCreation(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("mini game not created: %v", err)
|
t.Fatalf("mini game not created: %v", err)
|
||||||
}
|
}
|
||||||
if g.state != gameSetup {
|
if g.State != gameSetup {
|
||||||
t.Fatal("mini game not in right state")
|
t.Fatal("mini game not in right state")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,7 +128,7 @@ func TestMiniGameDemo(t *testing.T) {
|
|||||||
if !g.Start() {
|
if !g.Start() {
|
||||||
t.Fatal("could not start mini game")
|
t.Fatal("could not start mini game")
|
||||||
}
|
}
|
||||||
if g.state != gameTurnRed {
|
if g.State != gameTurnRed {
|
||||||
t.Error("game starting on wrong turn")
|
t.Error("game starting on wrong turn")
|
||||||
}
|
}
|
||||||
r := NewDummyPlayer(Red)
|
r := NewDummyPlayer(Red)
|
||||||
@ -177,7 +177,7 @@ func TestMiniGameFull(t *testing.T) {
|
|||||||
if !g.Start() {
|
if !g.Start() {
|
||||||
t.Fatal("could not start mini game")
|
t.Fatal("could not start mini game")
|
||||||
}
|
}
|
||||||
if g.state != gameTurnRed {
|
if g.State != gameTurnRed {
|
||||||
t.Error("game starting on wrong turn")
|
t.Error("game starting on wrong turn")
|
||||||
}
|
}
|
||||||
r := NewDummyPlayer(Red)
|
r := NewDummyPlayer(Red)
|
||||||
|
Loading…
Reference in New Issue
Block a user