Commit 5e6c6ea8 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

snapshot after project re-org



Signed-off-by: default avatarBoris Mühmer <boris@muehmer.de>
parent d772eb15
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -7,8 +7,9 @@ import (
	"os"
	"time"

	"repositories.muehmer.net/bsmrgo/tic-tac-toe/pkg/ttt/game"
	"repositories.muehmer.net/bsmrgo/tic-tac-toe/pkg/ttt/grid"
	"repositories.muehmer.net/bsmrgo/tictactoe/game"
	"repositories.muehmer.net/bsmrgo/tictactoe/grid/cross"
	"repositories.muehmer.net/bsmrgo/tictactoe/grid/nought"
)

func main() {
@@ -24,8 +25,8 @@ func main() {
	fmt.Fprintf(os.Stderr, "Seed: %d\n", seed)
	rand.Seed(seed)

	g := game.New()
	g.AddPlayer(name1, &grid.Cross{})
	g.AddPlayer(name2, &grid.Nought{})
	g := game.NewGame()
	g.AddPlayer(name1, cross.NewCross())
	g.AddPlayer(name2, nought.NewNought())
	g.Run()
}
+81 −0
Original line number Diff line number Diff line
@@ -4,50 +4,44 @@ import (
	"fmt"
	"os"

	"repositories.muehmer.net/bsmrgo/tic-tac-toe/pkg/ttt/grid"
	"repositories.muehmer.net/bsmrgo/tic-tac-toe/pkg/ttt/player"
	"repositories.muehmer.net/bsmrgo/tic-tac-toe/pkg/ttt/players/bot"
	"repositories.muehmer.net/bsmrgo/tictactoe/grid"
	"repositories.muehmer.net/bsmrgo/tictactoe/player"
	"repositories.muehmer.net/bsmrgo/tictactoe/players/bot"
)

// Game ...
type Game struct {
	Grid    *grid.Grid
	Players map[int]player.Player
	grid    *grid.Grid
	players map[int]player.Player
}

// New ...
func New() *Game {
// NewGame ...
func NewGame() *Game {
	return &Game{
		Grid:    grid.New(),
		Players: make(map[int]player.Player),
		grid:    grid.NewGrid(),
		players: make(map[int]player.Player),
	}
}

// AddPlayer ...
func (g *Game) AddPlayer(name string, cell grid.Cell) error {
	c := len(g.Players)
	g.Players[c] = bot.New(name, cell)
func (g *Game) AddPlayer(name string, cell grid.Celler) error {
	c := len(g.players)
	g.players[c] = bot.NewBot(name, cell)
	return nil
}

// Run ...
func (g *Game) Run() {
	for i := 0; i < 9; i++ {
		pl := i % 2
		p := g.Players[pl]
		pos := p.Next(g.Grid)
		g.Grid.Data[pos] = p.Cell()
		pl := i % len(g.players)
		p := g.players[pl]
		pos := p.Next(g.grid)
		x, y := grid.PosXY(pos)
		g.grid.SetCell(x, y, p.Cell())

		c0 := g.Grid.Data[grid.Pos(0, 0)]
		c1 := g.Grid.Data[grid.Pos(1, 0)]
		c2 := g.Grid.Data[grid.Pos(2, 0)]
		if (c0 == c1) && (c1 == c2) && (c0 == c2) {
		fmt.Fprintf(os.Stderr, "Round %d: %s\n%s", i+1, p.Name(), g.grid)

		}

		fmt.Fprintf(os.Stderr, "Round %d: %s\n%s", i+1, p.Name(), g.Grid)

		if checkLines(g.Grid) {
		if checkLines(g.grid) {
			fmt.Fprintf(os.Stderr, "Player %s has won!\n", p.Name())
			return
		}
@@ -77,9 +71,9 @@ func checkLines(g *grid.Grid) bool {
}

func line(g *grid.Grid, x0, y0, x1, y1, x2, y2 int) bool {
	c0 := g.Data[grid.Pos(x0, y0)]
	c1 := g.Data[grid.Pos(x1, y1)]
	c2 := g.Data[grid.Pos(x2, y2)]
	c0 := g.Cell(x0, y0)
	c1 := g.Cell(x1, y1)
	c2 := g.Cell(x2, y2)
	if (!c0.IsEmpty()) && (c0 == c1) && (c1 == c2) && (c0 == c2) {
		return true
	}
+5 −4
Original line number Diff line number Diff line
@@ -4,7 +4,8 @@ import (
	"math/rand"
	"testing"

	"repositories.muehmer.net/bsmrgo/tic-tac-toe/pkg/ttt/grid"
	"repositories.muehmer.net/bsmrgo/tictactoe/grid/cross"
	"repositories.muehmer.net/bsmrgo/tictactoe/grid/nought"
)

func TestGame(t *testing.T) {
@@ -12,10 +13,10 @@ func TestGame(t *testing.T) {
	seed := int64(1557681173)
	rand.Seed(seed)

	c1 := &grid.Cross{}
	c2 := &grid.Nought{}
	c1 := cross.NewCross()
	c2 := nought.NewNought()

	g := New()
	g := NewGame()
	g.AddPlayer("Player 1", c1)
	g.AddPlayer("Player 2", c2)
	g.Run()

go.mod

0 → 100644
+3 −0
Original line number Diff line number Diff line
module repositories.muehmer.net/bsmrgo/tictactoe

go 1.13

grid/cell.go

0 → 100644
+9 −0
Original line number Diff line number Diff line
package grid

// Celler ...
type Celler interface {
	Symbol() string
	IsEmpty() bool
	IsNought() bool
	IsCross() bool
}
Loading