aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/Game.cpp
blob: c7ecced93ad928c8733269fad8dea2d611f914a2 (plain)
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
#include "Game.hpp"

Game::Game() : current(nullptr), moves(nullptr), result("*") {
  tags["White"] = "";
  tags["Black"] = "";
  initial_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
  board = "rnbqkbnrpppppppp                                PPPPPPPPRNBQKBNR";
}

Game::Game(std::string fen) : current(nullptr), moves(nullptr), result("*") {
  tags["White"] = "";
  tags["Black"] = "";
  tags["FEN"] = fen;
  initial_fen = fen;
  board = chessarbiter::FENParser::Parse(fen).board;
}

Game::Game(HalfMove *m, std::string initial_fen) : result("*") {
  moves = m;
  current = m;
  this->initial_fen = initial_fen;
  board = chessarbiter::FENParser::Parse(initial_fen).board;
}

Game::Game(const Game* g){
  board=g->board;
  initial_fen=g->initial_fen;
  board = chessarbiter::FENParser::Parse(initial_fen).board;
  result=g->result;
  tags=g->tags;
  current=nullptr;
  moves=nullptr;
  if(g->moves != nullptr){
    moves=new HalfMove(g->moves);
  }
}

Game::~Game() {
  if (moves != nullptr) {
    delete moves;
  }
}

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;
}

bool Game::IsBlackToPlay() {
  if (current == nullptr) {
    return (false);
  }
  return (!current->IsBlack());
}

void Game::DeleteTag(std::string tagname) { tags.erase(tagname); }

void Game::DeleteMove(HalfMove *m) {
  if (moves == m) {
    current = nullptr;
    moves = nullptr;
    delete m;
  } else {
    if (m != nullptr) {
      current = static_cast<HalfMove*>(m->GetParent());
      if (current != nullptr) {
        current->RemoveChild(m);
      }
      delete m;
    }
  }
}

HalfMove *Game::GetCurrentMove() { return (current); }

HalfMove *Game::GetMoves() { return (moves); }

void Game::PromoteMove(HalfMove *m) {
  if (m != nullptr) {
    current = m;
    m->Promote();
  }
}

void Game::SetMoveAsMainline(HalfMove *m) {
  if (m != nullptr) {
    current = m;
    m->SetAsMainline();
  }
}

bool Game::IsCheckmate(bool forBlack){
  arbiter.Setup(GetFen());
  if(arbiter.IsCheckMate()){
    if(forBlack)
      return arbiter.IsBlackTurn();
    return !arbiter.IsBlackTurn();
  }
  return false;
}

bool Game::IsPromotionMove(std::string absolute_move){
  arbiter.Setup(GetFen());
  arbiter.Play(absolute_move);
  return(arbiter.WasPawnPromotion());
}

bool Game::Play(std::string move,char promotion) {
  wxLogDebug("Playing move %s", move);
  std::string fen = GetFen();
  arbiter.Setup(fen);
  if (arbiter.Play(move,promotion)) {
    HalfMove *m = new HalfMove(move, arbiter.GetSAN(), arbiter.GetFEN());
    char capture = arbiter.GetCapture();
    if (capture != ' ') {
      wxLogDebug("%c", capture);
      m->SetCapture(capture);
    }
    if (current != nullptr) {
      current->AddMove(m);
    } else if (moves != nullptr) {
      moves->AddVariation(m);
    }
    current = m;
    if (moves == nullptr) {
      moves = m;
    }
    return (true);
  }
  return (false);
}

void Game::GetOpening(std::string &name,std::string &eco){
  HalfMove *m=current;
  if(m == nullptr)
    m=moves;
  if(m!=nullptr){
    // First check if opening already set
    std::string cname,ceco;
    m->GetOpening(cname,ceco);
    if(ceco.size()>0){
      name=cname;
      eco=ceco;
    }else {
      // If not, get the current move line (or first move)
      // and try to guess opening
      wxGetApp().GetBook().GuessOpening(m->GetLineAsSAN(),name,eco);
      m->SetOpening(name,eco);
    }
  }
}

void Game::Previous() {
  if (current != nullptr) {
    current = static_cast<HalfMove*>(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() {
  if (current != nullptr) {
    HalfMove *m = static_cast<HalfMove*>(current->GetMainline());
    if (m != nullptr) {
      current = m;
    }
  } else {
    current = moves;
  }
}

HalfMove *Game::GetNextMove(){
  if(current!=nullptr)
    return static_cast<HalfMove*>(current->GetMainline());
  return moves;
}

void Game::SetCurrent(HalfMove *m) { current = m; }

std::string Game::GetFen() {
  if (current == nullptr) {
    return (initial_fen);
  }
  return (current->GetFen());
}

std::string Game::GetResult() { return (result); }

void Game::SetResult(std::string result) { this->result = result; }

void Game::BuildAndVerify() {
  if (moves != nullptr) {
    moves->BuildAndVerify(GetFen());
  }
}