![]() |
BitBully 0.0.39
|
#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 |
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
"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.
using GameSolver::Connect4::Position::position_t = uint64_t |
Definition at line 96 of file Position.hpp.
|
inline |
Default constructor, build an empty position.
Definition at line 231 of file Position.hpp.
|
inline |
Indicates whether a column is playable.
col | 0-based index of column to play |
Definition at line 239 of file Position.hpp.
|
inline |
return true if current player can win next move
Definition at line 147 of file Position.hpp.
|
inlinestaticconstexpr |
Definition at line 378 of file Position.hpp.
|
inline |
Indicates whether the current player wins by playing a given column. This function should never be called on a non-playable column.
col | 0-based index of a playable column. |
Definition at line 259 of file Position.hpp.
|
inline |
Definition at line 157 of file Position.hpp.
|
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.
|
inline |
Score a possible move.
move,a | possible 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.
|
inline |
Definition at line 152 of file Position.hpp.
|
inline |
Definition at line 133 of file Position.hpp.
|
inline |
Plays a possible move given by its bitmap representation
move | a 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.
|
inline |
Plays a playable column. This function should not be called on a non-playable column or a column making an alignment.
col | 0-based index of a playable column. |
Definition at line 248 of file Position.hpp.
|
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.
|
staticconstexpr |
Definition at line 87 of file Position.hpp.
|
staticconstexpr |
Definition at line 100 of file Position.hpp.
|
staticconstexpr |
Definition at line 99 of file Position.hpp.
|
staticconstexpr |
Definition at line 86 of file Position.hpp.