tictacstatemachine

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGameAlreadyStarted = errors.New("game already started")
	ErrGameHasAllPlayers  = errors.New("game is not waiting for player")
	ErrUniquePlayers      = errors.New("game can not have same player twice")
	ErrGameNotInProgress  = errors.New("cannot move, game is not in progress")
	ErrNotYourTurn        = errors.New("not your turn")
	ErrPositionTaken      = errors.New("position is taken")
	ErrGameFinished       = errors.New("game is finished")
	ErrInputInvalid       = errors.New("input is invalid")
)

Functions

func CachedGenerateWiningPositions

func CachedGenerateWiningPositions(rows, cols, winseq int) [][]Move

func GameRules

func GameRules(rows int, cols int, length int) (int, int, int)

func GenerateWiningPositions

func GenerateWiningPositions(inline int, rows, columns int) [][]Move

func IsGameFinished

func IsGameFinished(x State) bool

func NewMachine

func NewMachine() *machine.Machine[Command, State]

func NewMachineWithState

func NewMachineWithState(s State) *machine.Machine[Command, State]

func PrintGameRC

func PrintGameRC(w io.Writer, movesTaken map[Move]PlayerID, rows, cols int)

func ToMovesTaken

func ToMovesTaken(moves []Move) map[Move]PlayerID

func Wining

func Wining() [][]Move

func Wining3x3

func Wining3x3() [][]Move

Types

type CreateGameCMD

type CreateGameCMD struct {
	FirstPlayerID PlayerID
	BoardRows     int
	BoardCols     int
	WinningLength int
}

Commands that trigger state transitions.

type GameEndWithDraw

type GameEndWithDraw struct {
	TicTacToeBaseState

	MovesTaken map[Move]PlayerID
}

State of the game. Commands are used to update or change state

type GameEndWithWin

type GameEndWithWin struct {
	TicTacToeBaseState

	Winner         PlayerID
	WiningSequence []Move
	MovesTaken     map[Move]PlayerID
}

State of the game. Commands are used to update or change state

type GameProgress

type GameProgress struct {
	TicTacToeBaseState

	NextMovePlayerID Move
	MovesTaken       map[Move]PlayerID
	MovesOrder       []Move
}

State of the game. Commands are used to update or change state

type GameWaitingForPlayer

type GameWaitingForPlayer struct {
	TicTacToeBaseState
}

State of the game. Commands are used to update or change state

type GiveUpCMD

type GiveUpCMD struct {
	PlayerID PlayerID
}

Commands that trigger state transitions.

type JoinGameCMD

type JoinGameCMD struct{ SecondPlayerID PlayerID }

Commands that trigger state transitions.

type Move

type Move = string

Value objects, values that restrict cardinality of the state.

func CheckIfMoveWin

func CheckIfMoveWin(moves []Move, winseq [][]Move) ([]Move, bool)

func MkMove

func MkMove(x, y int) Move

func ParsePosition

func ParsePosition(position Move, boardRows int, boardCols int) (Move, error)

type MoveCMD

type MoveCMD struct {
	PlayerID PlayerID
	Position Move
}

Commands that trigger state transitions.

type PlayerID

type PlayerID = string

Value objects, values that restrict cardinality of the state.

type StartGameCMD

type StartGameCMD struct {
	FirstPlayerID  PlayerID
	SecondPlayerID PlayerID
	BoardRows      int
	BoardCols      int
	WinningLength  int
}

Commands that trigger state transitions.

type TicTacToeBaseState

type TicTacToeBaseState struct {
	FirstPlayerID  PlayerID
	SecondPlayerID PlayerID
	BoardRows      int
	BoardCols      int
	WinningLength  int
}

Jump to

Keyboard shortcuts

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