-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
83 lines (72 loc) · 2.34 KB
/
main.cpp
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
/*
* CHESS
* This game is created to explore algorithms and C++ in general.
* It is mainly a project to gain experience and understanding in these topics,
* so it probably won't be the best chess program.
*
* main.cpp
* Created by Lukas Bos on 30/11/2017.
* Initialize a board and the players and handle the immediate user input
* Initialize a proper interface for the player/viewer
*
*/
#include <SFML/Graphics.hpp>
#include "Board.h"
#include "Interface.h"
#include "players/HumanPlayer.h"
#include "players/MinMaxPlayer.h"
#include "players/RandomPlayer.h"
#include "players/MonteCarloPlayer.h"
#include "players/HybridPlayer.h"
using namespace sf;
int main() {
ContextSettings settings;
settings.antialiasingLevel = 8;
RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "Chess", Style::Default, settings);
window.setFramerateLimit(40);
Board board = Board();
Interface interface(&board, window);
Player *bottomPlayer = new MinMaxPlayer(PieceColor::WHITE, 2);
Player *topPlayer = new HybridPlayer(PieceColor::BLACK, 2);
// white begins
Player *currentPlayer = bottomPlayer->getColor()==PieceColor::WHITE ? bottomPlayer : topPlayer;
board.startGame(bottomPlayer, topPlayer, currentPlayer);
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
// Handle Events
if (event.type==Event::Closed) {
window.close();
}
if (event.type==Event::MouseButtonPressed) {
if (currentPlayer->isHuman) {
currentPlayer->setNextMove(interface.getHumanMove());
}
}
if (event.type==Event::KeyPressed) {
if (Keyboard::isKeyPressed(Keyboard::R)) { // humanplayer plays as White
board.undoMove();
}
}
interface.draw();
window.display();
}
Move *nextMove = currentPlayer->getNextMove(&board);
if (nextMove) {
board.doMove(nextMove);
board.checkGameStatus();
// remove the move from the player so it won't be tried again next time.
currentPlayer->setNextMove(nullptr);
interface.selectedSquare = nullptr;
// switch player after move
if (currentPlayer==topPlayer) {
currentPlayer = bottomPlayer;
} else {
currentPlayer = topPlayer;
}
board.setCurrentPlayer(currentPlayer);
interface.draw();
window.display();
}
}
}