Welcome to gogame’s documentation!

Enums

class Color(value)[source]

Specify the color of a vertice

Bool(color):

Returns False if color is Empty, True otherwise

Empty

The vertice is Empty

Black

The vertice is owned by Black

White

The vertice is owned by White

Green

The vertice is owned by Green

Blue

The vertice is owned by Blue

Yellow

The vertice is owned by Yellow

Purple

The vertice is owned by Purple

Wall

The vertice is occupied by a Wall

is_player()[source]

Check if a color is a player or a special value (empty, wall, etc…)

Return type:

bool

Player

class Player(name=None, color=None)[source]

Represents a go player. This class has to be overridden to implement the play() method

Parameters:
  • name (Optional[str]) – The name of the player (only used to identify it)

  • color (Optional[Color]) – The color the player will player, if set to None, it’s automatically set by the board

property board: Board | None

Returns the associated board if any

property color: Color | None

Returns the color of the player if any

free_vertices()[source]

Returns a list of all empty vertices on the board

Return type:

list[tuple[int, int]]

get_board_matrix()[source]

Returns the current state of the board (0 for empty, 1 for the player, -1 for the opponent)

Return type:

ndarray

get_territories(color)[source]

Returns a list of territories owned by the opponent

Parameters:

color (Color) –

Return type:

list[Territory]

get_vertices(color)[source]

Returns a list of all vertices owned by the opponent

Parameters:

color (Color) –

Return type:

list[tuple[int, int]]

property is_in_game: bool

Check if the player is currently linked to a board

my_territories()[source]

Returns a list of territories owned by the player

Return type:

list[Territory]

my_vertices()[source]

Returns a list of all vertices owned by the player

Return type:

list[tuple[int, int]]

abstract play()[source]

This method has to be overridden by subclasses It’s called when the player has to play and must return a 2-tuple (x, y) representing a move, or None for skipping the turn

Return type:

tuple[int, int] | None

playable_moves()[source]

Returns the list of all vertices available for playing

Return type:

list[tuple[int, int]]

territories()[source]

Returns all territories on the board

Return type:

list[Territory]

Board

class Board(*, size=19, show=False)[source]

Represents the goban of a game

Note

Board vertices can be accessed through indices:

>>> b = Board()
>>> b[0,0]
<Color.Empty: 0>
Parameters:
  • size (Union[int, tuple[int, int]]) – The size of the board, either an int for a square board, or a tuple (height, width)

  • show (bool) – Indicates if the board should be displayed after each move

around(x, y, include_center=False)[source]

A quick way to get vertices around a given point

Parameters:
  • x (int) – The x coordinate of the point

  • y (int) – The y coordinate of the point

  • include_center (bool) – Wether to include the given point or not

Yields:

The points around

Return type:

Generator[tuple[int, int], None, None]

classmethod circular(size=19, show=False)[source]

A quick way to generate a circular board using walls

Parameters:
  • size (int | tuple[int, int]) – An int denoting the diameter of the circle, or a 2-tuple denoting the two diameter of an oval

  • show (bool) – Indicates if the board should be displayed after each move

Returns:

The generated board

Return type:

Board

clear_players()[source]

Unlinks all players from the board

clone()[source]

Returns a deep copy of the board

Return type:

Board

display()[source]

Displays the board as a numpy matrix

classmethod from_grid(grid)[source]

Initialize a board from an 2D array of Color

Parameters:

grid (ndarray) – A 2D array of Color representing the state of the board

Returns:

The new created board

Return type:

Board

get_territory(x, y)[source]

Get a territory from a vertice

Parameters:
  • x (int) – The x coordinate of the territory to get

  • y (int) – The y coordinate of the territory to get

Returns:

The territory which owns the vertice if any

Return type:

Territory | None

is_playable(x, y, color)[source]

Checks if a move is valid

Parameters:
  • x (int) – The x coordinate to check

  • y (int) – The y coordinate to check

  • color (Color) – The color of the player to check

Returns:

Indicates if the move is valid

Return type:

bool

join(player)[source]

Links a player to the board for a game

Parameters:

player (Player) – The player to link

Raises:

ValueError – There are already two players linked

matrix()[source]

Returns the current state of the board as a numpy matrix to facilitate move calculation

Returns:

The matrix representing the board

Return type:

ndarray

next_player(player=None)[source]

Returns the player who is next in the rotation of the game

Parameters:

player (Optional[Player]) – The reference player. Default to the player currently playing.

Raises:

ValueError – The player you gave is not on the board or there is no player on the board

Returns:

The next player

Return type:

Player

play(x, y, *, color)[source]

Play a move manually without using Player object

Parameters:
  • x (int) – The x coordinate of the move to play

  • y (int) – The y coordinate of the move to play

  • color (Color) – The color of the move to play

Raises:

ValueError – The move is invalid, or it’s the wrong player

playable_moves(color)[source]

Gives the list of valid move for a given color

Parameters:

color (Color) – The player

Returns:

A list of all vertices where the player can play

Return type:

list[tuple[int, int]]

prisoners(color)[source]

Get the number of prisoners owned by a player

Parameters:

color (Color) – The color of the player

Returns:

The number of prisoners

Return type:

int

remove_player(player)[source]

Unlinks a player from the board

Parameters:

player (Player) – The player to unlink

Raises:

ValueError – This player is not linked to the board

run_game(max_turn=1000, max_duration=None)[source]

Runs a game on this board between two players. The players have to be linked to the board with join() before

Parameters:
  • max_turn (Optional[int]) – The maximum number of move before ending the game

  • max_duration (Optional[int]) – The maximum number of seconds before ending the game

Raises:
  • ValueError – Not enough players to start the game

  • TypeError – A player returns an invalid move type

Returns:

The player who wins the game

Return type:

Player

score(color)[source]

Returns the score of a player i.e. the number of vertices belonging to the player + the number of his prisoners

Parameters:

color (Color) – The color of the player

Returns:

The score of the given player

Return type:

int

skip(*, color)[source]

Skip a turn manually without using Player object

Parameters:

color (Color) – The color of the move to play

Raises:

ValueError – It’s the wrong player

Returns:

True if the game is over because it’s the second skip in a row, False otherwise

Return type:

bool

territories(color=None)[source]

Returns territories currently on the board. If a color is specified, only territories of the given color are returned

Parameters:

color (Color | None) – The color of the territories to get

Returns:

A list of territories

Return type:

list[Territory]

vertices(color)[source]

Get all vertices from a given color

Parameters:

color (Color) – The color of the vertices to get

Returns:

The list of vertices

Return type:

list[tuple[int, int]]

winner()[source]

Returns the current winner of the board by comparing the scores of both player In case of equality, White wins

Returns:

The player who currently leads the game

Return type:

Player

Territory

Attributes
Methods
class Territory(*, x=None, y=None, vertices=None, board)[source]

Represents a territory i.e. a list of nearby vertices of the same color

Note

When using the x, y initializer, the territory is built by exploring the board while with vertices it only uses the given vertices without any exploration

Parameters:
  • x (Optional[int]) – The x coordinate of the vertice to use to initiate the territory. This cannot be mixed with the vertices parameter

  • y (Optional[int]) – The y coordinate of the vertice to use to initiate the territory. This cannot be mixed with the vertices parameter

  • vertices (Optional[list[tuple[int, int]]]) – A list of vertices to initiate the territory. This cannot be mixed with the x and y parameters

  • board (Board) – The board associated with the territory

Raises:
  • TypeError – Parameters are not of the right type

  • ValueError – Failed to create the territory with the given parameters

property board: Board

The board associated with the territory

clone(board=None)[source]

Returns a deep copy of the territory

Parameters:

board (Optional[Board]) – The board to link the new territory, if None it’s the same as the current territory

Returns:

The copy of the territory

Return type:

Territory

property color: Color

The color of the territory

freedom()[source]

Calculate the freedom of the territory, i.e. the vertices where it can expend

Returns:

The list of available vertices to expend the territory

Return type:

list[tuple[int, int]]

includes(x, y, color=None)[source]

Checks if a vertice is included in the territory

Parameters:
  • x (int) – The x coordinate of the vertice

  • y (int) – The y coordinate of the vertice

  • color (Color | None) – The color of the targeted vertice

Returns:

Indicates if the vertice is included or not

Return type:

bool

is_nearby(territory)[source]

Checks if a territory is connected

Parameters:

territory (Territory) – The territory to check

Returns:

Indicate if the territory is connected or not

Return type:

bool

is_touching(x, y)[source]

Checks if a vertice is touching the territory

Parameters:
  • x (int) – The x coordinate of the vertice

  • y (int) – The y coordinate of the vertice

Returns:

Indicate if the vertice is touching the territory

Return type:

bool

classmethod merge(*territories, with_vertice=None)[source]

Merge several connected territories into one

Parameters:
  • territories (Territory) – An argument list of territories to merge

  • with_vertice (tuple[int, int] | None) – A vertice to connect all territories, if none is specified, territories have to be already connected

Raises:
  • TypeError – Parameters aren’t of the right type

  • ValueError – Failed to merge territories

Returns:

The new territory

Return type:

Territory

property size: int

Returns the number of vertices in the territory

Indices and tables

Other Project