My Project
Macros | Functions | Variables
board.c File Reference
#include "board.h"
#include "bit.h"
#include "hash.h"
#include "move.h"
#include "settings.h"
#include "util.h"
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include "flip_carry_32.c"
#include "count_last_flip_carry_32.c"

Macros

#define packA1A8(X)   (((((unsigned int)(X) & 0x01010101u) + (((unsigned int)((X) >> 32) & 0x01010101u) << 4)) * 0x01020408u) >> 24)
 
#define packH1H8(X)   (((((unsigned int)((X) >> 32) & 0x80808080u) + (((unsigned int)(X) & 0x80808080u) >> 4)) * 0x00204081u) >> 24)
 

Functions

void board_swap_players (Board *board)
 Swap players. More...
 
int board_set (Board *board, const char *string)
 Set a board from a string description. More...
 
int board_from_FEN (Board *board, const char *string)
 Set a board from a string description. More...
 
int board_from_obj (Board *board, const Board *obj, const int turn)
 Set a board from a Board object. More...
 
void board_init (Board *board)
 Set a board to the starting position. More...
 
void board_check (const Board *board)
 Check board consistency. More...
 
int board_compare (const Board *b1, const Board *b2)
 Compare two board. More...
 
bool board_equal (const Board *b1, const Board *b2)
 Compare two board for equality. More...
 
void board_symetry (const Board *board, const int s, Board *sym)
 symetric board More...
 
int board_unique (const Board *board, Board *unique)
 unique board More...
 
void board_rand (Board *board, int n_ply, Random *r)
 Get a random board by playing random moves. More...
 
unsigned long long board_get_move (const Board *board, const int x, Move *move)
 Compute a move. More...
 
bool board_check_move (const Board *board, Move *move)
 Check if a move is legal. More...
 
void board_update (Board *board, const Move *move)
 Update a board. More...
 
void board_restore (Board *board, const Move *move)
 Restore a board. More...
 
void board_pass (Board *board)
 Passing move. More...
 
unsigned long long board_next (const Board *board, const int x, Board *next)
 Compute a board resulting of a move played on a previous board. More...
 
unsigned long long board_pass_next (const Board *board, const int x, Board *next)
 Compute a board resulting of an opponent move played on a previous board. More...
 
static unsigned long long get_some_moves (const unsigned long long P, const unsigned long long mask, const int dir)
 Get a part of the moves. More...
 
DLL_API unsigned long long get_moves (const unsigned long long P, const unsigned long long O)
 Get legal moves. More...
 
unsigned long long get_moves_6x6 (const unsigned long long P, const unsigned long long O)
 Get legal moves on a 6x6 board. More...
 
DLL_API bool can_move (const unsigned long long P, const unsigned long long O)
 Check if a player can move. More...
 
bool can_move_6x6 (const unsigned long long P, const unsigned long long O)
 Check if a player can move. More...
 
int get_mobility (const unsigned long long P, const unsigned long long O)
 Count legal moves. More...
 
int get_weighted_mobility (const unsigned long long P, const unsigned long long O)
 
static unsigned long long get_some_potential_moves (const unsigned long long P, const int dir)
 Get some potential moves. More...
 
static unsigned long long get_potential_moves (const unsigned long long P, const unsigned long long O)
 Get potential moves. More...
 
int get_potential_mobility (const unsigned long long P, const unsigned long long O)
 Get potential mobility. More...
 
static int find_edge_stable (const int old_P, const int old_O, int stable)
 search stable edge patterns. More...
 
void edge_stability_init (void)
 Initialize the edge stability tables. More...
 
static unsigned long long get_full_lines (const unsigned long long line, const int dir)
 Get full lines. More...
 
static unsigned long long get_stable_edge (const unsigned long long P, const unsigned long long O)
 Get stable edge. More...
 
int get_stability (const unsigned long long P, const unsigned long long O)
 Estimate the stability. More...
 
int get_edge_stability (const unsigned long long P, const unsigned long long O)
 Estimate the stability of edges. More...
 
int get_corner_stability (const unsigned long long P)
 Estimate corner stability. More...
 
unsigned long long board_get_hash_code (const Board *board)
 Compute a hash code. More...
 
int board_get_square_color (const Board *board, const int x)
 Get square color. More...
 
bool board_is_occupied (const Board *board, const int x)
 Check if a square is occupied. More...
 
bool board_is_pass (const Board *board)
 Check if current player should pass. More...
 
bool board_is_game_over (const Board *board)
 Check if the game is over. More...
 
int board_count_empties (const Board *board)
 Check if the game is over. More...
 
void board_print (const Board *board, const int player, FILE *f)
 Print out the board. More...
 
char * board_to_string (const Board *board, const int player, char *s)
 convert the to a compact string. More...
 
void board_print_FEN (const Board *board, const int player, FILE *f)
 print using FEN description. More...
 
char * board_to_FEN (const Board *board, const int player, char *string)
 print to FEN description. More...
 

Variables

static unsigned char edge_stability [256][256]
 
static const unsigned long long A1_A8 [256]
 
static const unsigned long long H1_H8 [256]
 

Detailed Description

This module deals with the Board management.

The Board is represented with a structure containing the following data:

High level functions are provided to set/modify the board data or to compute some board properties. Most of the functions are optimized to be as fast as possible, while remaining readable.

Date
1998 - 2017
Author
Richard Delorme
Version
4.4

Macro Definition Documentation

◆ packA1A8

#define packA1A8 (   X)    (((((unsigned int)(X) & 0x01010101u) + (((unsigned int)((X) >> 32) & 0x01010101u) << 4)) * 0x01020408u) >> 24)

◆ packH1H8

#define packH1H8 (   X)    (((((unsigned int)((X) >> 32) & 0x80808080u) + (((unsigned int)(X) & 0x80808080u) >> 4)) * 0x00204081u) >> 24)

Function Documentation

◆ board_check()

void board_check ( const Board board)

Check board consistency.

Parameters
boardthe board to initialize

◆ board_check_move()

bool board_check_move ( const Board board,
Move move 
)

Check if a move is legal.

Parameters
boardboard
movea Move.
Returns
true if the move is legal, false otherwise.

◆ board_compare()

int board_compare ( const Board b1,
const Board b2 
)

Compare two board.

Parameters
b1first board
b2second board
Returns
-1, 0, 1

◆ board_count_empties()

int board_count_empties ( const Board board)

Check if the game is over.

Parameters
boardboard.
Returns
true if game is over, false otherwise.

◆ board_equal()

bool board_equal ( const Board b1,
const Board b2 
)

Compare two board for equality.

Parameters
b1first board
b2second board
Returns
true if both board are equal

◆ board_from_FEN()

int board_from_FEN ( Board board,
const char *  string 
)

Set a board from a string description.

Read a Forsyth-Edwards Notation string and translate it into our internal Board structure.

Parameters
boardthe board to set
stringstring describing the board
Returns
turn's color.

◆ board_from_obj()

int board_from_obj ( Board board,
const Board obj,
const int  turn 
)

Set a board from a Board object.

Parameters
boardthe board to set
objobject describing the board. assumes player attribute as black, opponent attribute as white
turnturn's color
Returns
turn's color.
Date
2018
Author
lavox

◆ board_get_hash_code()

unsigned long long board_get_hash_code ( const Board board)

Compute a hash code.

Parameters
boardthe board.
Returns
the hash code of the bitboard

◆ board_get_move()

unsigned long long board_get_move ( const Board board,
const int  x,
Move move 
)

Compute a move.

Compute how the board will be modified by a move without playing it.

Parameters
boardboard
xsquare on which to move.
movea Move structure remembering the modification.
Returns
the flipped discs.

◆ board_get_square_color()

int board_get_square_color ( const Board board,
const int  x 
)

Get square color.

returned value: 0 = player, 1 = opponent, 2 = empty;

Parameters
boardboard.
xsquare coordinate.
Returns
square color.

◆ board_init()

void board_init ( Board board)

Set a board to the starting position.

Parameters
boardthe board to initialize

◆ board_is_game_over()

bool board_is_game_over ( const Board board)

Check if the game is over.

Parameters
boardboard.
Returns
true if game is over, false otherwise.

◆ board_is_occupied()

bool board_is_occupied ( const Board board,
const int  x 
)

Check if a square is occupied.

Parameters
boardboard.
xsquare coordinate.
Returns
true if a square is occupied.

◆ board_is_pass()

bool board_is_pass ( const Board board)

Check if current player should pass.

Parameters
boardboard.
Returns
true if player is passing, false otherwise.

◆ board_next()

unsigned long long board_next ( const Board board,
const int  x,
Board next 
)

Compute a board resulting of a move played on a previous board.

Parameters
boardboard to play the move on.
xmove to play.
nextresulting board.
Returns
flipped discs.

◆ board_pass()

void board_pass ( Board board)

Passing move.

Modify a board by passing player's turn.

Parameters
boardboard to update.

◆ board_pass_next()

unsigned long long board_pass_next ( const Board board,
const int  x,
Board next 
)

Compute a board resulting of an opponent move played on a previous board.

Compute the board after passing and playing a move.

Parameters
boardboard to play the move on.
xopponent move to play.
nextresulting board.
Returns
flipped discs.

◆ board_print()

void board_print ( const Board board,
const int  player,
FILE *  f 
)

Print out the board.

Print an ASCII representation of the board to an output stream.

Parameters
boardboard to print.
playerplayer's color.
foutput stream.

◆ board_print_FEN()

void board_print_FEN ( const Board board,
const int  player,
FILE *  f 
)

print using FEN description.

Write the board according to the Forsyth-Edwards Notation.

Parameters
boardthe board to write
playerturn's color.
foutput stream.

◆ board_rand()

void board_rand ( Board board,
int  n_ply,
Random r 
)

Get a random board by playing random moves.

Parameters
boardThe output board.
n_plyThe number of random move to generate.
rThe random generator.

◆ board_restore()

void board_restore ( Board board,
const Move move 
)

Restore a board.

Restore a board by un-flipping its discs and restoring every other data, according to the 'move' description, in order to cancel a board_update_move.

Parameters
boardboard to restore.
movea Move structure describing the modification.

◆ board_set()

int board_set ( Board board,
const char *  string 
)

Set a board from a string description.

Read a standardized string (See http://www.nada.kth.se/~gunnar/download2.html for details) and translate it into our internal Board structure.

Parameters
boardthe board to set
stringstring describing the board
Returns
turn's color.

◆ board_swap_players()

void board_swap_players ( Board board)

Swap players.

Swap players, i.e. change player's turn.

Parameters
boardboard

◆ board_symetry()

void board_symetry ( const Board board,
const int  s,
Board sym 
)

symetric board

Parameters
boardinput board
ssymetry
symsymetric output board

◆ board_to_FEN()

char * board_to_FEN ( const Board board,
const int  player,
char *  string 
)

print to FEN description.

Write the board into a Forsyth-Edwards Notation string.

Parameters
boardthe board to write
playerturn's color.
stringoutput string.

◆ board_to_string()

char * board_to_string ( const Board board,
const int  player,
char *  s 
)

convert the to a compact string.

Parameters
boardboard to convert.
playerplayer's color.
soutput string.

◆ board_unique()

int board_unique ( const Board board,
Board unique 
)

unique board

Compute a board unique from all its possible symertries.

Parameters
boardinput board
uniqueoutput board

◆ board_update()

void board_update ( Board board,
const Move move 
)

Update a board.

Update a board by flipping its discs and updating every other data, according to the 'move' description.

Parameters
boardthe board to modify
moveA Move structure describing the modification.

◆ can_move()

DLL_API bool can_move ( const unsigned long long  P,
const unsigned long long  O 
)

Check if a player can move.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
true or false.

◆ can_move_6x6()

bool can_move_6x6 ( const unsigned long long  P,
const unsigned long long  O 
)

Check if a player can move.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
true or false.

◆ edge_stability_init()

void edge_stability_init ( void  )

Initialize the edge stability tables.

◆ find_edge_stable()

static int find_edge_stable ( const int  old_P,
const int  old_O,
int  stable 
)
static

search stable edge patterns.

Compute a 8-bit bitboard where each stable square is set to one

Parameters
old_Pprevious player edge discs.
old_Oprevious opponent edge discs.
stable8-bit bitboard with stable edge squares.

◆ get_corner_stability()

int get_corner_stability ( const unsigned long long  P)

Estimate corner stability.

Count the number of stable discs around the corner. Limiting the count to the corner keep the function fast but still get this information, particularly important at Othello. Corner stability will be used for move sorting.

Parameters
Pbitboard with player's discs.
Returns
the number of stable discs around the corner.

◆ get_edge_stability()

int get_edge_stability ( const unsigned long long  P,
const unsigned long long  O 
)

Estimate the stability of edges.

Count the number (in fact a lower estimate) of stable discs on the edges.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
the number of stable discs on the edges.

◆ get_full_lines()

static unsigned long long get_full_lines ( const unsigned long long  line,
const int  dir 
)
inlinestatic

Get full lines.

Parameters
lineall discs on a line.
dirtested direction
Returns
a bitboard with full lines along the tested direction.

◆ get_mobility()

int get_mobility ( const unsigned long long  P,
const unsigned long long  O 
)

Count legal moves.

Compute mobility, ie the number of legal moves.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
a count of all legal moves.

◆ get_moves()

DLL_API unsigned long long get_moves ( const unsigned long long  P,
const unsigned long long  O 
)

Get legal moves.

Compute a bitboard where each coordinate with a legal move is set to one.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
all legal moves in a 64-bit unsigned integer.

◆ get_moves_6x6()

unsigned long long get_moves_6x6 ( const unsigned long long  P,
const unsigned long long  O 
)

Get legal moves on a 6x6 board.

Compute a bitboard where each coordinate with a legal move is set to one.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
all legal moves in a 64-bit unsigned integer.

◆ get_potential_mobility()

int get_potential_mobility ( const unsigned long long  P,
const unsigned long long  O 
)

Get potential mobility.

Count the list of empty squares in contact of a player square.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
a count of potential moves.

◆ get_potential_moves()

static unsigned long long get_potential_moves ( const unsigned long long  P,
const unsigned long long  O 
)
static

Get potential moves.

Get the list of empty squares in contact of a player square.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
all potential moves in a 64-bit unsigned integer.

◆ get_some_moves()

static unsigned long long get_some_moves ( const unsigned long long  P,
const unsigned long long  mask,
const int  dir 
)
inlinestatic

Get a part of the moves.

Partially compute a bitboard where each coordinate with a legal move is set to one.

Two variants of the algorithm are provided, one based on Kogge-Stone parallel prefix.

Parameters
Pbitboard with player's discs.
maskbitboard with flippable opponent's discs.
dirflipping direction.
Returns
some legal moves in a 64-bit unsigned integer.

◆ get_some_potential_moves()

static unsigned long long get_some_potential_moves ( const unsigned long long  P,
const int  dir 
)
inlinestatic

Get some potential moves.

Parameters
Pbitboard with player's discs.
dirflipping direction.
Returns
some potential moves in a 64-bit unsigned integer.

◆ get_stability()

int get_stability ( const unsigned long long  P,
const unsigned long long  O 
)

Estimate the stability.

Count the number (in fact a lower estimate) of stable discs.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
the number of stable discs.

◆ get_stable_edge()

static unsigned long long get_stable_edge ( const unsigned long long  P,
const unsigned long long  O 
)
inlinestatic

Get stable edge.

Parameters
Pbitboard with player's discs.
Obitboard with opponent's discs.
Returns
a bitboard with (some of) player's stable discs.

◆ get_weighted_mobility()

int get_weighted_mobility ( const unsigned long long  P,
const unsigned long long  O 
)

Variable Documentation

◆ A1_A8

const unsigned long long A1_A8[256]
static

conversion from an 8-bit line to the A1-A8 line

◆ edge_stability

unsigned char edge_stability[256][256]
static

edge stability global data

◆ H1_H8

const unsigned long long H1_H8[256]
static

conversion from an 8-bit line to the H1-H8 line