ChineseCheckers
This project is a solver for two players' Chinese Checkers game using the Alpha Beta pruning algorithm. The program is written in C++ and uses a number of heuristics to improve the performance of the algorithm. Overall, the Chinese Checkers solver program has been built with performance in mind, and is designed to deliver fast and efficient game play. Whether you are playing against a bot or using the program as a library in your own project, you can be confident that the program will deliver high-performance results.
Loading...
Searching...
No Matches
ChineseCheckers Class Reference

This class represents a Chinese Checkers game The ChineseCheckers class is responsible for representing the game of Chinese Checkers and for providing functionalities such as checking if a move is legal, executing moves, printing the game state, returning the current state of the game, and creating a new game. It also stores information about the grid, the number of times a position has been seen, the winning positions, and the illegal positions. More...

#include <ChineseCheckers.hpp>

Inheritance diagram for ChineseCheckers:
AlphaBeta IntuitionDataGenerator OpeningsGenerator

Public Member Functions

 ChineseCheckers ()
 
void newGame ()
 
bool move (const Player &player, const ListOfPositionType &list_moves)
 
Result stateOfGame ()
 
void moveWithoutVerification (const uint_fast64_t &move)
 
Player getWhoIsToPlay () const
 
uint_fast64_t getBitBoardWhite () const
 
uint_fast64_t getBitBoardBlack () const
 
void printGrid () const
 
void printWhoIsToPlay () const
 

Protected Member Functions

MoveType elementaryMove (const PositionType &original_position, const PositionType &arrival_position) const
 
bool isPositionIllegal () const
 
int cantorPairingFunction (const int &x, const int &y) const
 
boost::unordered_map< uint32_t, bool > loadIllegalPositions () const
 
void generateZobristKeys ()
 
void computeAndSetZobristHash ()
 

Protected Attributes

const uint_fast64_t un_64_ = static_cast<uint_fast64_t>(1)
 
Player who_is_to_play_ = 0
 
bitBoards_t bit_boards_
 
uint_fast64_t zobrist_hash_
 
std::array< boost::unordered_map< uint_fast64_t, uint_fast64_t >, 2 > zobrist_keys_
 
std::array< boost::unordered_map< uint_fast64_t, uint_fast64_t >, 2 > zobrist_keys_moves_
 
boost::unordered_map< uint64_t, int > number_of_times_seen_
 
std::vector< uint64_t > positions_seen_
 
const uint_fast64_t winning_positions_white_ = 0xF0E0C08000000000
 
const uint_fast64_t winning_positions_black_ = 0x000000000103070F
 
const boost::unordered_map< uint32_t, bool > illegal_positions_
 
const std::array< std::array< uint32_t, 8 >, 8 > cantor_pairing_
 
const std::vector< std::vector< int > > valid_lines
 
const std::vector< std::vector< int > > valid_lines_illegal
 
const std::array< std::pair< int, int >, 64 > uint64_to_pair_
 
const std::array< std::array< uint_fast64_t, 8 >, 8 > int_to_uint64_
 
const std::array< std::vector< uint_fast64_t >, 64 > direct_neighbours_
 
const std::array< std::vector< std::vector< std::pair< std::pair< uint_fast64_t, uint_fast64_t >, uint_fast64_t > > >, 64 > k_neighbours_
 

Detailed Description

This class represents a Chinese Checkers game The ChineseCheckers class is responsible for representing the game of Chinese Checkers and for providing functionalities such as checking if a move is legal, executing moves, printing the game state, returning the current state of the game, and creating a new game. It also stores information about the grid, the number of times a position has been seen, the winning positions, and the illegal positions.

Constructor & Destructor Documentation

◆ ChineseCheckers()

ChineseCheckers::ChineseCheckers ( )

Construct the object

See also
newGame

Member Function Documentation

◆ cantorPairingFunction()

int ChineseCheckers::cantorPairingFunction ( const int &  x,
const int &  y 
) const
protected

Cantor's pairing function.

Parameters
xfirst argument.
ysecond argument.
Returns
cantor's pairing function applied to x and y.
See also
isPositionIllegal
loadIllegalPositions

◆ computeAndSetZobristHash()

void ChineseCheckers::computeAndSetZobristHash ( )
protected

The function calculates the Zobrist hash value for the current game state based on the position of each piece on the board, using precomputed random keys for each square and each color. The hash value is stored in the zobrist_hash_ member variable.

See also
generateZobristKeys

◆ elementaryMove()

MoveType ChineseCheckers::elementaryMove ( const PositionType original_position,
const PositionType arrival_position 
) const
protected

A member returning the type of an elementary move (not a succession of jumps).

Parameters
original_positionThe position.
arrival_positionThe arrival position.
Return values
Illegalif the elementary move realized was of illegal.
Jumpif the elementary move realized was a jump.
notJumpif the elementary move realized was not a jump.
See also
move

◆ generateZobristKeys()

void ChineseCheckers::generateZobristKeys ( )
protected

◆ getBitBoardBlack()

uint_fast64_t ChineseCheckers::getBitBoardBlack ( ) const

Returns bit_boards_.Black

Returns
bit_boards_.Black

◆ getBitBoardWhite()

uint_fast64_t ChineseCheckers::getBitBoardWhite ( ) const

Returns bit_boards_.White

Returns
bit_boards_.White

◆ getWhoIsToPlay()

Player ChineseCheckers::getWhoIsToPlay ( ) const

◆ isPositionIllegal()

bool ChineseCheckers::isPositionIllegal ( ) const
protected

Indicates whether the position is illegal or not.

Returns
Returns true iff the position is illegal.
See also
cantorPairingFunction
loadIllegalPositions

◆ loadIllegalPositions()

boost::unordered_map< uint32_t, bool > ChineseCheckers::loadIllegalPositions ( ) const
protected

Loads illegal positions.

See also
isPositionIllegal
cantorPairingFunction

◆ move()

bool ChineseCheckers::move ( const Player player,
const ListOfPositionType list_moves 
)

This member checks if a move is legal and executes it if it is legal.

Parameters
playerindicates which player made the move.
list_movescontains the list of intermediate positions. of a move (including the starting point and the arrival point).
Return values
trueif the move was legal.
falseif the move was not legal.
See also
MoveType elementaryMove

◆ moveWithoutVerification()

void ChineseCheckers::moveWithoutVerification ( const uint_fast64_t &  move)

This function is used to move a piece without verifying if the move is valid or not. It is not made to be used by the end user. It has been engineered to be used by solvers. It bypasses usual verifications and hence allows illegal moves to be played. This function should be used with great care.

Parameters
moveindicates the move do execute. This is a 64 bit type. Only two bits must be set : the original position and the arrival position.
See also
elementaryMove
move

◆ newGame()

void ChineseCheckers::newGame ( )

Creates a new game.

◆ printGrid()

void ChineseCheckers::printGrid ( ) const

Prints the grid.

◆ printWhoIsToPlay()

void ChineseCheckers::printWhoIsToPlay ( ) const

◆ stateOfGame()

Result ChineseCheckers::stateOfGame ( )

This function returns the current state of the game, which can be one of the following four values: WhiteWon, BlackWon, Draw, or NotFinished.

Return values
notFinishedif the game is not finished.
whiteWonif white won the game.
blackWonif black won the game.
drawif a draw happened.
See also
newGame

Member Data Documentation

◆ bit_boards_

bitBoards_t ChineseCheckers::bit_boards_
protected

A data structure containing the grid.

◆ cantor_pairing_

const std::array<std::array<uint32_t, 8>, 8> ChineseCheckers::cantor_pairing_
protected

Stores results of cantor pairing to make the check for illegal positions faster.

◆ direct_neighbours_

const std::array<std::vector<uint_fast64_t>, 64> ChineseCheckers::direct_neighbours_
protected

Stores the neighbours' position of all pawns.

◆ illegal_positions_

const boost::unordered_map<uint32_t, bool> ChineseCheckers::illegal_positions_
protected

Stores the illegal positions.

◆ int_to_uint64_

const std::array<std::array<uint_fast64_t, 8>, 8> ChineseCheckers::int_to_uint64_
protected

Stores the conversion of a pair of index to a bit-wise position.

◆ k_neighbours_

const std::array<std::vector< std::vector<std::pair<std::pair<uint_fast64_t, uint_fast64_t>, uint_fast64_t> > >, 64> ChineseCheckers::k_neighbours_
protected

@detailsStores all required bitMasks to find all jumps from a given pawn.

◆ number_of_times_seen_

boost::unordered_map<uint64_t , int> ChineseCheckers::number_of_times_seen_
protected

Indicates the number of times a position has been seen. It is used to check for draws.

◆ positions_seen_

std::vector<uint64_t> ChineseCheckers::positions_seen_
protected

Indicate the positions we have already seen.

◆ uint64_to_pair_

const std::array<std::pair<int, int>, 64> ChineseCheckers::uint64_to_pair_
protected

Stores the conversion of a bit-wise position to a pair of index.

◆ un_64_

const uint_fast64_t ChineseCheckers::un_64_ = static_cast<uint_fast64_t>(1)
protected

Basic object used to create bitmasks efficiently.

◆ valid_lines

const std::vector<std::vector<int> > ChineseCheckers::valid_lines
protected
Initial value:
= {{-1, 0}, {-1, 1}, {0 , -1},
{0 , 1}, {1 , -1}, {1 , 0}}

Stores the valid directions to find if a move is valid faster.

◆ valid_lines_illegal

const std::vector<std::vector<int> > ChineseCheckers::valid_lines_illegal
protected
Initial value:
= {{-1, 0}, {-1, 1}, {0 , -1},
{0 , 1}, {1 , -1}, {1 , 0},
{-2, 0}, {-2, 2}, {0 , -2},
{0 , 2}, {2 , -2}, {2 , 0}}

Stores the valid directions to find if a position is legal faster.

◆ who_is_to_play_

Player ChineseCheckers::who_is_to_play_ = 0
protected

Indicate which is the next player to play.

◆ winning_positions_black_

const uint_fast64_t ChineseCheckers::winning_positions_black_ = 0x000000000103070F
protected

Keeps the positions of the black winning zone.

◆ winning_positions_white_

const uint_fast64_t ChineseCheckers::winning_positions_white_ = 0xF0E0C08000000000
protected

Keeps the positions of the white winning zone.

◆ zobrist_hash_

uint_fast64_t ChineseCheckers::zobrist_hash_
protected

The hash of the current grid.

◆ zobrist_keys_

std::array<boost::unordered_map<uint_fast64_t, uint_fast64_t>, 2> ChineseCheckers::zobrist_keys_
protected

Zobrist keys associated with each positions.

◆ zobrist_keys_moves_

std::array<boost::unordered_map<uint_fast64_t, uint_fast64_t>, 2> ChineseCheckers::zobrist_keys_moves_
protected

Zobrist keys associated to a move.


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