summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-02-16 20:22:42 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-02-16 20:23:08 +0100
commit640d882099af0f7c13dc5e102943bb06c2df5400 (patch)
tree5e65b3e0e3ad434ad130222c855d88e48fc44610
parent0584edb107467a294f17a50d063e667f22f4a47b (diff)
Cleaning arbiter:
- Add backup macros - User can fetch last captured piece
-rw-r--r--src/ChessArbiter.cpp27
-rw-r--r--src/ChessArbiter.hpp21
-rw-r--r--tests/chessarbiter.cpp1
3 files changed, 33 insertions, 16 deletions
diff --git a/src/ChessArbiter.cpp b/src/ChessArbiter.cpp
index 6e66939..2b34ef6 100644
--- a/src/ChessArbiter.cpp
+++ b/src/ChessArbiter.cpp
@@ -2,13 +2,12 @@
namespace chessarbiter {
ChessArbiter::ChessArbiter()
- : wPawn(1), wRook(5), wKnight(3), wBishop(3), wQueen(9), wKing(0), SAN("") {
-}
+ : wPawn(1), wRook(5), wKnight(3), wBishop(3), wQueen(9), wKing(0), SAN(""),
+ capture(' '), capture_last(' ') {}
void ChessArbiter::Setup(std::string fen) {
positions.clear();
SetFEN(fen);
- fen_last = this->fen;
positions[this->fen.board] = 1;
}
@@ -46,9 +45,13 @@ bool ChessArbiter::Play(std::string move) {
std::string dst = move.substr(2, 2);
bool IsCapture = !board.IsEmpty(dst);
FEN newFen = fen;
- SAN_last = SAN;
+ INIT_BACKUP();
SAN = "";
+ if (IsCapture) {
+ capture = board.GetPieceAt(dst).piece;
+ }
+
// Perform the move
if (move == "O-O" || move == "O-O-O") {
if (fen.player && move == "O-O") {
@@ -134,13 +137,11 @@ bool ChessArbiter::Play(std::string move) {
}
// Update fen!
- fen_last = fen;
fen = newFen;
// Check for illegal move
if (IsCheck(!fen.player)) {
- SetFEN(fen_last);
- SAN = SAN_last;
+ RESTORE_BACKUP();
return (false);
}
@@ -334,10 +335,9 @@ bool ChessArbiter::IsDrawByNoMoves() {
if (!IsCheck(fen.player)) {
std::vector<std::string> moves = ListLegalMoves(fen.player);
for (std::string &move : moves) {
+ INIT_BACKUP();
if (Play(move)) {
- positions[fen.board]--; // If move work, remove its position
- SetFEN(fen_last);
- SAN = SAN_last;
+ RESTORE_BACKUP();
return (false);
}
}
@@ -364,10 +364,9 @@ bool ChessArbiter::IsCheckMate() {
if (IsCheck(fen.player)) {
std::vector<std::string> moves = ListLegalMoves(fen.player);
for (std::string &move : moves) {
+ INIT_BACKUP();
if (Play(move)) {
- positions[fen.board]--; // If move work, remove its position
- SetFEN(fen_last);
- SAN = SAN_last;
+ RESTORE_BACKUP();
return (false);
}
}
@@ -377,5 +376,5 @@ bool ChessArbiter::IsCheckMate() {
}
std::string ChessArbiter::GetSAN() { return (SAN); }
-
+char ChessArbiter::GetCapture() { return (capture); }
} // namespace chessarbiter \ No newline at end of file
diff --git a/src/ChessArbiter.hpp b/src/ChessArbiter.hpp
index e5cec69..541253c 100644
--- a/src/ChessArbiter.hpp
+++ b/src/ChessArbiter.hpp
@@ -4,18 +4,34 @@
#include <iostream>
#include <unordered_map>
+#define INIT_BACKUP() \
+ FEN fen_backup = fen; \
+ char positions_backup = 0; \
+ if (positions.find(fen.board) != positions.end()) \
+ positions_backup = positions[fen.board]; \
+ std::string SAN_backup = SAN; \
+ char capture_backup = capture;
+
+#define RESTORE_BACKUP() \
+ SetFEN(fen_backup); \
+ if (positions_backup != 0) \
+ positions[fen.board] = positions_backup; \
+ SAN = SAN_backup; \
+ capture = capture_backup;
+
namespace chessarbiter {
class ChessArbiter {
Board board;
FEN fen;
- FEN fen_last; // To undo a move
int wPawn, wRook, wKnight, wBishop, wQueen, wKing;
/// @brief Use to compute occurences of positions
std::unordered_map<std::string, char> positions;
/// @brief FEN methods used internally
void SetFEN(std::string);
void SetFEN(FEN);
- std::string SAN,SAN_last;
+ std::string SAN, SAN_last;
+ char capture, capture_last;
+
public:
ChessArbiter();
void Setup(std::string);
@@ -36,6 +52,7 @@ public:
bool IsPlayable();
/// @brief Get pieces captures by a player
std::string GetCaptures(bool);
+ char GetCapture();
/// @brief Get the english SAN format of the last move
std::string GetSAN();
/// @brief List all the legal moves of a player
diff --git a/tests/chessarbiter.cpp b/tests/chessarbiter.cpp
index 475816e..48e58fa 100644
--- a/tests/chessarbiter.cpp
+++ b/tests/chessarbiter.cpp
@@ -367,4 +367,5 @@ TEST_CASE("SimpleCapture", "[SimplePieceCapture]") {
a.Play("e4d5");
CHECK(a.GetFEN() ==
"rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2");
+ CHECK(a.GetCapture()=='p');
}