tictactoe

package module
v0.0.0-...-e573ff1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2021 License: MIT Imports: 4 Imported by: 4

README

tictactoe

Go Reference Build Status

Package tictactoe defines the game of tic-tac-toe.

Installation

go get github.com/shurcooL/tictactoe

Directories

Path Synopsis
cmd/tictactoe tictactoe plays a game of tic-tac-toe with two players.
player/bad Package bad contains a bad tic-tac-toe player.
player/human Package human contains a human-controlled tic-tac-toe player.
player/perfect Package perfect implements a perfect tic-tac-toe player.
player/random Package random implements a random player of tic-tac-toe.

License

Documentation

Overview

Package tictactoe defines the game of tic-tac-toe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Board

type Board struct {
	// Cells is a 3x3 matrix in row major order.
	// Cells[3*r + c] is the cell in the r'th row and c'th column.
	// Move m will affect Cells[m].
	Cells [9]State
}

Board for tic-tac-toe.

func (*Board) Apply

func (b *Board) Apply(move Move, mark State) error

Apply a move to this board. Mark is either X or O. If the move is not valid or not legal, the board is not modified and an error is returned.

func (Board) Condition

func (b Board) Condition() Condition

Condition returns the condition of the board.

func (Board) String

func (b Board) String() string

type CellClicker

type CellClicker interface {
	// CellClick is called when cell with
	// specified index is clicked.
	CellClick(index int)
}

CellClicker is an optional interface implemented by players that wish to be notified about cell clicks.

type Condition

type Condition uint8

Condition of the board configuration.

const (
	NotEnd Condition = iota
	XWon
	OWon
	Tie
)

Conditions of the board configuration.

func (Condition) String

func (c Condition) String() string

type Imager

type Imager interface {
	// Image returns the URL of the player's image.
	// Optimal size is 100 by 100 pixels (or higher for high DPI screens).
	Image() template.URL
}

Imager is an optional interface implemented by players that have an image that represents them.

type Move

type Move int

Move is the board cell index where to place one's mark, a value in range [0, 9).

A move is valid if it's in the range [0, 9). A move is legal if it can be applied to a given board configuration.

func (Move) Valid

func (m Move) Valid() error

Valid reports if the move is valid. A valid move may not be legal depending on the board configuration.

type Player

type Player interface {
	// Name of player.
	Name() string

	// Play takes a tic-tac-toe board b and returns the next move
	// for this player. Its mark is either X or O.
	// ctx is expected to have a deadline set, and Play may take time
	// to "think" until deadline is reached before returning.
	Play(ctx context.Context, b Board, mark State) (Move, error)
}

Player of tic-tac-toe.

type State

type State uint8

State of a board cell.

const (
	F State = iota // Free cell.
	X              // Cell with an X mark.
	O              // Cell with an O mark.
)

States of a board cell.

func (State) String

func (s State) String() string

Directories

Path Synopsis
cmd
tictactoe
tictactoe plays a game of tic-tac-toe with two players.
tictactoe plays a game of tic-tac-toe with two players.
player
bad
Package bad contains a bad tic-tac-toe player.
Package bad contains a bad tic-tac-toe player.
human
Package human contains a human-controlled tic-tac-toe player.
Package human contains a human-controlled tic-tac-toe player.
perfect
Package perfect implements a perfect tic-tac-toe player.
Package perfect implements a perfect tic-tac-toe player.
random
Package random implements a random player of tic-tac-toe.
Package random implements a random player of tic-tac-toe.

Jump to

Keyboard shortcuts

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