tictactoe

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

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

Go to latest
Published: Jul 10, 2016 License: MIT Imports: 6 Imported by: 0

README

Slack TicTacToe game

Turn based slack tic tac toe game.

TicTacToe board

Commands

Slack commands examples:

  • /ttt start - start a new game
  • /ttt move [1-9] - make move to cell
  • /ttt current - show the current game state
  • /ttt stats - show user stats, wins, losses etc [not implemented]
  • /ttt help - show user command help and how to play
  • /ttt ping - ping request, for development

TODO

  • Improve AI: instead of choosing random spots use MinMax based decision tree
  • Add font support for drawing, named cells

Documentation

Index

Constants

View Source
const (
	// MaxInt maximum field value
	MaxInt = 0xFF
	// MinInt minimum field value
	MinInt = -0xFF
)
View Source
const (
	// Height is also same
	Width  = 3
	Height = 3
)

Variables

This section is empty.

Functions

func GetXY

func GetXY(index uint8) (x, y uint8)

GetXY convert the index from 0..9 to coordinates X,Y Example: the 0 -> [0; 0], 3 - [0; 1]

func Loop

func Loop(fn func(uint8, uint8))

Loop helper function to replace the repeating double loop pattern The outer and inner loop size is fixed by the board width and height

Types

type Board

type Board struct {
	Field [3][3]uint8
}

type Player

type Player uint8
const (
	UnkownPlayer Player = iota
	MyPlayer
	OpponentPlayer
)

type Spot

type Spot struct {
	X, Y uint8
}

func AB

func AB(game TicTacToe, depth, maximizer, player uint8, a, b int) (score int, spot Spot)

func (*Spot) ToMove

func (s *Spot) ToMove() uint8

type State

type State int
const (
	GameOverState State = 1 << iota
	DrawState
	WinState
	TurnState
	StartState
)

func (State) String

func (s State) String() string

type TicTacToe

type TicTacToe struct {
	Board
	First  Player
	Second Player
	Turn   Player
	State
}

func CreateFromField

func CreateFromField(from [3][3]uint8, p1, p2, turn Player) (game TicTacToe)

CreateFromField is a method to quickly create TicTacToe structure

func GetNewGameState

func GetNewGameState(oldGame TicTacToe, x, y uint8) TicTacToe

func (*TicTacToe) GetBoardAsString

func (t *TicTacToe) GetBoardAsString() string

func (*TicTacToe) GetFreeSpots

func (t *TicTacToe) GetFreeSpots() []Spot

func (*TicTacToe) GetRandomFreeSpot

func (t *TicTacToe) GetRandomFreeSpot() (Spot, error)

func (*TicTacToe) HasWinner

func (t *TicTacToe) HasWinner() bool

func (*TicTacToe) InRow

func (t *TicTacToe) InRow(current uint8) bool

func (*TicTacToe) MakeTurn

func (t *TicTacToe) MakeTurn(x, y uint8) error

func (*TicTacToe) SelectRandomPlayer

func (t *TicTacToe) SelectRandomPlayer() Player

func (*TicTacToe) Start

func (t *TicTacToe) Start()

func (TicTacToe) String

func (t TicTacToe) String() string

func (*TicTacToe) ToggleTurn

func (t *TicTacToe) ToggleTurn() Player

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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