export board, convert to lib

This commit is contained in:
stryan 2022-02-21 19:07:28 -05:00
parent 4653dfc614
commit a501f1ec53
13 changed files with 45 additions and 251 deletions

View File

@ -1,4 +1,4 @@
package main package freego
import "errors" import "errors"

View File

@ -1,4 +1,4 @@
package main package freego
import "testing" import "testing"

View File

@ -1,7 +1,7 @@
// Code generated by "enumer -type=Colour"; DO NOT EDIT. // Code generated by "enumer -type=Colour"; DO NOT EDIT.
// //
package main package freego
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package main package freego
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package main package freego
import ( import (
"fmt" "fmt"

50
game.go
View File

@ -1,4 +1,4 @@
package main package freego
import ( import (
"errors" "errors"
@ -31,24 +31,24 @@ 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,
} }
} }
func (g *Game) String() string { func (g *Game) String() string {
var board string var board string
for i := 0; i < g.board.size; i++ { for i := 0; i < g.Board.size; i++ {
board = board + "|" board = board + "|"
for j := 0; j < g.board.size; j++ { for j := 0; j < g.Board.size; j++ {
board = board + g.board.board[i][j].String() board = board + g.Board.board[i][j].String()
} }
board = board + "|\n" board = board + "|\n"
} }
@ -61,20 +61,20 @@ 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)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if cmd.act != "-" && cmd.act != "x" { if cmd.act != "-" && cmd.act != "x" {
return nil, errors.New("invalid command action") return nil, errors.New("invalid command action")
} }
end, err := g.board.GetPiece(cmd.dstX, cmd.dstY) end, err := g.Board.GetPiece(cmd.dstX, cmd.dstY)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -124,13 +124,13 @@ func (g *Game) SetupPiece(x, y int, p *Piece) (bool, error) {
if p == nil { if p == nil {
return false, errors.New("Tried to setup a nil piece") return false, errors.New("Tried to setup a nil piece")
} }
if !g.board.validatePoint(x, y) { if !g.Board.validatePoint(x, y) {
return false, errors.New("Invalid location") return false, errors.New("Invalid location")
} }
if p.Owner != g.board.GetColor(x, y) { if p.Owner != g.Board.GetColor(x, y) {
return false, fmt.Errorf("Can't setup piece on enemy board: %v != %v", p.Owner, g.board.GetColor(x, y)) return false, fmt.Errorf("Can't setup piece on enemy board: %v != %v", p.Owner, g.Board.GetColor(x, y))
} }
return g.board.Place(x, y, p) return g.Board.Place(x, y, p)
} }
//Start start the game //Start start the game
@ -143,11 +143,11 @@ func (g *Game) Start() bool {
} }
func (g *Game) move(x, y, s, t int) (bool, error) { func (g *Game) move(x, y, s, t int) (bool, error) {
startPiece, err := g.board.GetPiece(x, y) startPiece, err := g.Board.GetPiece(x, y)
if err != nil { if err != nil {
return false, err return false, err
} }
endPiece, err := g.board.GetPiece(s, t) endPiece, err := g.Board.GetPiece(s, t)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -158,12 +158,12 @@ func (g *Game) move(x, y, s, t int) (bool, error) {
return false, nil return false, nil
} }
//attempt to remove starting piece first //attempt to remove starting piece first
err = g.board.Remove(x, y) err = g.Board.Remove(x, y)
if err != nil { if err != nil {
return false, err return false, err
} }
// then place piece in new location // then place piece in new location
res, err := g.board.Place(s, t, startPiece) res, err := g.Board.Place(s, t, startPiece)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -171,11 +171,11 @@ func (g *Game) move(x, y, s, t int) (bool, error) {
} }
func (g *Game) strike(x, y, s, t int) (bool, error) { func (g *Game) strike(x, y, s, t int) (bool, error) {
startPiece, err := g.board.GetPiece(x, y) startPiece, err := g.Board.GetPiece(x, y)
if err != nil { if err != nil {
return false, err return false, err
} }
endPiece, err := g.board.GetPiece(s, t) endPiece, err := g.Board.GetPiece(s, t)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -203,30 +203,30 @@ func (g *Game) strike(x, y, s, t int) (bool, error) {
switch r { switch r {
case -1: case -1:
//startPiece lost //startPiece lost
err = g.board.Remove(x, y) err = g.Board.Remove(x, y)
if err != nil { if err != nil {
return true, err return true, err
} }
case 0: case 0:
//tie //tie
err = g.board.Remove(x, y) err = g.Board.Remove(x, y)
err2 := g.board.Remove(s, t) err2 := g.Board.Remove(s, t)
if err != nil || err2 != nil { if err != nil || err2 != nil {
return true, fmt.Errorf("Errors: %v %v", err, err2) return true, fmt.Errorf("Errors: %v %v", err, err2)
} }
case 1: case 1:
//endPiece lost //endPiece lost
err := g.board.Remove(s, t) err := g.Board.Remove(s, t)
if err != nil { if err != nil {
return true, err return true, err
} }
//scouts replace the piece that was destroyed //scouts replace the piece that was destroyed
if startPiece.Rank == Scout { if startPiece.Rank == Scout {
err = g.board.Remove(x, y) err = g.Board.Remove(x, y)
if err != nil { if err != nil {
return true, err return true, err
} }
res, err := g.board.Place(s, t, startPiece) res, err := g.Board.Place(s, t, startPiece)
if err != nil { if err != nil {
return true, err return true, err
} }

View File

@ -1,4 +1,4 @@
package main package freego
import ( import (
"errors" "errors"
@ -8,7 +8,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
@ -19,7 +19,7 @@ func dummyMiniGame() (*Game, error) {
{2, 2, 1}, {2, 2, 1},
} }
for _, tt := range terrain { for _, tt := range terrain {
res, err := g.board.AddTerrain(tt.x, tt.y, tt.t) res, err := g.Board.AddTerrain(tt.x, tt.y, tt.t)
if err != nil { if err != nil {
return nil, err return nil, err
} }

206
input.go
View File

@ -1,206 +0,0 @@
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
// Dir represents a direction.
type Dir int
// represents the valid directions
const (
DirUp Dir = iota
DirRight
DirDown
DirLeft
)
type mouseState int
const (
mouseStateNone mouseState = iota
mouseStatePressing
mouseStateSettled
)
type touchState int
const (
touchStateNone touchState = iota
touchStatePressing
touchStateSettled
touchStateInvalid
)
// String returns a string representing the direction.
func (d Dir) String() string {
switch d {
case DirUp:
return "Up"
case DirRight:
return "Right"
case DirDown:
return "Down"
case DirLeft:
return "Left"
}
panic("not reach")
}
// Vector returns a [-1, 1] value for each axis.
func (d Dir) Vector() (x, y int) {
switch d {
case DirUp:
return 0, -1
case DirRight:
return 1, 0
case DirDown:
return 0, 1
case DirLeft:
return -1, 0
}
panic("not reach")
}
// Input represents the current key states.
type Input struct {
mouseState mouseState
mouseInitPosX int
mouseInitPosY int
mouseDir Dir
touches []ebiten.TouchID
touchState touchState
touchID ebiten.TouchID
touchInitPosX int
touchInitPosY int
touchLastPosX int
touchLastPosY int
touchDir Dir
}
// NewInput generates a new Input object.
func NewInput() *Input {
return &Input{}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func vecToDir(dx, dy int) (Dir, bool) {
if abs(dx) < 4 && abs(dy) < 4 {
return 0, false
}
if abs(dx) < abs(dy) {
if dy < 0 {
return DirUp, true
}
return DirDown, true
}
if dx < 0 {
return DirLeft, true
}
return DirRight, true
}
// Update updates the current input states.
func (i *Input) Update() {
switch i.mouseState {
case mouseStateNone:
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
i.mouseInitPosX = x
i.mouseInitPosY = y
i.mouseState = mouseStatePressing
}
case mouseStatePressing:
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
dx := x - i.mouseInitPosX
dy := y - i.mouseInitPosY
d, ok := vecToDir(dx, dy)
if !ok {
i.mouseState = mouseStateNone
break
}
i.mouseDir = d
i.mouseState = mouseStateSettled
}
case mouseStateSettled:
i.mouseState = mouseStateNone
}
i.touches = ebiten.AppendTouchIDs(i.touches[:0])
switch i.touchState {
case touchStateNone:
if len(i.touches) == 1 {
i.touchID = i.touches[0]
x, y := ebiten.TouchPosition(i.touches[0])
i.touchInitPosX = x
i.touchInitPosY = y
i.touchLastPosX = x
i.touchLastPosX = y
i.touchState = touchStatePressing
}
case touchStatePressing:
if len(i.touches) >= 2 {
break
}
if len(i.touches) == 1 {
if i.touches[0] != i.touchID {
i.touchState = touchStateInvalid
} else {
x, y := ebiten.TouchPosition(i.touches[0])
i.touchLastPosX = x
i.touchLastPosY = y
}
break
}
if len(i.touches) == 0 {
dx := i.touchLastPosX - i.touchInitPosX
dy := i.touchLastPosY - i.touchInitPosY
d, ok := vecToDir(dx, dy)
if !ok {
i.touchState = touchStateNone
break
}
i.touchDir = d
i.touchState = touchStateSettled
}
case touchStateSettled:
i.touchState = touchStateNone
case touchStateInvalid:
if len(i.touches) == 0 {
i.touchState = touchStateNone
}
}
}
// Dir returns a currently pressed direction.
// Dir returns false if no direction key is pressed.
func (i *Input) Dir() (Dir, bool) {
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
return DirUp, true
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
return DirLeft, true
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
return DirRight, true
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
return DirDown, true
}
if i.mouseState == mouseStateSettled {
return i.mouseDir, true
}
if i.touchState == touchStateSettled {
return i.touchDir, true
}
return 0, false
}

16
main.go
View File

@ -1,4 +1,4 @@
package main package freego
import ( import (
"errors" "errors"
@ -8,7 +8,7 @@ import (
//DummyGame Creates a dummygame //DummyGame Creates a dummygame
func DummyGame() (*Game, error) { func DummyGame() (*Game, error) {
g := &Game{ g := &Game{
board: NewBoard(4), Board: NewBoard(4),
state: gameSetup, state: gameSetup,
} }
//Setup terrain //Setup terrain
@ -19,7 +19,7 @@ func DummyGame() (*Game, error) {
{2, 2, 1}, {2, 2, 1},
} }
for _, tt := range terrain { for _, tt := range terrain {
res, err := g.board.AddTerrain(tt.x, tt.y, tt.t) res, err := g.Board.AddTerrain(tt.x, tt.y, tt.t)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -81,7 +81,7 @@ func addpiece(game *Game, rank int, c Colour, x int, y int) {
} }
func addriver(game *Game, x int, y int) { func addriver(game *Game, x int, y int) {
res, err := game.board.AddTerrain(x, y, 1) res, err := game.Board.AddTerrain(x, y, 1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -91,12 +91,12 @@ func addriver(game *Game, x int, y int) {
} }
func printboardcolours(g *Game) { func printboardcolours(g *Game) {
for i := range g.board.board { for i := range g.Board.board {
for j := range g.board.board[i] { for j := range g.Board.board[i] {
c := "X" c := "X"
if g.board.board[i][j].colour == Red { if g.Board.board[i][j].colour == Red {
c = "R" c = "R"
} else if g.board.board[i][j].colour == Blue { } else if g.Board.board[i][j].colour == Blue {
c = "B" c = "B"
} }
fmt.Printf("%v", c) fmt.Printf("%v", c)

View File

@ -1,4 +1,4 @@
package main package freego
//Rank represents the rank of a piece //Rank represents the rank of a piece
//go:generate enumer -type=Rank //go:generate enumer -type=Rank

View File

@ -1,4 +1,4 @@
package main package freego
//Player represents a player of the game //Player represents a player of the game
type Player interface { type Player interface {

View File

@ -1,7 +1,7 @@
// Code generated by "enumer -type=Rank"; DO NOT EDIT. // Code generated by "enumer -type=Rank"; DO NOT EDIT.
// //
package main package freego
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package main package freego
import ( import (
"errors" "errors"