game

package
v0.0.0-...-c9dfc42 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountPieces

func CountPieces(board Board) (int, int)

CountPieces counts the number of pieces of each color on the board Returns the count of black pieces and white pieces

func CountPiecesBitBoard

func CountPiecesBitBoard(bb BitBoard) (int, int)

func HasAnyMoves

func HasAnyMoves(board Board, playerColor Piece) bool

HasAnyMoves checks if there are any valid moves for a given player color on a board

func IsGameFinished

func IsGameFinished(board Board) bool

IsGameFinished checks if the game is over on a given board by determining if any valid moves remain The game is finished when neither player has any valid moves

func IsGameFinishedBitBoard

func IsGameFinishedBitBoard(bb BitBoard) bool

func IsValidMove

func IsValidMove(board Board, playerColor Piece, pos Position) bool

IsValidMove checks if placing a piece of the given color at the specified position is a valid move. A move is valid if it results in flipping at least one opponent's piece.

Parameters:

  • board: The current game board state
  • playerColor: The color of the piece to be placed (Black or White)
  • pos: The position where the piece would be placed

Returns:

  • bool: true if the move is valid, false otherwise

Types

type BitBoard

type BitBoard struct {
	BlackPieces uint64
	WhitePieces uint64
}

func ApplyMoveToBitBoard

func ApplyMoveToBitBoard(board BitBoard, playerColor Piece, pos Position) (BitBoard, bool)

ApplyMoveToBitBoard applies a move to a bitboard and returns the new bitboard state

func GetNewBitBoardAfterMove

func GetNewBitBoardAfterMove(bb BitBoard, pos Position, player Piece) (BitBoard, bool)

GetNewBitBoardAfterMove returns a new bitboard state after applying a move

type Board

type Board [8][8]Piece

func ApplyMoveToBoard

func ApplyMoveToBoard(board Board, playerColor Piece, pos Position) (Board, bool)

ApplyMoveToBoard applies a move to a board and returns the new board state without modifying the original board. Returns a new board and whether the move was valid. ApplyMoveToBoard applies the specified move to the game board and returns the new board state.

This function performs the following steps: 1. Validates if the move is legal using IsValidMove 2. Places the player's piece at the specified position 3. Flips opponent pieces in all 8 directions according to Othello rules

Parameters:

  • board: The current state of the game board
  • playerColor: The color of the player making the move (BLACK or WHITE)
  • pos: The position where the player wants to place their piece

Returns:

  • The updated board after applying the move
  • A boolean indicating whether the move was successfully applied (true) or invalid (false)

If the move is invalid, the original board is returned unchanged with false as the second return value.

func GetNewBoardAfterMove

func GetNewBoardAfterMove(board Board, pos Position, player Piece) (Board, bool)

GetNewBoardAfterMove returns a new game state after applying a move

type Game

type Game struct {
	Board         Board
	Players       [2]Player
	CurrentPlayer Player
	NbMoves       int
	History       []Position
}

Game represents the state of an Othello game. It contains the game board, the two players, the current player's turn, and the number of moves that have been made in the game. This struct is used to maintain the complete state of a game session.

func NewGame

func NewGame(player1, player2 string) *Game

NewGame creates and initializes a new Othello game. It sets up the board with the standard initial position where four pieces are placed in the center of the board (two black and two white in a diagonal pattern). The function also initializes both players, sets Black as the first player to move, and initializes the move counter to zero. Returns a ready-to-play Game instance.

func (*Game) ApplyMove

func (g *Game) ApplyMove(pos Position) bool

ApplyMove applies a move to the current game state

func (*Game) CountPiecesMethod

func (g *Game) CountPiecesMethod() (int, int)

CountPiecesMethod is a method wrapper for CountPieces

func (*Game) DisplayBoard

func (g *Game) DisplayBoard(board Board)

DisplayBoard prints a representation of the board to the console DisplayBoard prints the current state of the Othello board to the console. The function displays column letters (A-H) across the top and row numbers (1-8) along the left side, using chess-style notation. Empty cells are shown as "·", black pieces as "●", and white pieces as "○".

Parameters:

  • board: The Board to display

func (*Game) GetNewBoardAfterMoveMethod

func (g *Game) GetNewBoardAfterMoveMethod(pos Position) (Board, bool)

GetNewBoardAfterMoveMethod is a method wrapper for GetNewBoardAfterMove

func (*Game) GetOtherPlayerMethod

func (g *Game) GetOtherPlayerMethod() Player

GetOtherPlayerMethod is a method wrapper for GetOtherPlayer

func (*Game) GetValidMovesForCurrentPlayer

func (g *Game) GetValidMovesForCurrentPlayer() []Position

GetValidMovesForCurrentPlayer is a method wrapper for the ValidMoves function

func (*Game) GetWinnerMethod

func (g *Game) GetWinnerMethod() Piece

GetWinnerMethod is a method wrapper for GetWinner

func (*Game) HasAnyMovesInGame

func (g *Game) HasAnyMovesInGame() bool

HasAnyMovesInGame is a method wrapper for HasAnyMoves

func (*Game) IsGameFinishedMethod

func (g *Game) IsGameFinishedMethod() bool

IsGameFinishedMethod is a method wrapper for IsGameFinished

type Piece

type Piece int
const (
	Empty Piece = 0
	White Piece = 1
	Black Piece = 2
)

func GetOpponentColor

func GetOpponentColor(playerColor Piece) Piece

GetOpponentColor returns the opposite color of the given player color

func GetWinner

func GetWinner(board Board) Piece

GetWinner returns the winner of the game (color with more pieces) If it's a tie, returns Empty GetWinner determines the winner of an Othello game based on the current board. It counts the number of black and white pieces on the board and returns: - Black if there are more black pieces - White if there are more white pieces - Empty if there's a tie (equal number of black and white pieces)

Parameters:

  • board: The game board to evaluate

Returns:

  • Piece: The winning piece color (Black, White) or Empty in case of a tie

type Player

type Player struct {
	Color Piece
	Name  string
}

func GetOtherPlayer

func GetOtherPlayer(currentColor Piece) Player

GetOtherPlayer returns the Player object with the opposite color GetOtherPlayer returns the opponent player given the current player's color. It takes an array of two players and the current player's color as arguments, then returns the player whose color differs from the current player's color. This function is useful for alternating turns in the game.

type Position

type Position struct {
	Row int8
	Col int8
}

func ValidMoves

func ValidMoves(board Board, playerColor Piece) []Position

ValidMoves returns all valid moves for a player on a given board

func ValidMovesBitBoard

func ValidMovesBitBoard(board BitBoard, playerColor Piece) []Position

ValidMovesBitBoard returns all valid moves for a player using state-of-the-art bitboard operations Uses optimized Kogge-Stone sliding attack generation for maximum performance

Jump to

Keyboard shortcuts

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