BitBully 0.0.39
Loading...
Searching...
No Matches
GameSolver::Connect4::Position Class Reference

#include <Position.hpp>

Public Types

using position_t = uint64_t
 

Public Member Functions

void play (position_t move)
 
unsigned int play (const std::string &seq)
 
bool canWinNext () const
 
int nbMoves () const
 
position_t key () const
 
uint64_t key3 () const
 
position_t possibleNonLosingMoves () const
 
int moveScore (position_t move) const
 
 Position ()
 
bool canPlay (int col) const
 
void playCol (int col)
 
bool isWinningMove (int col) const
 

Static Public Member Functions

static constexpr position_t column_mask (int col)
 

Static Public Attributes

static constexpr int WIDTH = 7
 
static constexpr int HEIGHT = 6
 
static constexpr int MIN_SCORE = -(WIDTH * HEIGHT) / 2 + 3
 
static constexpr int MAX_SCORE = (WIDTH * HEIGHT + 1) / 2 - 3
 

Detailed Description

A class storing a Connect 4 position. Functions are relative to the current player to play. Position containing alignment are not supported by this class.

A binary bitboard representationis used. Each column is encoded on HEIGH+1 bits.

Example of bit order to encode for a 7x6 board . . . . . . . 5 12 19 26 33 40 47 4 11 18 25 32 39 46 3 10 17 24 31 38 45 2 9 16 23 30 37 44 1 8 15 22 29 36 43 0 7 14 21 28 35 42

Position is stored as

  • a bitboard "mask" with 1 on any color stones
  • a bitboard "current_player" with 1 on stones of current player

"current_player" bitboard can be transformed into a compact and non ambiguous key by adding an extra bit on top of the last non empty cell of each column. This allow to identify all the empty cells whithout needing "mask" bitboard

current_player "x" = 1, opponent "o" = 0 board position mask key bottom 0000000 0000000 0000000 0000000 ....... 0000000 0000000 0001000 0000000 ...o... 0000000 0001000 0010000 0000000 ..xx... 0011000 0011000 0011000 0000000 ..ox... 0001000 0011000 0001100 0000000 ..oox.. 0000100 0011100 0000110 0000000 ..oxxo. 0001100 0011110 1101101 1111111

current_player "o" = 1, opponent "x" = 0 board position mask key bottom 0000000 0000000 0001000 0000000 ...x... 0000000 0001000 0000000 0000000 ...o... 0001000 0001000 0011000 0000000 ..xx... 0000000 0011000 0000000 0000000 ..ox... 0010000 0011000 0010100 0000000 ..oox.. 0011000 0011100 0011010 0000000 ..oxxo. 0010010 0011110 1110011 1111111

key is an unique representation of a board key = position + mask + bottom in practice, as bottom is constant, key = position + mask is also a non-ambigous representation of the position. Generate a bitmask containing one for the bottom slot of each colum must be defined outside of the class definition to be available at compile time for bottom_mask

Definition at line 84 of file Position.hpp.

Member Typedef Documentation

◆ position_t

using GameSolver::Connect4::Position::position_t = uint64_t

Definition at line 96 of file Position.hpp.

Constructor & Destructor Documentation

◆ Position()

GameSolver::Connect4::Position::Position ( )
inline

Default constructor, build an empty position.

Definition at line 231 of file Position.hpp.

Member Function Documentation

◆ canPlay()

bool GameSolver::Connect4::Position::canPlay ( int col) const
inline

Indicates whether a column is playable.

Parameters
col0-based index of column to play
Returns
true if the column is playable, false if the column is already full.

Definition at line 239 of file Position.hpp.

◆ canWinNext()

bool GameSolver::Connect4::Position::canWinNext ( ) const
inline

return true if current player can win next move

Definition at line 147 of file Position.hpp.

◆ column_mask()

static constexpr position_t GameSolver::Connect4::Position::column_mask ( int col)
inlinestaticconstexpr

Definition at line 378 of file Position.hpp.

◆ isWinningMove()

bool GameSolver::Connect4::Position::isWinningMove ( int col) const
inline

Indicates whether the current player wins by playing a given column. This function should never be called on a non-playable column.

Parameters
col0-based index of a playable column.
Returns
true if current player makes an alignment by playing the corresponding column col.

Definition at line 259 of file Position.hpp.

◆ key()

position_t GameSolver::Connect4::Position::key ( ) const
inline
Returns
a compact representation of a position on WIDTH*(HEIGHT+1) bits.

Definition at line 157 of file Position.hpp.

◆ key3()

uint64_t GameSolver::Connect4::Position::key3 ( ) const
inline

Build a symetric base 3 key. Two symetric positions will have the same key.

This key is a base 3 representation of the sequence of played moves column per column, from bottom to top. The 3 digits are top_of_colum(0), current_player(1), opponent(2).

example: game "45" where player one played colum 4, then player two played column 5 has a representation in base 3 digits : 0 0 0 1 0 2 0 0 0 or : 3*3^3 + 1*3^5

The symetric key is the mimimum key of the two keys built iterating columns from left to righ or right to left.

as the last digit is always 0, we omit it and a base 3 key uses N = (nbMoves + nbColums - 1) base 3 digits or N*log2(3) bits.

Definition at line 176 of file Position.hpp.

◆ moveScore()

int GameSolver::Connect4::Position::moveScore ( position_t move) const
inline

Score a possible move.

Parameters
move,apossible move given in a bitmap format.

The score we are using is the number of winning spots the current player has after playing the move.

Definition at line 224 of file Position.hpp.

◆ nbMoves()

int GameSolver::Connect4::Position::nbMoves ( ) const
inline
Returns
number of moves played from the beginning of the game.

Definition at line 152 of file Position.hpp.

◆ play() [1/2]

unsigned int GameSolver::Connect4::Position::play ( const std::string & seq)
inline

Definition at line 133 of file Position.hpp.

◆ play() [2/2]

void GameSolver::Connect4::Position::play ( position_t move)
inline

Plays a possible move given by its bitmap representation

Parameters
movea possible move given by its bitmap representation only one bit of the bitmap should be set to 1 the move should be a valid possible move for the current player

Definition at line 113 of file Position.hpp.

◆ playCol()

void GameSolver::Connect4::Position::playCol ( int col)
inline

Plays a playable column. This function should not be called on a non-playable column or a column making an alignment.

Parameters
col0-based index of a playable column.

Definition at line 248 of file Position.hpp.

◆ possibleNonLosingMoves()

position_t GameSolver::Connect4::Position::possibleNonLosingMoves ( ) const
inline

Return a bitmap of all the possible next moves the do not lose in one turn. A losing move is a move leaving the possibility for the opponent to win directly.

Warning this function is intended to test position where you cannot win in one turn If you have a winning move, this function can miss it and prefer to prevent the opponent to make an alignment.

Definition at line 200 of file Position.hpp.

Member Data Documentation

◆ HEIGHT

int GameSolver::Connect4::Position::HEIGHT = 6
staticconstexpr

Definition at line 87 of file Position.hpp.

◆ MAX_SCORE

int GameSolver::Connect4::Position::MAX_SCORE = (WIDTH * HEIGHT + 1) / 2 - 3
staticconstexpr

Definition at line 100 of file Position.hpp.

◆ MIN_SCORE

int GameSolver::Connect4::Position::MIN_SCORE = -(WIDTH * HEIGHT) / 2 + 3
staticconstexpr

Definition at line 99 of file Position.hpp.

◆ WIDTH

int GameSolver::Connect4::Position::WIDTH = 7
staticconstexpr

Definition at line 86 of file Position.hpp.


The documentation for this class was generated from the following file: