engine

package
v0.0.0-...-6b51b5a Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2017 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultCols     = 7
	DefaultRows     = 6
	DefaultNPlayers = 2
	DefaultNWin     = 4
	DefaultMode     = "terminal"
)

Common defaults.

View Source
const (
	Empty = iota
	Red
	Yellow
	Green
	Magenta
	Blue
	Cyan
	Black
	Stale

	PlayerUnicode = '●'
)

GridState is the enum values

Variables

View Source
var AvailablePlayers = []State{Red, Yellow, Green, Magenta, Blue, Cyan, Black}

AvailablePlayers is the list of available player colors.

View Source
var (
	ErrInvalidMove = errors.New("invalid move")
)

Common errors.

Functions

This section is empty.

Types

type Four

type Four struct {
	sync.RWMutex // Lock to protect the Players map.

	Content  [][]State `json:"content"`
	NWin     int       `json:"nwin"`
	NPlayers int       `json:"nplayers"`

	Columns      int   `json:"columns"`
	Rows         int   `json:"rows"`
	CurPlayerIdx int   `json:"cur_player_idx"`
	CurPlayer    State `json:"cur_player"`

	AvailablePlayers []State          `json:"available_players"`
	Players          map[State]string `json:"players"` // Used for the server mode.

	ActivityChan chan State `json:"-"`          // Channel populated with latest game state.
	GridState    State      `json:"grid_state"` // If not "Empty", then the game is finished.
}

Four holds the game state.

func NewConnectFour

func NewConnectFour(columns, rows, nPlayers, nWin int) (*Four, error)

NewConnectFour instantiates a new game. TODO: Use a single dimension slice.

func (*Four) ColumnCount

func (f *Four) ColumnCount(col int) int

ColumnCount returns the count of occupied cells in the requested column.

func (*Four) Compute

func (f *Four) Compute() State

Compute processes the current state and checks if one of the player won.

func (*Four) PlayerMove

func (f *Four) PlayerMove(player State, col int) (State, error)

PlayerMove plays a move for the given player.

func (*Four) Reset

func (f *Four) Reset() (*Four, error)

Reset restarts the game.

func (*Four) State

func (f *Four) State(x, y int) State

State return the state of the grid at the x/y position.

func (*Four) ValidateMove

func (f *Four) ValidateMove(player State, col int) error

ValidateMove checks if the given move is valid.

type State

type State int

State is the enum type for the grid state.

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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