Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
gameboard.cpp
Go to the documentation of this file.
1
3
6#include <iostream>
7#include <typeinfo>
9
10using namespace gameboard;
11using namespace std;
12
13namespace gameboard {
14
19 {5, 4, 3, 2, 1, 2, 3, 4, 5},
20 {0, 0, 0, 0, 0, 0, 0, 0, 0},
21 {0, 6, 0, 0, 0, 0, 0, 6, 0},
22 {7, 0, 7, 0, 7, 0, 7, 0, 7},
23 {0, 0, 0, 0, 0, 0, 0, 0, 0},
24 {0, 0, 0, 0, 0, 0, 0, 0, 0},
25 {-7, 0, -7, 0, -7, 0, -7, 0, -7},
26 {0, -6, 0, 0, 0, 0, 0, -6, 0},
27 {0, 0, 0, 0, 0, 0, 0, 0, 0},
28 {-5, -4, -3, -2, -1, -2, -3, -4, -5},
29}};
30
32const int kRepeatPeriodsToCheck[3] = {2, 3, 4};
33
38const int kRepeatPeriodsMaxAllowed = 2;
39
40const int kMaxMovesWithoutCapture = 120;
41
45 : board_map_{int_board_to_game_pieces(starting_board)}
46 , move_calculator_{MoveCalculator()}
47 , moves_since_last_capture_{} {}
48
51
54}
55
57 return get_color(board_map_, space);
58}
59
61 return get_type(board_map_, space);
62}
63
65 auto un_tested_moves = move_calculator_.CalcAllMovesNoCheckTest(color, board_map_);
66 MoveCollection validated_moves{};
67
68 if (IsDraw()) {
69 return validated_moves;
70 }
71
72 validated_moves.moves.reserve(un_tested_moves.moves.size());
73
74 for (auto move : un_tested_moves.moves) {
75 auto executed_move = ImplementExecuteMove(move);
76 auto resulting_opponent_moves =
78 auto resulting_gen_position = get_general_position(board_map_, color);
79
80 if (not resulting_opponent_moves.ContainsDestination(resulting_gen_position) and
81 not ViolatesRepeatRule(color)) {
82 validated_moves.Append(move);
83 }
84
85 ImplementUndoMove(executed_move);
86 }
87 return validated_moves;
88}
89
91 auto gen_position = get_general_position(board_map_, color);
92 auto opponent_moves =
94 return opponent_moves.ContainsDestination(gen_position);
95}
96
98 auto moving_piece = GetOccupantAt(move.start);
99 auto destination_piece = GetOccupantAt(move.end);
100 SetOccupantAt(move.end, moving_piece);
102
103 auto executed_move =
104 ExecutedMove{move, moving_piece, destination_piece, moves_since_last_capture_};
105 UpdateStateTracker(executed_move);
106 AddToMoveLog(executed_move);
107 if (!IsCaptureMove(executed_move)) {
109 } else {
111 }
112
113 return executed_move;
114};
115
116bool GameBoard::IsCaptureMove(const ExecutedMove &executed_move) const {
117 return executed_move.destination_piece != PieceColor::kNul;
118}
119
120void GameBoard::ImplementUndoMove(const ExecutedMove &executed_move) {
121 SetOccupantAt(executed_move.spaces.start, executed_move.moving_piece);
122 SetOccupantAt(executed_move.spaces.end, executed_move.destination_piece);
123 UpdateStateTracker(executed_move);
125 RemoveFromMoveLog(executed_move);
126};
127
129 return board_map_[space.rank][space.file];
130};
131
132const BoardMap_t &GameBoard::map() const { return board_map_; }
133
134void GameBoard::ImplementAttachMoveCallback(const function<void(const ExecutedMove&)> &callback
135) {
136 move_callbacks_.emplace_back(callback);
137}
138
141}
142
143const std::map<PieceColor, vector<ExecutedMove>> &GameBoard::move_log() const {
144 return move_log_;
145}
146
147void GameBoard::UpdateStateTracker(const ExecutedMove &executed_move) {
148 for (const auto &callback : move_callbacks_) {
149 callback(executed_move);
150 }
151};
152
154 board_map_[space.rank][space.file] = piece;
155}
156
157void GameBoard::AddToMoveLog(const ExecutedMove &executed_move) {
158 auto piece_color = executed_move.moving_piece.piece_color;
159 move_log_[piece_color].push_back(executed_move);
160};
161
162void GameBoard::RemoveFromMoveLog(const ExecutedMove &executed_move) {
163 auto piece_color = executed_move.moving_piece.piece_color;
164 auto last_move_by_color = move_log_[piece_color].back();
165 if (!(executed_move == last_move_by_color)) {
166 throw runtime_error("Last move in log does not match move to be removed");
167 }
168 move_log_[piece_color].pop_back();
169}
170
172 for (auto period_length : kRepeatPeriodsToCheck) {
173 auto lookback_length = (kRepeatPeriodsMaxAllowed + 1) * period_length;
175 move_log_[color],
176 lookback_length,
177 period_length
178 )) {
179 return true;
180 }
181 }
182 return false;
183}
184
185} // namespace gameboard
Constants, typedefs, and simple structs used by gameboard::GameBoard.
Implements SpaceInfoProvider interface; stores piece positions, and exposes methods for calculating,...
Definition: game_board.hpp:25
bool ViolatesRepeatRule(PieceColor color)
Definition: gameboard.cpp:171
vector< BoardSpace > ImplementGetAllSpacesOccupiedBy(PieceColor color) const
Definition: gameboard.cpp:52
MoveCalculator move_calculator_
Encapsulates all calculations of allowed moves.
Definition: game_board.hpp:48
void AddToMoveLog(const ExecutedMove &executed_move)
Definition: gameboard.cpp:157
void ImplementAttachMoveCallback(const function< void(const ExecutedMove &)> &callback)
Definition: gameboard.cpp:134
MoveCollection ImplementCalcFinalMovesOf(PieceColor color)
Definition: gameboard.cpp:64
void UpdateStateTracker(const ExecutedMove &executed_move)
Definition: gameboard.cpp:147
std::map< PieceColor, vector< ExecutedMove > > move_log_
Vectors of all moves that have been executed (and not un-done) by each player.
Definition: game_board.hpp:55
const std::map< PieceColor, vector< ExecutedMove > > & move_log() const
Definition: gameboard.cpp:143
vector< function< void(const ExecutedMove &)> > move_callbacks_
Stores functions that are called after any change in board config to keep boardstate::SingleZobristCo...
Definition: game_board.hpp:52
bool IsInCheck(PieceColor color)
Definition: gameboard.cpp:90
PieceType ImplementGetType(const BoardSpace &space) const
Definition: gameboard.cpp:60
void RemoveFromMoveLog(const ExecutedMove &executed_move)
Definition: gameboard.cpp:162
ExecutedMove ImplementExecuteMove(const Move &move)
Definition: gameboard.cpp:97
MoveCountType moves_since_last_capture_
Number of moves executed since last time a piece was captured.
Definition: game_board.hpp:58
bool IsCaptureMove(const ExecutedMove &executed_move) const
Definition: gameboard.cpp:116
GamePiece GetOccupantAt(const BoardSpace &space) const
Definition: gameboard.cpp:128
BoardMap_t board_map_
2-D array of GamePiece objects.
Definition: game_board.hpp:45
PieceColor ImplementGetColor(const BoardSpace &space) const
Definition: gameboard.cpp:56
void SetOccupantAt(const BoardSpace &space, GamePiece piece)
Definition: gameboard.cpp:153
const BoardMap_t & map() const
Definition: gameboard.cpp:132
void ImplementUndoMove(const ExecutedMove &executed_move)
Definition: gameboard.cpp:120
Calculates legal gameboard::Move objects for of a gameboard::GameBoard with a particular state.
MoveCollection CalcAllMovesNoCheckTest(PieceColor color, const BoardMap_t &board_map)
Definition of gameboard::GameBoard class.
Tracking piece positions and determining legal moves.
const BoardMapInt_t kStandardInitialBoard
Starting board represented as 2-D array of integers.
BoardSpace get_general_position(const BoardMap_t &board_map, const PieceColor color)
array< array< int, kNumFiles >, kNumRanks > BoardMapInt_t
2-D array of integers; can be converted to gameboard::BoardMap_t using gameboard::int_board_to_game_p...
vector< BoardSpace > get_all_spaces_occupied_by(const BoardMap_t &board_map, const PieceColor color)
PieceColor opponent_of(PieceColor color)
BoardMap_t int_board_to_game_pieces(const BoardMapInt_t int_board)
array< array< GamePiece, kNumFiles >, kNumRanks > BoardMap_t
2-D array of gameboard::GamePiece objects.
PieceColor get_color(const BoardMap_t &board_map, const BoardSpace &space)
const int kRepeatPeriodsToCheck[3]
Max allowed repetitions of prohibited move sequence lengths.
const int kMaxMovesWithoutCapture
const int kRepeatPeriodsMaxAllowed
Repeated move sequence lengths forbidden under move repetition rules.
PieceType get_type(const BoardMap_t &board_map, const BoardSpace &space)
bool hasRepeatingPattern(const std::vector< T > &vec, int lookback_length, int period)
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
MoveCountType moves_since_last_capture
gameboard::GamePiece destination_piece
A Xiangqi game piece described by its gameboard::PieceType and its gameboard::PieceColor.
Definition: game_piece.hpp:42
PieceColor piece_color
Definition: game_piece.hpp:44
A container for multiple gameboard::Move objects.
bool ContainsDestination(const gameboard::BoardSpace &space)
A gameboard::BoardSpace pair (start and end).
gameboard::BoardSpace end
gameboard::BoardSpace start
Defiition of miscellaneous free functions (and implementation of those that are templates).