-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.cpp
609 lines (482 loc) · 16 KB
/
backend.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#include "backend.hpp"
#include "include/Exceptions.hpp"
#include <dirent.h>
#include <fstream>
#include <iomanip>
#include <sys/stat.h>
using namespace std;
//__________________________________________________________________________________________________ game
//__________________________________________________________________________
void BackEnd::setGame(QString gameName)
{
manager->setGameName(gameName.QString::toStdString());
}
//__________________________________________________________________________
void BackEnd::startGame()
{
manager->startGame();
}
//__________________________________________________________________________ restartGame
void BackEnd::restartGame()
{
manager->restartGame();
previewsSrc = -1;
srcIndex = -1;
destIndex = -1;
}
//__________________________________________________________________________
void BackEnd::saveAndExit()
{
manager->endGame();
manager->saveAutoSaved();
manager->getFileManager()->resetData();
previewsSrc = -1;
srcIndex = -1;
destIndex = -1;
}
//__________________________________________________________________________ endGame
void BackEnd::endGame()
{
manager->endGame();
manager->getFileManager()->removeFile();
previewsSrc = -1;
srcIndex = -1;
destIndex = -1;
}
//__________________________________________________________________________
bool BackEnd::checkInput(QString name)
{
for (auto ch : name.toStdString())
if (!isalpha(ch))
if (ch != ' ' && ch != '_' && !isdigit(ch))
return false;
return true;
}
//__________________________________________________________________________
void BackEnd::getFiles()
{
//if save file doesn't exist , makes one
struct stat buff;
if (stat("./SavedGames", &buff) != 0)
if (mkdir("./SavedGames", S_IRUSR | S_IWUSR | S_IXUSR) != 0)
throw runtime_error("can't make save files");
_dirFiles.clear();
DIR *dir;
struct dirent *diread;
if ((dir = opendir("./SavedGames")) != nullptr) {
while ((diread = readdir(dir)) != nullptr) {
string temp = diread->d_name;
string suffix = temp.substr(temp.find('.'), temp.back());
if (suffix == ".txt") {
_dirFiles.push_back(temp);
}
}
closedir(dir);
} else {
perror("opendir");
throw("can't acces files");
}
}
//__________________________________________________________________________ get file
//________________// read plyer info
QString BackEnd::getFileInfo(unsigned index)
{
manager->getFileManager()->readFile(_dirFiles[index]);
QString name1 = QString::fromStdString(manager->getFileManager()->get_P1_Name());
QString name2 = QString::fromStdString(manager->getFileManager()->get_P2_Name());
QString score1 = QString::fromStdString(
'(' + to_string(manager->getFileManager()->get_P1_Score()) + ')');
QString score2 = QString::fromStdString(
'(' + to_string(manager->getFileManager()->get_P2_Score()) + ')');
QString fileName = QString::fromStdString(_dirFiles[index]);
QString temp = fileName + " : P1: " + name1 + score1 + " P2: " + name2 + score2;
return temp;
}
//__________________________________________________________________________ load test
void BackEnd::loadGame(unsigned index)
{
qDebug() << "loaded file: " << _dirFiles[index].c_str();
try {
manager->loadGame(_dirFiles[index]);
emit gameLoaded();
} catch (exception &s) {
qDebug() << s.what();
emit fileError();
}
}
//__________________________________________________________________________ filesCount
unsigned BackEnd::filesCount()
{
return _dirFiles.size();
}
//__________________________________________________________________________ deleteFile
void BackEnd::deleteFile(unsigned index)
{
remove(("./SavedGames/" + (_dirFiles[index])).c_str());
this->getFiles();
}
//__________________________________________________________________________ i & j and index convertion
//__________________________________________________________________________ IJToIndex
unsigned IJToIndex(pair<unsigned int, unsigned int> index)
{
return index.first * 8 + index.second;
}
//__________________________________________________________________________ indexToIJ
std::pair<unsigned, unsigned> indexToIJ(unsigned index)
{
unsigned j = index % 8;
unsigned i = 0;
while (index >= 8) {
i++;
index -= 8;
}
return make_pair(i, j);
}
//______________________________________________________________________________________________________ User options
//__________________________________________________________________________ setP1Name
void BackEnd::setP1(QString P1Name)
{
manager->setUser1(P1Name.toStdString(), 0, 0);
}
//__________________________________________________________________________ setP2Name
void BackEnd::setP2(QString P2Name)
{
manager->setUser2(P2Name.toStdString(), 0, 0);
}
//__________________________________________________________________________ getGameName
QString BackEnd::getP1Name()
{
return QString::fromStdString(manager->getUser1()->getName());
}
//__________________________________________________________________________ getGameName
QString BackEnd::getP2Name()
{
return QString::fromStdString(manager->getUser2()->getName());
}
//__________________________________________________________________________ get P1 Positive Score
int BackEnd::getP1_PScore()
{
return manager->getUser1()->getScore();
}
//__________________________________________________________________________ get P1 Negative Score
int BackEnd::getP1_NScore()
{
return manager->getUser1()->getNegativeScore();
}
//__________________________________________________________________________ get P2 Positive Score
int BackEnd::getP2_PScore()
{
return manager->getUser2()->getScore();
}
//__________________________________________________________________________ get P2 Negative Score
int BackEnd::getP2_NScore()
{
return manager->getUser2()->getNegativeScore();
}
//______________________________________________________________________________________________________ Board options
//__________________________________________________________________________ getGameName
QString BackEnd::getGameName()
{
return QString::fromStdString(manager->getGameName());
}
//__________________________________________________________________________ Winner
unsigned BackEnd::winner()
{
// qDebug() << "winner: " << manager->getWinner();
return manager->getWinner();
}
//__________________________________________________________________________ random move
void BackEnd::checkRandomMove()
{
if (manager->getTurn() == GameManager::USER1) {
if (manager->getUser1()->getNegativeScore() >= 15) {
manager->getUser1()->decNegativeScore(15);
this->randomMove();
}
} else {
if (manager->getUser2()->getNegativeScore() >= 15) {
manager->getUser2()->decNegativeScore(15);
this->randomMove();
}
}
}
//__________________________________________________________________________ setCanceler
void BackEnd::setCanceler()
{
this->_canceler = manager->getTurn();
emit cancel();
}
//__________________________________________________________________________ getCanceler
unsigned BackEnd::getCanceler()
{
return this->_canceler;
}
//__________________________________________________________________________ getIcon
QString BackEnd::getIcon(unsigned index)
{
return QString::fromStdString(
manager->getChessBoardGame()->getCell(indexToIJ((index))).getIcon());
}
//__________________________________________________________________________ choose
unsigned BackEnd::choose(unsigned index)
{
// qDebug() << "choosen: " << index;
// qDebug() << "choosen: " << indexToIJ(index);
Chessman::Index src = indexToIJ(index);
try {
srcState = manager->getCellState(src);
}
catch (EmptySquare &s) {
qDebug() << s.what();
return EMPTY;
}
catch (AccessDenied &s) {
qDebug() << s.what();
return UNACCESSABLE;
}
srcIndex = index;
emit choosen();
return OK;
}
//__________________________________________________________________________ canGo
bool BackEnd::canGo(unsigned index, std::vector<std::pair<unsigned int, unsigned int>> bkndcanGo)
{
for (auto const &item : bkndcanGo) {
if (IJToIndex(item) == index) //if this square is in the available squares which piece can go
return true;
}
return false;
}
//__________________________________________________________________________ canHit
bool BackEnd::canHit(unsigned index, std::vector<std::pair<unsigned int, unsigned int>> bkndcanHit)
{
for (auto const &item : bkndcanHit) {
if (IJToIndex(item)
== index) //if this square is in the available squares which piece can hit
return true;
}
return false;
}
//__________________________________________________________________________ cellState
unsigned BackEnd::cellState(unsigned index)
{
if (index == (unsigned) srcIndex)
return SELECTED;
else if (canHit(index, srcState.second))
return CANHIT;
else if (canGo(index, srcState.first))
return CANGO;
else
return UNAVAILABLE;
}
//__________________________________________________________________________ isMoved
bool BackEnd::isMoved(unsigned index)
{
if (previewsSrc != -1 && destIndex != -1)
if (index == previewsSrc || index == destIndex)
return true;
return false;
}
//__________________________________________________________________________ getSrcI
unsigned BackEnd::getSrcI()
{
return indexToIJ(previewsSrc).first;
}
//__________________________________________________________________________ getSrcJ
unsigned BackEnd::getSrcJ()
{
return indexToIJ(previewsSrc).second;
}
//__________________________________________________________________________ getDestI
unsigned BackEnd::getDestI()
{
return indexToIJ(destIndex).first;
}
//__________________________________________________________________________ getDestJ
unsigned BackEnd::getDestJ()
{
return indexToIJ(destIndex).second;
}
//__________________________________________________________________________ getSrcIndex
unsigned BackEnd::getSrcIndex()
{
return this->previewsSrc;
}
//__________________________________________________________________________ getDestIndex
unsigned BackEnd::getDestIndex()
{
return this->destIndex;
}
//QString BackEnd::getHitPiece()
//{
// return QString::fromStdString(manager->getHitPiece());
//}
//__________________________________________________________________________ unchoosePiece
bool BackEnd::unchoosePiece(unsigned index)
{
if (_change) { //____________ on changing piece no error should be opened
_change = false;
return true;
}
if (index == (unsigned) srcIndex) {
emit unchoosen();
return true;
}
else
return false;
}
//__________________________________________________________________________ P1 outs
QString BackEnd::getP1OutsIcon(unsigned index)
{
auto outs = manager->getUser1()->getChessmansOut();
if (index + 1 > outs.size() || outs.size() == 0)
return "";
else {
return QString::fromStdString(outs[index]->getIcon());
}
}
//__________________________________________________________________________ P2 outs
QString BackEnd::getP2OutsIcon(unsigned index)
{
auto outs = manager->getUser2()->getChessmansOut();
//if index is in outs length
if (index < outs.size())
return QString::fromStdString(outs[index]->getIcon());
else {
return "";
}
}
//__________________________________________________________________________ move
bool BackEnd::move(unsigned index) noexcept
{
// qDebug() << "src: " << srcIndex;
// qDebug() << "src: " << indexToIJ(srcIndex);
// qDebug() << "dest: " << index;
// qDebug() << "dest: " << indexToIJ(index);
//unchoose
if (cellState(index) == SELECTED) {
//if user have chosen a moveable piece for first time in this turn
if (!_touchedPiece)
//if piece is moveable
if (!srcState.first.empty()) {
_touchedPiece = true;
this->touchedPiece(manager->getTurn());
}
return false;
}
//___________________________change choosen piece
//if cell is full
if (manager->getChessBoardGame()->getCell(indexToIJ(index)).isFull()
&& manager->getChessBoardGame()->getCell(indexToIJ(srcIndex)).isFull()) {
//if colors are the same
if (manager->getChessBoardGame()->getCell(indexToIJ(index)).getChessPieces()->getColor()
== manager->getChessBoardGame()
->getCell(indexToIJ(srcIndex))
.getChessPieces()
->getColor()) {
_change = true;
//if user have chosen a moveable piece for first time in this turn
if (!_touchedPiece)
//if piece is moveable
if (!srcState.first.empty()) {
_touchedPiece = true;
this->touchedPiece(manager->getTurn());
}
emit unchoosen();
choose(index);
return false;
}
}
//if can't go
if (cellState(index) == UNAVAILABLE) {
return false;
}
try {
destIndex = index;
manager->checkMove(indexToIJ(srcIndex), indexToIJ(destIndex));
manager->setMove(indexToIJ(srcIndex), indexToIJ(destIndex), false, false, _extraMove);
}
catch (ImpossibleHitKing &s) {
qDebug() << s.what();
return false;
}
catch (FinalCellForPawn &s) {
qDebug() << s.what();
emit promotion();
_change = true;
previewsSrc = srcIndex;
manager->setMove(indexToIJ(srcIndex), indexToIJ(destIndex), false, false, _extraMove);
return false;
}
catch (exception &s) {
qDebug() << s.what();
return false;
}
previewsSrc = srcIndex;
// qDebug() << "moved from" << indexToIJ(srcIndex) << " to " << indexToIJ(destIndex);
if (!_extraMove)
manager->changeTurn();
else
_extraMove = false;
//Turn changed
_touchedPiece = false;
// emit unchoosen();
this->_moved = true;
emit moved();
return true;
}
//__________________________________________________________________________ undo
void BackEnd::undo()
{
auto back = manager->undo();
previewsSrc = IJToIndex(back.first);
destIndex = IJToIndex(back.second);
emit moved();
}
//__________________________________________________________________________ extraMove
bool BackEnd::extraMove()
{
if (manager->extraMovements()) {
_extraMove = true;
return true;
}
return false;
}
//__________________________________________________________________________ random Move
void BackEnd::randomMove()
{
pair<Chessman::Index, Chessman::Index> temp;
try {
temp = manager->randomMovements();
previewsSrc = (int) IJToIndex(temp.first);
destIndex = (int) IJToIndex(temp.second);
manager->changeTurn();
emit moved();
}
catch (exception &s) {
qDebug() << s.what();
}
}
//__________________________________________________________________________ promote
void BackEnd::promote(unsigned type)
{
manager->promote(indexToIJ(destIndex), (Chessman::ChessType) type);
emit moved();
manager->changeTurn();
}
//__________________________________________________________________________ game status
unsigned BackEnd::gameStatus()
{
//0->NORMAL 1->CHECKED 2->STALEMATE 3->CHECKMATE
return manager->analayzeGameStatus();
}
//__________________________________________________________________________ touched piece
void BackEnd::touchedPiece(GameManager::Turn turn)
{
if (turn == GameManager::USER1) {
manager->getUser1()->incNegativeScore(5);
} else {
manager->getUser2()->incNegativeScore(5);
}
checkRandomMove();
}