mancala

package
v0.0.0-...-59b50ae Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

mancala is a library for the game of Mancala. It includes the core game logic and added functionality like algorithmic opponents. It is designed to be used by a UI, such as the tui package in the same repository.

Index

Constants

View Source
const (
	Player1Turn = 1
	Player2Turn = 2
)

The value of Turn should always be one of these constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoardSide

type BoardSide struct {
	Player Player
	Pits   []int
	Store  int
}

BoardSide represents one side of a Mancala board. It contains:

  • a Player,
  • a slice of integers (Pits) representing the number of stones in each pit,
  • and an integer (Store) representing the number of stones in the store.

func NewBoardSide

func NewBoardSide(player Player, stonesPerPit int, pitsPerSide int) *BoardSide

NewBoardSide creates a new BoardSide with the given player, number of stones per pit, and number of pits per side.

func (*BoardSide) ArePitsEmpty

func (side *BoardSide) ArePitsEmpty() bool

ArePitsEmpty returns true if all pits are empty.

func (*BoardSide) GetOpposingPitIndex

func (side *BoardSide) GetOpposingPitIndex(pitIndex int) int

GetOpposingPitIndex returns the index of the pit on the opposing side of the board. This takes into account the pits being reversed for the opposing player.

func (*BoardSide) GetScore

func (side *BoardSide) GetScore() int

GetScore returns the total number of stones on this side (pits and store).

func (*BoardSide) GetStones

func (side *BoardSide) GetStones(pitIndex int) int

GetStones returns the number of stones in the given pit and removes them from the pit.

func (*BoardSide) ValidatePitIndex

func (side *BoardSide) ValidatePitIndex(pitIndex int) error

ValidatePitIndex returns an error if the given pit index is invalid. Otherwise, it returns nil.

type Game

type Game struct {
	Side1 *BoardSide
	Side2 *BoardSide
	Turn  uint8
}

Game represents a game of Mancala. It contains: - a BoardSide for each player (Side1, Side2), - and an unsigned integer (Turn) representing which player's turn it is.

func NewGame

func NewGame(player1 Player, player2 Player, stonesPerPit int, pitsPerSide int) *Game

NewGame creates a new game of Mancala with the given players, number of stones per pit, and number of pits per side.

func (*Game) AlternateTurn

func (g *Game) AlternateTurn()

AlternateTurn alternates the turn between players.

func (*Game) ExecuteMove

func (g *Game) ExecuteMove(pitIndex int) error

ExecuteMove executes a move for the current player. It returns an error if the game is over or if the given pit index is invalid.

func (*Game) IsOver

func (g *Game) IsOver() bool

IsOver returns true if the game is over, false otherwise.

type Human

type Human struct {
	Name  string
	Score int
}

Human represents a human player in a Mancala game. It contains a name and a score.

func NewHuman

func NewHuman(name string) *Human

NewHuman creates a new Human with the given name.

func (*Human) GetName

func (human *Human) GetName() string

GetName returns the human's name.

func (*Human) GetScore

func (human *Human) GetScore() int

GetScore returns the human's score.

func (*Human) SetScore

func (human *Human) SetScore(score int)

SetScore sets the human's score.

type MinimaxBot

type MinimaxBot struct {
	Name  string
	Score int
}

MinimaxBot represents a bot that uses the minimax algorithm to determine its next move. It contains a name and a score.

func NewMinimaxBot

func NewMinimaxBot(name string) *MinimaxBot

NewMinimaxBot creates a new MinimaxBot with the given name.

func (*MinimaxBot) GetMove

func (bot *MinimaxBot) GetMove(game *Game) int

GetMove returns the bot's next move using the minimax algorithm.

func (*MinimaxBot) GetName

func (bot *MinimaxBot) GetName() string

GetName returns the bot's name.

func (*MinimaxBot) GetScore

func (bot *MinimaxBot) GetScore() int

GetScore returns the bot's score.

func (*MinimaxBot) SetScore

func (bot *MinimaxBot) SetScore(score int)

SetScore sets the bot's score.

type Player

type Player interface {
	GetName() string    // GetName returns the player's name.
	GetScore() int      // GetScore returns the player's score.
	SetScore(score int) // SetScore sets the player's score.
}

Player represents a player in a Mancala game.

Jump to

Keyboard shortcuts

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