Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
zobrist_calculator_for_concepts.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <memory>
7
8namespace boardstate {
12template <typename K>
14
15
16private:
17 using PieceZarray_t = array<array<K, gameboard::kNumFiles>, gameboard::kNumRanks>;
18 using TeamZarray_t = array<PieceZarray_t, gameboard::kNumPieceTypeVals>;
19 using GameZarray_t = array<TeamZarray_t, 2>;
20
23 uint32_t seed_;
25
26public:
27 using KeyType = K;
28 static std::shared_ptr<ZobristCalculatorForConcepts<K>> Create(
29 uint32_t seed = std::random_device{}()
30 ) {
31 return std::shared_ptr<ZobristCalculatorForConcepts<K>>(
33 );
34 }
35
36 // Getters
37 KeyType board_state() const { return board_state_; }
38 uint32_t seed() const { return seed_; }
39
40 // Calculation methods
43 }
44
45 void UpdateBoardState(const gameboard::ExecutedMove &executed_move) {
46 UpdateBoardStateInternal(executed_move);
47 }
48
49private:
52 ZobristCalculatorForConcepts(uint32_t seed = std::random_device{}())
53 : zarray_{}
54 , turn_key_{}
55 , board_state_{}
56 , seed_{seed} {
57 PseudoRandomKeyGenerator<K> key_generator{seed};
58 turn_key_ = key_generator.GenerateKey();
59 zarray_ = CreateGameZarray(key_generator);
60 };
61
64 ) {
65 GameZarray_t game_zarray{};
66 for (auto color_idx = 0; color_idx < 2; color_idx++) {
67 for (auto piece_id = 1; piece_id < kNumPieceTypeVals; piece_id++) {
68 for (auto rank = 0; rank < kNumRanks; rank++) {
69 for (auto file = 0; file < kNumFiles; file++) {
70 game_zarray[color_idx][piece_id][rank][file] = key_generator.GenerateKey();
71 }
72 }
73 }
74 }
75 return game_zarray;
76 }
77
78 K GetHashValueAt(PieceColor color, PieceType piece_type, BoardSpace space) {
79 return zarray_[GetZColorIndexOf(color)][piece_type][space.rank][space.file];
80 }
81
82 // Calculation methods
83
85 board_state_ = 0;
86 for (size_t rank = 0; rank < gameboard::kNumRanks; rank++) {
87 for (size_t file = 0; file < gameboard::kNumFiles; file++) {
88 if (board_map[rank][file].piece_color != 0) {
90 board_map[rank][file].piece_color,
91 board_map[rank][file].piece_type,
92 gameboard::BoardSpace{(int)rank, (int)file}
93 );
94 }
95 }
96 }
97 }
98
101 executed_move.moving_piece.piece_color,
102 executed_move.moving_piece.piece_type,
103 executed_move.spaces.start
104 );
105
106 // if capture piece, remove from board
109 executed_move.destination_piece.piece_color,
110 executed_move.destination_piece.piece_type,
111 executed_move.spaces.end
112 );
113 }
114
115 // moving piece to new space
117 executed_move.moving_piece.piece_color,
118 executed_move.moving_piece.piece_type,
119 executed_move.spaces.end
120 );
121
122 // change state now that its other player's turn
124 }
125};
126} // namespace boardstate
Constants, typedefs, and simple structs used by gameboard::GameBoard.
Generates pseudorandom integers.
IntType GenerateKey()
Generates a pseudorandom IntType value using mt19937.
Uses Zobrist hashing to calculate a "reasonably unique" integer value for each board configuration en...
void UpdateBoardStateInternal(const gameboard::ExecutedMove &executed_move)
static std::shared_ptr< ZobristCalculatorForConcepts< K > > Create(uint32_t seed=std::random_device{}())
K GetHashValueAt(PieceColor color, PieceType piece_type, BoardSpace space)
void FullBoardStateCalc(const gameboard::BoardMap_t &board_map)
array< PieceZarray_t, gameboard::kNumPieceTypeVals > TeamZarray_t
void UpdateBoardState(const gameboard::ExecutedMove &executed_move)
static const GameZarray_t CreateGameZarray(PseudoRandomKeyGenerator< K > &key_generator)
Static helper method for building 4-D array of Zobrist keys in constuctor.
ZobristCalculatorForConcepts(uint32_t seed=std::random_device{}())
Constructs a ZobristCalculatorForConcepts.
array< array< K, gameboard::kNumFiles >, gameboard::kNumRanks > PieceZarray_t
void FullBoardStateCalcInternal(const gameboard::BoardMap_t &board_map)
Declaration of boardstate::KeyGenerator and implementation of its template methods.
Definitions and implementations of gameboard::Move and other move-related structs.
Calculate / manage board state and associate Minimax results.
size_t GetZColorIndexOf(PieceColor color)
Definition: game_piece.hpp:77
const BoardIndexType kNumFiles
const BoardIndexType kNumRanks
array< array< GamePiece, kNumFiles >, kNumRanks > BoardMap_t
2-D array of gameboard::GamePiece objects.
const int kNumPieceTypeVals
Definition: game_piece.hpp:23
A pair of coordinate (rank, and file) with properties determined by comparison with values of gameboa...
A change in the state of a gameboard::GameBoard represented by a gameboard::Move, and each of the gam...
gameboard::GamePiece moving_piece
gameboard::GamePiece destination_piece
PieceColor piece_color
Definition: game_piece.hpp:44
gameboard::BoardSpace end
gameboard::BoardSpace start