-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
121 lines (101 loc) · 3.79 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
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
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Chessboard.h"
#include "Piece.h"
#include "Pawn.h"
#include "Knight.h"
#include "Bishop.h"
#include "Queen.h"
#include "King.h"
#include "Rook.h"
#include "Input.h"
#include "GameManager.h"
using std::vector;
int main() {
std::cout << "Program Start.." << std::endl;
// the width and height of the window
const int width = 1200;
const int height = 1200;
sf::RenderWindow window;
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
window.create(sf::VideoMode(1024, 768, desktop.bitsPerPixel), "SFML window");
window.setFramerateLimit(60);
// load the background texture for chessboard
sf::Texture light_green_bg;
if (!light_green_bg.loadFromFile("assets/light_green_bg.png")) {
std::cerr << "Failed to load image \"light_green_bg.png\"" << std::endl;
}
sf::Texture dark_green_bg;
if (!dark_green_bg.loadFromFile("assets/dark_green_bg.png")) {
std::cerr << "Failed to load image \"dark_green_bg.png\"" << std::endl;
}
Chessboard board(light_green_bg, dark_green_bg);
// load the texture for the chess pieces
sf::Texture chess_piece_texture;
if (!chess_piece_texture.loadFromFile("assets/chess_pieces.png")) {
std::cerr << "Failed to load image" << std::endl;
}
chess_piece_texture.setSmooth(true);
// Create the game manager
GameManager GM = GameManager(chess_piece_texture);
GM.InitialPrompt();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case (sf::Event::Closed):
window.close();
break;
default:
break;
}
}
// Render the chess pieces and the chess board.
window.clear(sf::Color(49, 46, 43)); // dark dark background
board.renderBoard(window);
for (unsigned int i = 0; i < GM.white->set.size(); i++) {
if (GM.white->set.at(i).getInPlay()) {
GM.white->set.at(i).drawSprite(window);
}
}
for (unsigned int i = 0; i < GM.black->set.size(); i++) {
if (GM.black->set.at(i).getInPlay()) {
GM.black->set.at(i).drawSprite(window);
}
}
window.display();
// Get the user input, the piece position and it's destination
GM.usersMove.clear();
GM.promptUserInput();
int x0 = get<0>(GM.usersMove[0]);
int y0 = get<1>(GM.usersMove[0]);
// If the chess piece exist at the target/starting position
GM.getPiece(x0,y0);
if (GM.currPiece != nullptr) {
int xf = get<0>(GM.usersMove[1]);
int yf = get<1>(GM.usersMove[1]);
// check if the move for that piece is legal or not
if (GM.currPiece->isLegal(xf, yf) && GM.currPiece->getColorInt() == GM.currTurn) {
if (GM.isSpaceFree(GM.usersMove[1])) {
// empty space at the destination, move the piece there
GM.currPiece->move(xf,yf);
} else {
// enemy piece at destination, capture the piece there
GM.CapturePiece();
}
// switch turns from white to black, or black to white
if (GM.currTurn == 1) {
GM.currTurn = 2;
} else {
GM.currTurn = 1;
}
} else {
cout << "The move is not legal, please enter the coordinates again." << endl;
}
} else {
cout << "No chess piece at this location, please enter the coordinates again." << endl;
}
}
cout << "..Finished\n" << endl;
return EXIT_SUCCESS;
}