freego/main.go

160 lines
2.8 KiB
Go
Raw Normal View History

2022-02-18 18:36:18 -05:00
package main
2022-02-21 14:22:13 -05:00
import (
"errors"
"fmt"
"log"
"time"
2022-02-18 18:36:18 -05:00
2022-02-21 14:22:13 -05:00
"github.com/hajimehoshi/ebiten/v2"
)
func dummyGame() (*Game, error) {
g := &Game{
board: NewBoard(4),
state: gameSetup,
}
//Setup terrain
terrain := []struct {
x, y, t int
}{
{1, 1, 1},
{2, 2, 1},
}
for _, tt := range terrain {
res, err := g.board.AddTerrain(tt.x, tt.y, tt.t)
if err != nil {
return nil, err
}
if !res {
return nil, errors.New("Error creating terrain")
}
}
pieces := []struct {
x, y int
p *Piece
}{
{0, 0, NewPiece(Flag, Blue)},
{3, 0, NewPiece(Spy, Blue)},
{2, 0, NewPiece(Captain, Blue)},
{3, 1, NewPiece(Marshal, Blue)},
{0, 1, NewPiece(Bomb, Blue)},
{1, 2, NewPiece(Flag, Red)},
{3, 2, NewPiece(Spy, Red)},
{2, 3, NewPiece(Captain, Red)},
{0, 2, NewPiece(Marshal, Red)},
{0, 3, NewPiece(Bomb, Red)},
}
for _, tt := range pieces {
res, err := g.SetupPiece(tt.x, tt.y, tt.p)
if err != nil {
return nil, fmt.Errorf("Piece %v,%v:%v", tt.x, tt.y, err)
}
if !res {
return nil, errors.New("error placing dummy piece")
}
}
_, err := g.SetupPiece(0, 0, NewPiece(Flag, Blue))
if err != nil {
return nil, err
}
return g, nil
}
2022-02-18 18:36:18 -05:00
func main() {
2022-02-20 18:36:52 -05:00
//red := NewDummyPlayer(Red)
//blue := NewDummyPlayer(Blue)
2022-02-21 14:22:13 -05:00
g, err := dummyGame()
if err != nil {
panic(err)
}
go func(g *Game) {
r := NewDummyPlayer(Red)
b := NewDummyPlayer(Blue)
g.Start()
var moves = []struct {
input string
player Player
res bool
}{
{"c3-b3", r, true},
{"c0-c1", b, true},
{"d2xd1", r, true},
{"c1-d1", b, true},
{"b3-c3", r, true},
{"d1xd2", b, true},
}
for i, tt := range moves {
time.Sleep(3 * time.Second)
log.Printf("playing move %v\n", i)
raw, err := NewRawCommand(tt.input)
if err != nil {
panic(err)
}
parsed, err := g.Parse(tt.player, raw)
if err != nil {
panic(err)
}
res, err := g.Mutate(parsed)
if err != nil {
panic(err)
}
if res {
log.Printf("move %v successful\n", i)
}
}
}(g)
viewer(g)
2022-02-18 18:36:18 -05:00
return
}
2022-02-21 14:22:13 -05:00
func viewer(g *Game) {
v, err := NewViewer(g)
if err != nil {
panic(err)
}
ebiten.SetWindowSize(800, 640)
ebiten.SetWindowTitle("Freego")
if err := ebiten.RunGame(v); err != nil {
panic(err)
}
}
2022-02-18 18:36:18 -05:00
func addpiece(game *Game, rank int, c Colour, x int, y int) {
2022-02-20 18:36:52 -05:00
res, err := game.SetupPiece(x, y, NewPieceFromInt(rank, c))
2022-02-18 18:36:18 -05:00
if err != nil {
panic(err)
}
if !res {
panic("can't setup")
}
}
func addriver(game *Game, x int, y int) {
2022-02-20 16:07:20 -05:00
res, err := game.board.AddTerrain(x, y, 1)
2022-02-18 18:36:18 -05:00
if err != nil {
panic(err)
}
if !res {
panic("can't river")
}
}
2022-02-20 18:36:52 -05:00
func printboardcolours(g *Game) {
for i := range g.board.board {
for j := range g.board.board[i] {
c := "X"
if g.board.board[i][j].colour == Red {
c = "R"
} else if g.board.board[i][j].colour == Blue {
c = "B"
}
fmt.Printf("%v", c)
}
fmt.Println("")
}
}