-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
403 lines (334 loc) · 11.7 KB
/
main.cpp
File metadata and controls
403 lines (334 loc) · 11.7 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
////////////////////////////////////////////////////////////////////////////
// //
// * Chinese Checkers * //
// //
// Author: Cédric Schoonen <cedric.schoonen1@gmail.com> //
// February 2020 //
// //
// Developped under Ubuntu 18.04 with g++ 7.4.0 and sfml 2.4 //
// Compile with $ g++ -o chinese_checkers main.cpp Board.h Board.cpp \ //
// -lsfml-graphics -lsfml-window -lsfml-system //
// //
// Controls: You can select a pawn by left-clicking on it and place //
// it by releasing the mouse above the destination vertex. //
// Press "a" to play with the current algorithm in use. //
// Press "z" to undo a move. Press "m" to toggle the //
// display of available moves for the selected pawn. //
// Press "r" to replay a move from the input file "data/ //
// record_in.dat". A record of each game is automatically //
// written to the file "data/record.dat". //
// //
////////////////////////////////////////////////////////////////////////////
/// next on the todo list
// o compilation for windows
// o report on how the game can be played and implementation details
// o better algorithms (see algorithm.cpp)
/// current minor problems:
//
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <thread>
#include <SFML/Graphics.hpp>
#include "Board.h"
#include "rendering.cpp"
#include "algorithm.cpp"
using namespace std;
int main()
{
/////////////////////////////// Files //////////////////////////////////
system("mkdir -p data");
ofstream recordFile("data/record.dat");
ifstream recordInFile("data/record_in.dat");
///////////////////////////// Game board ///////////////////////////////
Hexagram board(6,3);
/////////////////////////////// Window /////////////////////////////////
sf::RenderWindow window(sf::VideoMode(640,640), "Chinese Checkers");
double boardLengthX = board.getTotalSizeX();
double boardLengthY = board.getTotalSizeY();
sf::View view(sf::FloatRect(-boardLengthX*0.55, -boardLengthY*0.55,
boardLengthX*1.10, boardLengthY*1.10));
window.setView(view);
window.setFramerateLimit(10);
////////////////////////////// Game loop ///////////////////////////////
bool gameEnded = false;
int pawnSelected = -1;
int counterMoves = 0;
bool showAvailableMoves = false;
// board save at each move
vector<Hexagram> boardSaves(1,board);
// seed from algorithm.cpp
cout << "seed = " << seed << endl;
while (window.isOpen())
{
/////////////////////// Event processing ///////////////////////////
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
// undo when key 'Z' is pressed
if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::Z && !gameEnded)
{
if (boardSaves.size()>0)
{
// reset to last board saved
board = boardSaves.back();
boardSaves.pop_back();
counterMoves --;
recordFile << "Undo" << endl;
}
}
// toggle show available moves when key 'M' is pressed
if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::M && !gameEnded)
{
showAvailableMoves = !showAvailableMoves;
}
// make a move using an algorithm when key 'A' is pressed
if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::A && !gameEnded)
{
// save the board before making changes
Hexagram boardSave = board;
// decide move to perform
// we copy the board to prevent the algorithm from making
// changes
int ipawnToMove = -1;
int ivertexDestination = -1;
Hexagram boardCopy = board;
algorithm(boardCopy, ipawnToMove, ivertexDestination);
// place selected pawn
int status = board.move(ipawnToMove, ivertexDestination, recordFile);
if (status == 0)
{
counterMoves ++;
boardSaves.push_back(boardSave);
#ifdef DEBUG
cout << "*** Board print ***" << endl;
board.print();
cout << "*******************" << endl;
#endif
}
else
{
cout << "Move is not valid, error code "
<< status << endl;
}
}
// select pawn by pressing mouse left click
if (event.type == sf::Event::MouseButtonPressed && !gameEnded)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
// coordinates of mouse in graph
sf::Vector2i windowCoords(event.mouseButton.x,
event.mouseButton.y);
double graphX = window.mapPixelToCoords(windowCoords).x;
double graphY = window.mapPixelToCoords(windowCoords).y;
// find the closest vertex
vector<Vertex> vertices = board.getVertices();
int ivertexMin = -1;
double distanceMin = -1;
for (int i=0; i<vertices.size(); i++)
{
double x = vertices[i].getX();
double y = vertices[i].getY();
double d = sqrt((graphX-x)*(graphX-x)
+(graphY-y)*(graphY-y));
if (d<distanceMin || ivertexMin<0)
{
ivertexMin = i;
distanceMin = d;
}
}
// find selected pawn
pawnSelected = board.getPawnFromVertex(ivertexMin);
#ifdef DEBUG
cout << "---- Mouse left click pressed ---" << endl;
cout << "mouse graphX = " << graphX << endl;
cout << "mouse graphY = " << graphY << endl;
cout << "vertexSelected = " << ivertexMin << endl;
cout << "pawnSelected = " << pawnSelected << endl;
cout << "available moves for selected pawn = ";
for (int ivertexAvail : board.availableMovesDirect(ivertexMin))
cout << ivertexAvail << " ";
for (int ivertexAvail : board.availableMovesHopping(ivertexMin))
cout << ivertexAvail << " ";
cout << endl;
#endif
// check if selected pawn is in the right team
vector<Pawn> pawns = board.getPawns();
if (pawns[pawnSelected].getTeam() !=
board.getPlayingTeam())
{
pawnSelected = -1;
cout << "Selected pawn not in the right team" << endl;
}
}
}
// place selected pawn by releasing mouse left click
if (event.type == sf::Event::MouseButtonReleased && !gameEnded)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
// coordinates of mouse in graph
sf::Vector2i windowCoords(event.mouseButton.x,
event.mouseButton.y);
double graphX = window.mapPixelToCoords(windowCoords).x;
double graphY = window.mapPixelToCoords(windowCoords).y;
// find the closest vertex
vector<Vertex> vertices = board.getVertices();
int ivertexMin = -1;
double distanceMin = -1;
for (int i=0; i<vertices.size(); i++)
{
double x = vertices[i].getX();
double y = vertices[i].getY();
double d = sqrt((graphX-x)*(graphX-x)
+(graphY-y)*(graphY-y));
if (d<distanceMin || ivertexMin<0)
{
ivertexMin = i;
distanceMin = d;
}
}
#ifdef DEBUG
cout << "---- Mouse left click released ---" << endl;
cout << "mouse graphX = " << graphX << endl;
cout << "mouse graphY = " << graphY << endl;
cout << "vertexSelected = " << ivertexMin << endl;
#endif
// save the board before making changes
Hexagram boardSave = board;
// place selected pawn
int status = board.move(pawnSelected, ivertexMin, recordFile);
if (status == 0)
{
counterMoves ++;
boardSaves.push_back(boardSave);
#ifdef DEBUG
cout << "*** Board print ***" << endl;
board.print();
cout << "*******************" << endl;
#endif
}
else
{
cout << "Move is not valid, error code "
<< status << endl;
}
// clear selected pawn
pawnSelected = -1;
}
}
// replay from file when key 'R' is pressed
if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::R && !gameEnded)
{
if (recordInFile)
{
string line = "";
getline(recordInFile,line);
if (line.substr(0,4) == "Move")
{
#ifdef DEBUG
cout << "--- Replay move ---" << endl;
cout << "line = \"" << line << "\"" << endl;
#endif
// string is formatted as "Move from vertex <num> to <num>"
// find position of the first number in string
int posFrom = 0;
for (int i=4; i<line.size(); i++)
if (line[i-4] == 't' && line[i-3] == 'e' &&
line[i-2] == 'x' && line[i-1] == ' ') posFrom = i;
// find position of the second number in string
int posTo = 0;
for (int i=posFrom; i<line.size(); i++)
if (line[i-4] == ' ' && line[i-3] == 't' &&
line[i-2] == 'o' && line[i-1] == ' ') posTo = i;
// identify move
int ivertexFrom = stoi(line.substr(posFrom,posTo-4-posFrom));
int ivertexTo = stoi(line.substr(posTo,line.size()-posTo));
int ipawn = board.getPawnFromVertex(ivertexFrom);
#ifdef DEBUG
cout << "identified move from vertex " << ivertexFrom
<< " to " << ivertexTo << endl;
#endif
// save the board before making changes
Hexagram boardSave = board;
// place selected pawn
int status = board.move(ipawn, ivertexTo, recordFile);
if (status == 0)
{
counterMoves ++;
boardSaves.push_back(boardSave);
#ifdef DEBUG
cout << "*** Board print ***" << endl;
board.print();
cout << "*******************" << endl;
#endif
}
else
{
cout << "Move to replay is not valid, error code "
<< status << endl;
}
}
else if (line == "Undo")
{
#ifdef DEBUG
cout << "--- Replay undo ---" << endl;
cout << "line = \"" << line << "\"" << endl;
#endif
if (boardSaves.size()>0)
{
// reset to last board saved
board = boardSaves.back();
boardSaves.pop_back();
counterMoves --;
recordFile << "Undo" << endl;
}
else
{
cout << "Impossible to replay undo" << endl;
}
}
else
{
#ifdef DEBUG
cout << "--- Replay line not recognised ---" << endl;
cout << "line = \"" << line << "\"" << endl;
#endif
}
}
}
}
/////////////////////////// Rendering //////////////////////////////
window.clear(sf::Color::White);
renderHomes(window,board);
renderBoardEdges(window,board);
renderBoardVertices(window,board);
#ifdef DEBUG
renderTextVertices(window,board);
#endif
renderPawns(window,board,pawnSelected);
if (pawnSelected >= 0)
renderSelectedPawn(window,board,pawnSelected);
if (pawnSelected >= 0 && showAvailableMoves)
renderAvailableMoves(window,board,pawnSelected);
renderWinners(window,board);
window.display();
///////////////////////////// Misc. ////////////////////////////////
// detect end of the game
if (board.getPlayingTeam()<0) gameEnded = true;
////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////
return 0;
}