Commit 46e9ec85 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

made all struct fields hidden

parent c095b5dc
Loading
Loading
Loading
Loading
+17 −22
Original line number Diff line number Diff line
@@ -11,42 +11,37 @@ import (

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

// NewGame ...
func NewGame() *Game {
	return &Game{
		Grid:    grid.NewGrid(),
		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.NewBot(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 % len(g.Players)
		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
		}
@@ -76,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
	}
+2 −2
Original line number Diff line number Diff line
package grid

// Cell ...
type Cell interface {
// Celler ...
type Celler interface {
	Symbol() string
	IsEmpty() bool
	IsNought() bool
+12 −7
Original line number Diff line number Diff line
@@ -8,27 +8,32 @@ import (

// Grid ...
type Grid struct {
	Data map[Position]Cell
	data map[Position]Celler
}

// NewGrid ...
func NewGrid() *Grid {
	g := &Grid{
		Data: make(map[Position]Cell),
		data: make(map[Position]Celler),
	}

	for y := 0; y < 3; y++ {
		for x := 0; x < 3; x++ {
			g.Data[Pos(x, y)] = &empty.Empty{}
			g.SetCell(x, y, empty.NewEmpty())
		}
	}

	return g
}

// Pos ...
func Pos(x, y int) Position {
	return Position{X: x, Y: y}
// Cell ...
func (g *Grid) Cell(x, y int) Celler {
	return g.data[Pos(x, y)]
}

// SetCell ...
func (g *Grid) SetCell(x, y int, c Celler) {
	g.data[Pos(x, y)] = c
}

// String ...
@@ -43,7 +48,7 @@ func (g *Grid) String() string {

	for y := 0; y < 3; y++ {
		for x := 0; x < 3; x++ {
			sb.WriteString(g.Data[Pos(x, y)].Symbol())
			sb.WriteString(g.Cell(x, y).Symbol())
			switch x {
			case 0, 1:
				sb.WriteString("┃")
+13 −2
Original line number Diff line number Diff line
@@ -2,6 +2,17 @@ package grid

// Position ...
type Position struct {
	X int
	Y int
	x int
	y int
}

// Pos ...
func Pos(x, y int) Position {
	xx, yy := x%3, y%3
	return Position{x: xx, y: yy}
}

// PosXY ...
func PosXY(p Position) (int, int) {
	return p.x, p.y
}
+1 −1
Original line number Diff line number Diff line
@@ -5,6 +5,6 @@ import "repositories.muehmer.net/bsmrgo/tictactoe/grid"
// Player ...
type Player interface {
	Name() string
	Cell() grid.Cell
	Cell() grid.Celler
	Next(*grid.Grid) grid.Position
}
Loading