1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
//-----------------------------------------------------------------------------
/** @file libpentobi_base/BoardConst.h
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#ifndef LIBPENTOBI_BASE_BOARD_CONST_H
#define LIBPENTOBI_BASE_BOARD_CONST_H
#include "MoveInfo.h"
#include "PrecompMoves.h"
#include "SymmetricPoints.h"
#include "libboardgame_base/Range.h"
namespace libpentobi_base {
using libboardgame_base::Range;
//-----------------------------------------------------------------------------
/** Constant precomputed data that is shared between all instances of Board
with a given board type and set of unique pieces per color. */
class BoardConst
{
public:
/** See get_adj_status_points() */
using AdjStatusPoints = array<Point, PrecompMoves::adj_status_nu_adj>;
/** Start of the MoveInfo array, which can be cached by the user in
performance-critical code and then passed into the static version of
get_move_info(). */
using MoveInfoArray = const void*;
/** Start of the MoveInfoExt array, which can be cached by the user in
performance-critical code and then passed into the static version of
get_move_info_ext(). */
using MoveInfoExtArray = const void*;
/** Get the single instance for a given board size.
The instance is created the first time this function is called.
This function is not thread-safe. */
static const BoardConst& get(Variant variant);
template<unsigned MAX_SIZE>
static const MoveInfo<MAX_SIZE>&
get_move_info(Move mv, MoveInfoArray move_info_array);
template<unsigned MAX_ADJ_ATTACH>
static const MoveInfoExt<MAX_ADJ_ATTACH>&
get_move_info_ext(Move mv, MoveInfoExtArray move_info_ext_array);
Piece::IntType get_nu_pieces() const;
const PieceInfo& get_piece_info(Piece piece) const;
unsigned get_nu_attach_points(Piece piece) const;
bool get_piece_by_name(const string& name, Piece& piece) const;
const PieceTransforms& get_transforms() const;
unsigned get_max_piece_size() const { return m_max_piece_size; }
unsigned get_max_adj_attach() const { return m_max_adj_attach; }
Range<const Point> get_move_points(Move mv) const;
/** Return start of move points array.
For unrolling loops, there are guaranteed to be as many elements
as the maximum piece size in the current game variant. If the piece
is smaller, the remaining points are guaranteed to be Point::null(). */
const Point* get_move_points_begin(Move mv) const;
template<unsigned MAX_SIZE>
const Point* get_move_points_begin(Move mv) const;
Piece get_move_piece(Move mv) const;
template<unsigned MAX_SIZE>
Piece get_move_piece(Move mv) const;
MoveInfoArray get_move_info_array() const { return m_move_info.get(); }
/** Get pointer to extended move info array.
Can be used to speed up the access to the move info by avoiding the
multiple pointer dereferencing of Board::get_move_info_ext(Move) */
MoveInfoExtArray get_move_info_ext_array() const;
const MoveInfoExt2& get_move_info_ext_2(Move mv) const;
const MoveInfoExt2* get_move_info_ext_2_array() const;
Move::IntType get_range() const { return m_range; }
bool find_move(const MovePoints& points, Move& move) const;
bool find_move(const MovePoints& points, Piece piece, Move& move) const;
/** Get all moves of a piece at a point constrained by the forbidden
status of adjacent points. */
PrecompMoves::Range get_moves(Piece piece, Point p,
unsigned adj_status = 0) const
{
return m_precomp_moves.get_moves(piece, p, adj_status);
}
const PrecompMoves& get_precomp_moves() const { return m_precomp_moves; }
BoardType get_board_type() const { return m_board_type; }
PieceSet get_piece_set() const { return m_piece_set; }
const Geometry& get_geometry() const;
/** Array containing the points used for the adjacent status.
Contains a selection of first-order or second-order adjacent and
diagonal neighbor points.
@pre has_adj_status_points(p) */
const AdjStatusPoints& get_adj_status_points(Point p) const
{
return m_adj_status_points[p];
}
/** Adjacent status arrays are not initialized for junction points in
Nexos. */
bool has_adj_status_points(Point p) const
{
return m_board_type != BoardType::nexos || m_geo.get_point_type(p) != 0;
}
/** Only initialized in game variants with central symmetry of board
including starting points. */
const SymmetricPoints& get_symmetrc_points() const
{
return m_symmetric_points;
}
/** Convert a move to its string representation.
The string representation is a comma-separated list of points (without
spaces between the commas or points). If with_piece_name is true,
it is prepended by the piece name in square brackets (also without any
spaces). The representation without the piece name is used by the SGF
files and GTP interface used by Pentobi (version >= 0.2). */
string to_string(Move mv, bool with_piece_name = false) const;
bool from_string(Move& mv, const string& s) const;
/** Sort move points using the ordering used in blksgf files. */
void sort(MovePoints& points) const;
private:
struct MallocFree
{
void operator()(void* x) { free(x); }
};
Piece::IntType m_nu_pieces;
Move::IntType m_range;
unsigned m_max_piece_size;
/** See MoveInfoExt */
unsigned m_max_adj_attach;
BoardType m_board_type;
PieceSet m_piece_set;
const Geometry& m_geo;
vector<PieceInfo> m_pieces;
Grid<AdjStatusPoints> m_adj_status_points;
unique_ptr<PieceTransforms> m_transforms;
PieceMap<unsigned> m_nu_attach_points{0};
/** Array of MoveInfo<MAX_SIZE> with MAX_SIZE being the maximum piece size
in the corresponding game variant.
See comments at MoveInfo. */
unique_ptr<void, MallocFree> m_move_info;
/** Array of MoveInfoExt<MAX_ADJ_ATTACH> with MAX_ADJ_ATTACH being the
maximum total number of attach points and adjacent points of a piece in
the corresponding game variant.
See comments at MoveInfoExt. */
unique_ptr<void, MallocFree> m_move_info_ext;
unique_ptr<MoveInfoExt2[]> m_move_info_ext_2;
PrecompMoves m_precomp_moves;
/** Value for comparing points using the ordering used in blksgf files.
As specified in doc/blksgf/Pentobi-SGF.html, the order should be
(a1, b1, ..., a2, b2, ...) with y going upwards whereas the convention
for Point is that y goes downwards. */
Grid<unsigned> m_compare_val;
SymmetricPoints m_symmetric_points;
BoardConst(BoardType board_type, PieceSet piece_set);
template<unsigned MAX_SIZE, unsigned MAX_ADJ_ATTACH>
void create_move(unsigned& moves_created, Piece piece,
const MovePoints& points, Point label_pos);
void create_moves();
template<unsigned MAX_SIZE, unsigned MAX_ADJ_ATTACH>
void create_moves(unsigned& moves_created, Piece piece);
template<unsigned MAX_SIZE>
const MoveInfo<MAX_SIZE>& get_move_info(Move mv) const;
void init_adj_status_points(Point p);
template<unsigned MAX_SIZE>
void init_symmetry_info();
};
inline const Geometry& BoardConst::get_geometry() const
{
return m_geo;
}
template<unsigned MAX_SIZE>
inline const MoveInfo<MAX_SIZE>&
BoardConst::get_move_info(Move mv, MoveInfoArray move_info_array)
{
LIBBOARDGAME_ASSERT(! mv.is_null());
return *(static_cast<const MoveInfo<MAX_SIZE>*>(move_info_array)
+ mv.to_int());
}
template<unsigned MAX_SIZE>
inline const MoveInfo<MAX_SIZE>& BoardConst::get_move_info(Move mv) const
{
LIBBOARDGAME_ASSERT(m_max_piece_size == MAX_SIZE);
return get_move_info<MAX_SIZE>(mv, m_move_info.get());
}
template<unsigned MAX_ADJ_ATTACH>
inline const MoveInfoExt<MAX_ADJ_ATTACH>&
BoardConst::get_move_info_ext(Move mv, MoveInfoExtArray move_info_ext_array)
{
LIBBOARDGAME_ASSERT(! mv.is_null());
return *(static_cast<const MoveInfoExt<MAX_ADJ_ATTACH>*>(
move_info_ext_array) + mv.to_int());
}
inline const MoveInfoExt2& BoardConst::get_move_info_ext_2(Move mv) const
{
LIBBOARDGAME_ASSERT(mv.to_int() < m_range);
return m_move_info_ext_2[mv.to_int()];
}
inline auto BoardConst::get_move_info_ext_array() const -> MoveInfoExtArray
{
return m_move_info_ext.get();
}
inline const MoveInfoExt2* BoardConst::get_move_info_ext_2_array() const
{
return m_move_info_ext_2.get();
}
template<unsigned MAX_SIZE>
inline Piece BoardConst::get_move_piece(Move mv) const
{
return get_move_info<MAX_SIZE>(mv).get_piece();
}
inline Range<const Point> BoardConst::get_move_points(Move mv) const
{
if (m_max_piece_size == 5)
{
auto& info = get_move_info<5>(mv);
return {info.begin(), info.end()};
}
if (m_max_piece_size == 6)
{
auto& info = get_move_info<6>(mv);
return {info.begin(), info.end()};
}
if (m_max_piece_size == 7)
{
auto& info = get_move_info<7>(mv);
return {info.begin(), info.end()};
}
LIBBOARDGAME_ASSERT(m_max_piece_size == 22);
auto& info = get_move_info<22>(mv);
return {info.begin(), info.end()};
}
inline const Point* BoardConst::get_move_points_begin(Move mv) const
{
if (m_max_piece_size == 5)
return get_move_points_begin<5>(mv);
if (m_max_piece_size == 6)
return get_move_points_begin<6>(mv);
if (m_max_piece_size == 7)
return get_move_points_begin<7>(mv);
LIBBOARDGAME_ASSERT(m_max_piece_size == 22);
return get_move_points_begin<22>(mv);
}
template<unsigned MAX_SIZE>
inline const Point* BoardConst::get_move_points_begin(Move mv) const
{
return get_move_info<MAX_SIZE>(mv).begin();
}
inline unsigned BoardConst::get_nu_attach_points(Piece piece) const
{
return m_nu_attach_points[piece];
}
inline Piece::IntType BoardConst::get_nu_pieces() const
{
return m_nu_pieces;
}
inline const PieceInfo& BoardConst::get_piece_info(Piece piece) const
{
LIBBOARDGAME_ASSERT(piece.to_int() < m_pieces.size());
return m_pieces[piece.to_int()];
}
inline const PieceTransforms& BoardConst::get_transforms() const
{
return *m_transforms;
}
//-----------------------------------------------------------------------------
} // namespace libpentobi_base
#endif // LIBPENTOBI_BASE_BOARD_CONST_H
|