-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
101 lines (83 loc) · 2.48 KB
/
Copy pathGame.cpp
File metadata and controls
101 lines (83 loc) · 2.48 KB
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
//
// Created by Benedikt Hornig on 15.09.22.
//
#include <iostream>
#include "Game.h"
// 10 Minute game
auto PLAYER_MAX_TIME = std::chrono::milliseconds(1000 * 600);
Game::Game() {
timeWhite = PLAYER_MAX_TIME;
timeBlack = PLAYER_MAX_TIME;
this->board = Board();
moveStart = std::chrono::high_resolution_clock::now();
}
void Game::step(const Move& move) {
// update players clocks
auto moveTime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - moveStart);
if (board.getTurn() == WHITE) {
timeWhite = timeWhite - moveTime;
} else {
timeBlack = timeBlack - moveTime;
}
if (move == UNDO_MOVE) {
board.undoMove();
board.undoMove();
moveHistory.erase(moveHistory.end() - 2, moveHistory.end());
} else {
board.makeMove(move);
moveHistory.push_back(move);
}
// start new move timer
moveStart = std::chrono::high_resolution_clock::now();
}
bool Game::isOver() {
return board.isOver();
}
Color Game::getResult() {
return board.getResult();
}
void Game::visualizeBoard() {
// display player clocks and the current board
auto secondsLeftWhite = std::chrono::duration_cast<std::chrono::seconds>(timeWhite).count();
std::cout << "White: " << secondsLeftWhite / 60 << ":";
auto secondsWhite = secondsLeftWhite - secondsLeftWhite / 60 * 60;
if (secondsWhite < 10) {
std::cout << "0";
}
std::cout << secondsWhite << std::endl;
auto secondsLeftBlack = std::chrono::duration_cast<std::chrono::seconds>(timeBlack).count();
std::cout << "Black: " << secondsLeftBlack / 60 << ":";
auto secondsBlack = secondsLeftBlack - secondsLeftBlack / 60 * 60;
if (secondsBlack < 10) {
std::cout << "0";
}
std::cout << secondsBlack << std::endl;
board.visualizeBoard(WHITE);
}
std::string Game::getPGN() {
std::string res;
for (int i = 0; i < moveHistory.size(); ++i) {
if (i % 2 == 0) {
res += std::to_string(i / 2 + 1);
res += ". ";
}
res += std::string(moveHistory[i]);
res += " ";
}
return res;
}
std::string Game::getBoardNotation() {
return board.getBoardNotation();
}
bool Game::getTurn() {
return board.getTurn();
}
Board Game::getBoard() {
return board;
}
void Game::setBoardByNotation(const std::string& notation) {
board.setBoardByNotation(notation);
}
int Game::getMoveCount() {
return (int) moveHistory.size();
}