-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
231 lines (226 loc) · 9.5 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <bits/stdc++.h>
#include "connection.cpp"
using namespace sf;
// For spacing the board and the pieces
#define X 702.0f
#define Y 75.0f
// Spacing between the pieces
#define spacing 51.0f
#include "moves.cpp"
// Background board color
#define boardColor Color(150,100,45,255)
// Paths to all the files
std::string paths[] {"images\\white-rook.png","images\\white-knight.png","images\\white-bishop.png","images\\white-queen.png","images\\white-king.png","images\\white-pawn.png","images\\black-rook.png","images\\black-knight.png","images\\black-bishop.png","images\\black-queen.png","images\\black-king.png","images\\black-pawn.png"};
// Board image path
#define board_path "images\\board.jpg"
// Heart of the program (Starting poistion and most probably updated after every move)
// const int boardLayout[8][8] = {{-1,-2,-3,-4,-5,-3,-2,-1},
// {-6,-6,-6,-6,-6,-6,-6,-6},
// {0,0,0,0,0,0,0,0},
// {0,0,0,0,0,0,0,0},
// {0,0,0,0,0,0,0,0},
// {0,0,0,0,0,0,0,0},
// {6,6,6,6,6,6,6,6},
// {1,2,3,4,5,3,2,1}};
// Updated the layout of the board
const int board[8][8] = {{0,1,2,3,4,5,6,7},
{8,9,10,11,12,13,14,15},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{31,30,29,28,27,26,25,24},
{16,17,18,19,20,21,22,23}};
// Loading the textures in the texture array
Texture pieces_text[12];
// Loading the sprites in the sprite array
Sprite pieces[32];
void set_position() {
// Main drawing loop that should be executed every single frame
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 8 ; j++) {
if(board[i][j] != 0 || (i == 0 && j == 0)) {
pieces[board[i][j]].setPosition({X+j*spacing,Y+i*spacing});
}
}
}
}
int main()
{
connectEngine("Engine/stockfish_13.exe");
// Arp sound used in the background (will probably change in the future)
SoundBuffer buffer;
if(!buffer.loadFromFile("Audio/Chess.wav"))
std::cout << "Error" << std::endl;
Sound sound;
sound.setLoop(true);
sound.setVolume(10.0f);
sound.setBuffer(buffer);
// Font used in the game
Font game_font;
game_font.loadFromFile("Fonts/Montserrat-Regular.ttf");
ContextSettings settings;
settings.antialiasingLevel = 8;
// Creating a window for containing all the elements
RenderWindow window(VideoMode(1200, 600), "Chess",Style::Close,settings);
window.setVerticalSyncEnabled(true);
moves::sq a;
// Limiting the framerate to 60 to decrease the load from GPU
window.setFramerateLimit(60);
// Creating a texture and assigning an image to it
Texture text;
text.loadFromFile(board_path);
text.setSmooth(true);
// Creating a sprite for the board
Sprite chessBoard;
chessBoard.setTexture(text);
chessBoard.setScale(0.4f,0.4f);
chessBoard.setPosition({700.0f,75.0f});
// Loading the textures from the files in the "path" array
for(int i = 0 ; i < 12 ; i++)
pieces_text[i].loadFromFile(paths[i]);
// Setting the textures to different spirites
for(int i = 0 ; i < 32 ; i++) {
if(i >= 8 && i <= 15)
pieces[i].setTexture(pieces_text[11]);
else if(i >= 24 && i <= 31)
pieces[i].setTexture(pieces_text[5]);
else if(i == 0 || i == 7)
pieces[i].setTexture(pieces_text[6]);
else if(i == 1 || i == 6)
pieces[i].setTexture(pieces_text[7]);
else if(i == 2 || i == 5)
pieces[i].setTexture(pieces_text[8]);
else if(i == 3)
pieces[i].setTexture(pieces_text[9]);
else if(i == 4)
pieces[i].setTexture(pieces_text[10]);
else if(i == 16 || i == 23)
pieces[i].setTexture(pieces_text[0]);
else if(i == 17 || i == 22)
pieces[i].setTexture(pieces_text[1]);
else if(i == 18 || i == 21)
pieces[i].setTexture(pieces_text[2]);
else if(i == 19)
pieces[i].setTexture(pieces_text[3]);
else
pieces[i].setTexture(pieces_text[4]);
}
// Some declarations
bool move = false;
int n = 0;
Vector2f oldPos,newPos;
Vector2f d;
set_position();
sound.play();
// Main game loop
while (window.isOpen())
{
// Creating an event which will look on all the activities related to the game window
sf::Event event;
while (window.pollEvent(event))
{
// If we cross thw window then it should close
if (event.type == Event::Closed) {
window.close();
}
if (event.type == Event::KeyPressed)
{
if (event.key.code == Keyboard::Escape)
{
window.close();
}
if (event.key.code == Keyboard::Q) {
for(int i = 0 ; i < 32 ; i++) {
std::cout << pieces[i].getGlobalBounds().left << "," << pieces[i].getGlobalBounds().top << "\n";
}
}
if (event.key.code == Keyboard::S) {
demoWrite();
}
if (event.key.code == Keyboard::R) {
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 8 ; j++) {
RectangleShape rec({48,48});
rec.setPosition({X+j*spacing,Y+i*spacing});
rec.setFillColor(Color::Transparent);
rec.setOutlineColor(Color::Green);
rec.setOutlineThickness(1.0f);
window.draw(rec);
}
}
}
}
else if (event.type == Event::MouseButtonPressed)
{
if (event.key.code == Mouse::Left) {
if(!move) {
for(int i = 0 ; i < 32 ; i++) {
if(pieces[i].getGlobalBounds().contains(Mouse::getPosition(window).x,Mouse::getPosition (window).y)) {
move = true;
n = i;
// d.x = Mouse::getPosition(window).x - pieces[i].getPosition().x;
// d.y = Mouse::getPosition(window).y - pieces[i].getPosition().y;
oldPos = pieces[n].getPosition();
}
}
}
else {
move = false;
// Capturing the new position for movement
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 8 ; j++) {
if((Mouse::getPosition(window).x > X+j*spacing && Mouse::getPosition(window).x < X+(j+1)*spacing) && (Mouse::getPosition(window).y > Y+i*spacing && Mouse::getPosition(window).y < Y+(i+1)*spacing)) {
newPos = Vector2f({X+j*spacing,Y+i*spacing});
break;
}
}
}
// Have to check here if the move is valid or not
// Making the position of the previous piece as {-100,-100} so that it disappears
for(int i = 0 ; i < 32 ; i++) {
if(pieces[i].getPosition() == newPos) {
pieces[i].setPosition({-100,-100});
break;
}
}
// Animation of moving the piece
auto dist = newPos - oldPos;
for(int i = 0 ; i < 40 ; i++) {
window.clear(boardColor);
window.draw(chessBoard);
pieces[n].move(dist.x/40,dist.y/40);
for(int j = 0 ; j < 32 ; j++)
window.draw(pieces[j]);
window.display();
}
// Setting the new position of the piece
pieces[n].setPosition(newPos);
}
}
}
}
// Clearing with the board color so that it looks good :)
window.clear(boardColor);
// Setting the required text and font for displaying the mouse coordinates for debugging purposes
Text mouse_coord;
mouse_coord.setFont(game_font);
mouse_coord.setCharacterSize(18);
mouse_coord.setFillColor(Color::Black);
mouse_coord.setString(std::to_string(Mouse::getPosition(window).x) + "," + std::to_string(Mouse::getPosition(window).y));
window.draw(mouse_coord);
// Drawing the chess board
window.draw(chessBoard);
// Drawing the pieces on the chess board
for(int i = 0 ; i < 32 ; i++)
window.draw(pieces[i]);
// Testing the demo code
a.demo(&window);
// Displaying all the sprites that were drawn on the window
window.display();
}
disconnectEngine();
return 0;
}