mnkgame

package
v0.0.0-...-66662a9 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0, Apache-2.0 Imports: 7 Imported by: 0

README

m-n-k game library package.

This package is a Go library for implementing a variety of the classic m-n-k type games such as Tic-Tac-Toe, Naughts and Crosses, Connect 4, Gomoku and many others.

Per https://en.wikipedia.org/wiki/M,n,k-game: An m,n,k-game is an abstract board game in which two players take turns in placing a stone of their color on an m-by-n board, the winner being the player who first gets k stones of their own color in a row, horizontally, vertically, or diagonally. Thus, tic-tac-toe is the 3,3,3-game and free-style gomoku is the 15,15,5-game. An m,n,k-game is also called a k-in-a-row game on an m-by-n board.

Documentation

Overview

Package mnkgame implements the main parts of an m-n-k boardgame. (such as Tic Tac Toe, or Connect Four.)

Index

Constants

This section is empty.

Variables

View Source
var (
	MarkerEmpty      = Marker(" ")
	MarkerX          = Marker(blackX)
	MarkerWhiteStone = Marker(filledWhiteCircle)
	MarkerBlackStone = Marker(filledBlackCircle)
)

Predefine some markers.

TODO(rsned): Add more markers to choose from.

View Source
var (
	Player1 = &Player{
		id:          "1",
		displayName: "Player 1",
		marker:      MarkerX,
		playerType:  playerTypeHuman,
	}

	Player2 = &Player{
		id:          "2",
		displayName: "Player 2",
		marker:      MarkerWhiteStone,
		playerType:  playerTypeHuman,
	}

	PlayerComputer1 = &Player{
		id:          "1001",
		displayName: "Computer Player Player 1",
		marker:      MarkerWhiteStone,
		playerType:  playerTypeComputerRandom,
	}

	PlayerComputer2 = &Player{
		id:          "1002",
		displayName: "Computer Player Player 2",
		marker:      MarkerBlackStone,
		playerType:  playerTypeComputerRandom,
	}
)

Predefine some players that can be used in games.

Functions

This section is empty.

Types

type Board

type Board struct {
	// contains filtered or unexported fields
}

Board represents the cells and their states comprising an m-n-k game. The board has a variety of optionally set attributes to make parts of the game clearer such as custom labels.

TODO(rsned): Separate rendering of board from its state management.

func (*Board) ApplyMove

func (b *Board) ApplyMove(player *Player, move string) error

ApplyMove applies the given move for the given player to the board. If there are errors preventing the move, they are returned.

func (*Board) OpenPositions

func (b *Board) OpenPositions() []string

OpenPositions returns the set all possible cells that have not yet been filled. If there are notation labels, those values are returned. Otherwise, a list of cell coordinates is returned.

func (*Board) Outcome

func (b *Board) Outcome() (player1, player2 Outcome)

Outcome reports the game outcome state for both players.

func (*Board) SetLabels

func (b *Board) SetLabels(rowLabels, colLabels []string)

SetLabels sets the given set of labels for the rows and columns in the board and updates the corresponding state elements of the board.

func (*Board) String

func (b *Board) String() string

String returns a fixed width layout text version of the current boards state.

TODO(rsned): Consider renaming this method and leaving String() as a simpler state dump of the instance.

type BoardOptions

type BoardOptions struct {
	HasOuterBorder bool // Should there be a line around the labels outside the main board.
	HasInnerBorder bool // Should there be a line around the main board area.
	HasInnerGrid   bool // Should we render the lines separating each row and column.
	HasLabels      bool // Do we have labels to show.
	LabelWidth     int  // Width of longest label to be displayed.
	MarkerWidth    int  // Width of the widest player marker symbol.
	Padding        int  // Amount of whitespace on either side of labels and markers.
}

BoardOptions packages up the various settings used when rendering the game board.

type Coord

type Coord struct {
	Row int
	Col int
}

Coord is the coordinates of a cell in a game using a Top-Left origin.

func (Coord) String

func (c Coord) String() string

type Coords

type Coords []Coord

Coords is a slice of Coord values.

func (*Coords) Add

func (c *Coords) Add(coord Coord)

Add attempts to add the given value, skipping if the value already is in the slice.

type CoordsList

type CoordsList []Coords

CoordsList is a list of list of Coord.

func (*CoordsList) Add

func (c *CoordsList) Add(coords Coords)

Add attempts to add the given value, skipping if the value already is in this.

This method assuemes the input is in the same order as existing values. Permutations are NOT checked and are considered distinct.

type MNKGame

type MNKGame struct {
	// contains filtered or unexported fields
}

An MNKGame is an abstract board game in which two players take turns in placing a stone of their color on an m-by-n board, the winner being the player who first gets k stones of their own color in a row, horizontally, vertically, or diagonally.

func Connect4

func Connect4(p1, p2 *Player) *MNKGame

Connect4 returns a new instance using the parameters in a connect 4 game.

func TicTacToe

func TicTacToe(p1, p2 *Player) *MNKGame

TicTacToe returns a new instance of an m-n-k game as defined by the common Tic Tac Toe rules.

func (*MNKGame) ApplyMove

func (t *MNKGame) ApplyMove(player *Player, move string) error

ApplyMove attempts to apply the users choice of move. If any errors occur, such as an illegal move, the error will be non-nil.

func (*MNKGame) OpenPositions

func (t *MNKGame) OpenPositions() []string

OpenPositions returns a list of all the open positions on the board.

func (*MNKGame) Outcome

func (t *MNKGame) Outcome() (Outcome, Outcome)

Outcome reports the current status of the game for each player.

TODO(rsned): Convert this to take a player and return their outcome to make it easier to simplify the game loop.

func (*MNKGame) PotentialMoves

func (t *MNKGame) PotentialMoves() []string

PotentialMoves returns a list of potential moves available.

TODO(rsned): Augment this to support games like Nine Mens Morris and others that allow markers to move after they have been played.

func (*MNKGame) RenderBoard

func (t *MNKGame) RenderBoard() string

RenderBoard returns a string representation of the current board state.

type Marker

type Marker string

Marker represents the various pre-defined markers that may appear in the games.

type Outcome

type Outcome int

Outcome is an enumeration of the various possible states of a game.

const (
	OutcomeIncomplete Outcome = iota
	OutcomeWin
	OutcomeDraw
	OutcomeLoss
)

Define the enumeration of outcomes.

func (Outcome) String

func (r Outcome) String() string

type Player

type Player struct {
	// contains filtered or unexported fields
}

Player holds basic fields about a player in this game, primarily what type of player and what marker it uses.

func (*Player) SetComputer

func (p *Player) SetComputer()

SetComputer sets the player type to be a computer making random moves.

func (*Player) SetHuman

func (p *Player) SetHuman()

SetHuman updates the player type to be a human.

func (*Player) String

func (p *Player) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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