Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewAllLinesIterator ¶
func NewAllLinesIterator() [8][3]Coordinates
NewAllLinesIterator allows you to range over all lines on the grid (rows, columns, and diagonals).
Example ¶
package main
import (
"fmt"
"github.com/hectorj/TicTacToe"
)
func main() {
for _, lineIterator := range TicTacToe.NewAllLinesIterator() {
for _, coordinates := range lineIterator {
fmt.Println(coordinates)
}
fmt.Println()
}
}
Output: {0 0} {1 0} {2 0} {0 1} {1 1} {2 1} {0 2} {1 2} {2 2} {0 0} {0 1} {0 2} {1 0} {1 1} {1 2} {2 0} {2 1} {2 2} {0 0} {1 1} {2 2} {0 2} {1 1} {2 0}
Types ¶
type Coordinates ¶
Coordinates points to a cell on the grid
func BestNextMove ¶
func BestNextMove(g Grid) Coordinates
BestNextMove analyzes the given grid and returns the best next move according to the "IA" (simple minmax algorithm)
Example ¶
grid := NewGrid()
// Let's have a game just between IA
var (
isOver bool
winner NullPlayer
)
for !isOver {
coordinatesToPlay := BestNextMove(grid)
grid.Play(coordinatesToPlay)
isOver, winner = grid.IsGameOver()
}
if !winner.Valid {
// Spoilers: it's always a draw game
fmt.Println("Draw game!")
} else {
fmt.Println(winner.String() + " wins!")
}
Output: Draw game!
func NewAllCellsIterator ¶
func NewAllCellsIterator() [9]Coordinates
NewAllCellsIterator allows you to range over all cells on the grid.
Example ¶
package main
import (
"fmt"
"github.com/hectorj/TicTacToe"
)
func main() {
for _, coordinates := range TicTacToe.NewAllCellsIterator() {
fmt.Println(coordinates)
}
}
Output: {0 0} {0 1} {0 2} {1 0} {1 1} {1 2} {2 0} {2 1} {2 2}
type Grid ¶
type Grid interface {
// GetID returns a unique ID corresponding to the state of the grid.
GetID() uint32
// GetNextID returns the ID of the grid after the given coordinates would be played.
GetNextID(Coordinates) uint32
// IsGameOver returns true if there is a winner (in which case the second value is OPlayer or XPlayer)
// or the grid is full
IsGameOver() (isOver bool, winner NullPlayer)
// OccupiedBy tells you if the cell at the given coordinates is occupied by OPlayer, XPlayer, or free
OccupiedBy(Coordinates) NullPlayer
// Play fills the cell at the given coordinates with the token of the active player.
Play(Coordinates)
// GetNextPlayer tells you which player is the current active player.
GetNextPlayer() Player
// Copy makes a copy of the grid which is not a reference (can be modified without altering the original).
Copy() Grid
}
Grid represents the game's board, with its 3x3 cells
type NullPlayer ¶
NullPlayer represents the X player or the O player, or none of them
func (NullPlayer) String ¶
func (p NullPlayer) String() string
Click to show internal directories.
Click to hide internal directories.