TicTacToe

package module
v0.0.0-...-37fe5d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2015 License: MIT Imports: 2 Imported by: 1

README

TicTacToe

A simple human vs CPU TicTacToe game, powered by Go. You can play it here: http://hectorj.net/TicTacToe/

Build Status GoDoc Coverage Status

Usage

There is currently 2 ways of using this package:

  • Build your own UI and just use the library
  • go run TicTacToe/web/server/main.go -listen=":80" will start an HTTP server on port 80

Coming soonish: a GopherJS version

Story

I made this mainly to train myself in Go, but I chose TicTacToe because this game was one of my first successful project (as in working, finished, and doing someting "useful") as a kid.

At the time I was using C++ and Qt4 and implemented the minmax algorithm found on the internet. You can see my old code there: https://github.com/hectorj/TicTacToe-qt4

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

type Coordinates struct {
	X int
	Y int
}

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

func GridFromID

func GridFromID(ID uint32) Grid

GridFromID re-builds a grid from an ID

func NewGrid

func NewGrid() Grid

NewGrid instantiates a new blank grid, ready to be played.

type NullPlayer

type NullPlayer struct {
	Valid bool
	Value Player
}

NullPlayer represents the X player or the O player, or none of them

func (NullPlayer) String

func (p NullPlayer) String() string

type Player

type Player bool

Player represents the X player or the O player

const (
	// OPlayer is the player using circles
	OPlayer Player = false
	// XPlayer is the player using Xs
	XPlayer Player = true
	// FirstToPlay designates the first player
	FirstToPlay Player = XPlayer
)

func (Player) String

func (p Player) String() string

Directories

Path Synopsis
web
server command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL