2022-02-23 18:11:55 +01:00
|
|
|
#include "Game.hpp"
|
|
|
|
|
2022-12-31 20:45:03 +01:00
|
|
|
Game::Game() : current(nullptr), moves(nullptr), result("*") {
|
2022-02-23 18:11:55 +01:00
|
|
|
tags["White"] = "";
|
|
|
|
tags["Black"] = "";
|
|
|
|
initial_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
|
|
board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR";
|
|
|
|
}
|
|
|
|
|
2022-12-31 20:45:03 +01:00
|
|
|
Game::Game(std::string fen) : current(nullptr), moves(nullptr), result("*") {
|
2022-02-23 18:11:55 +01:00
|
|
|
tags["White"] = "";
|
|
|
|
tags["Black"] = "";
|
|
|
|
tags["FEN"] = fen;
|
|
|
|
initial_fen = fen;
|
|
|
|
board = chessarbiter::FENParser::Parse(fen).board;
|
|
|
|
}
|
|
|
|
|
2022-02-28 19:13:27 +01:00
|
|
|
Game::Game(HalfMove *m, std::string initial_fen) : result("*") {
|
2022-02-23 18:11:55 +01:00
|
|
|
moves = m;
|
|
|
|
current = m;
|
2022-02-23 18:47:18 +01:00
|
|
|
this->initial_fen = initial_fen;
|
2022-02-23 18:11:55 +01:00
|
|
|
board = chessarbiter::FENParser::Parse(initial_fen).board;
|
|
|
|
}
|
|
|
|
|
2022-12-30 18:43:21 +01:00
|
|
|
Game::Game(const Game* g){
|
|
|
|
board=g->board;
|
|
|
|
initial_fen=g->initial_fen;
|
2022-12-31 20:25:12 +01:00
|
|
|
board = chessarbiter::FENParser::Parse(initial_fen).board;
|
2022-12-30 18:43:21 +01:00
|
|
|
result=g->result;
|
|
|
|
tags=g->tags;
|
2022-12-31 20:25:12 +01:00
|
|
|
current=nullptr;
|
|
|
|
moves=nullptr;
|
2022-12-31 20:45:03 +01:00
|
|
|
if(g->moves != nullptr){
|
2022-12-30 18:43:21 +01:00
|
|
|
moves=new HalfMove(g->moves);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 19:13:27 +01:00
|
|
|
Game::~Game() {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (moves != nullptr) {
|
2022-02-28 19:13:27 +01:00
|
|
|
delete moves;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 18:11:55 +01:00
|
|
|
std::string Game::GetBoard() { return (board); }
|
|
|
|
|
|
|
|
std::string Game::GetTag(std::string tagname) { return (tags[tagname]); }
|
|
|
|
|
|
|
|
void Game::SetTag(std::string tagname, std::string value) {
|
|
|
|
tags[tagname] = value;
|
|
|
|
}
|
2022-02-25 13:38:49 +01:00
|
|
|
|
2022-02-23 18:11:55 +01:00
|
|
|
bool Game::IsBlackToPlay() {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (current == nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
return (!current->IsBlack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::DeleteTag(std::string tagname) { tags.erase(tagname); }
|
|
|
|
|
|
|
|
void Game::DeleteMove(HalfMove *m) {
|
|
|
|
if (moves == m) {
|
2022-12-31 20:45:03 +01:00
|
|
|
current = nullptr;
|
|
|
|
moves = nullptr;
|
2022-02-23 18:11:55 +01:00
|
|
|
delete m;
|
|
|
|
} else {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (m != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
current = m->GetParent();
|
2022-12-31 20:45:03 +01:00
|
|
|
if (current != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
current->RemoveChild(m);
|
|
|
|
}
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HalfMove *Game::GetCurrentMove() { return (current); }
|
2022-02-25 13:38:49 +01:00
|
|
|
|
2022-02-23 18:11:55 +01:00
|
|
|
HalfMove *Game::GetMoves() { return (moves); }
|
|
|
|
|
|
|
|
void Game::PromoteMove(HalfMove *m) {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (m != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
current = m;
|
|
|
|
m->Promote();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::SetMoveAsMainline(HalfMove *m) {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (m != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
current = m;
|
|
|
|
m->SetAsMainline();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Game::Play(std::string move) {
|
|
|
|
wxLogDebug("Playing move %s", move);
|
|
|
|
std::string fen = GetFen();
|
|
|
|
arbiter.Setup(fen);
|
|
|
|
if (arbiter.Play(move)) {
|
2022-12-29 10:08:22 +01:00
|
|
|
HalfMove *m = new HalfMove(move, arbiter.GetSAN(), arbiter.GetFEN());
|
2022-02-23 18:11:55 +01:00
|
|
|
char capture = arbiter.GetCapture();
|
|
|
|
if (capture != ' ') {
|
|
|
|
wxLogDebug("%c", capture);
|
|
|
|
m->SetCapture(capture);
|
|
|
|
}
|
2022-12-31 20:45:03 +01:00
|
|
|
if (current != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
current->AddMove(m);
|
2022-12-31 20:45:03 +01:00
|
|
|
} else if (moves != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
moves->AddVariation(m);
|
|
|
|
}
|
|
|
|
current = m;
|
2022-12-31 20:45:03 +01:00
|
|
|
if (moves == nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
moves = m;
|
|
|
|
}
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::Previous() {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (current != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
current = current->GetParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Game::ListTags() {
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
for (auto const &element : tags) {
|
|
|
|
keys.push_back(element.first);
|
|
|
|
}
|
|
|
|
return (keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::Next() {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (current != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
HalfMove *m = current->GetMainline();
|
2022-12-31 20:45:03 +01:00
|
|
|
if (m != nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
current = m;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
current = moves;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::SetCurrent(HalfMove *m) { current = m; }
|
|
|
|
|
|
|
|
std::string Game::GetFen() {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (current == nullptr) {
|
2022-02-23 18:11:55 +01:00
|
|
|
return (initial_fen);
|
|
|
|
}
|
|
|
|
return (current->GetFen());
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:58:02 +01:00
|
|
|
std::string Game::GetResult() { return (result); }
|
2022-02-25 11:13:35 +01:00
|
|
|
|
|
|
|
void Game::SetResult(std::string result) { this->result = result; }
|
2022-02-25 11:42:46 +01:00
|
|
|
|
|
|
|
void Game::BuildAndVerify() {
|
2022-12-31 20:45:03 +01:00
|
|
|
if (moves != nullptr) {
|
2022-02-25 11:42:46 +01:00
|
|
|
moves->BuildAndVerify(GetFen());
|
|
|
|
}
|
|
|
|
}
|