-
Notifications
You must be signed in to change notification settings - Fork 3
/
board.cpp
938 lines (809 loc) · 32.9 KB
/
board.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/**
* \file board.cpp
*
* \section LICENSE
*
* Copyright (C) 2015-present Thorsten Roth
*
* This file is part of StackAndConquer.
*
* StackAndConquer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StackAndConquer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with StackAndConquer. If not, see <https://www.gnu.org/licenses/>.
*
* \section DESCRIPTION
* Game board generation.
*/
#include "./board.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSvgItem>
#include <QInputDialog>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QSvgRenderer>
#include <QTimer>
#include "./settings.h"
Board::Board(QWidget *pParent, const quint8 nMaxTower, quint8 NumOfPlayers,
const QString &sIN, const QString &sOUT, Settings *pSettings,
QObject *pParentObj)
: m_pParent(pParent),
m_sIN(sIN),
m_sOUT(sOUT),
m_sPAD(QStringLiteral("-")),
m_pSettings(pSettings),
m_nGridSize(m_pSettings->getGridSize()),
m_nScale(m_pSettings->getGridSize() / m_pSettings->getDefaultGrid()),
m_nMaxPlayerStones(0),
m_nMaxTower(nMaxTower),
m_NumOfPlayers(NumOfPlayers) {
Q_UNUSED(pParentObj)
this->setBackgroundBrush(QBrush(m_pSettings->getBgColor()));
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::createBoard(const QString &sBoard) -> bool {
QList<QString> tmpBoard;
if (!this->loadBoard(sBoard, tmpBoard)) {
return false;
}
this->addBoardPadding(tmpBoard, m_nMaxTower);
this->drawBoard(tmpBoard);
this->createHighlighters();
if (!this->createStones()) {
return false;
}
/*
* Moving directions factor
* E.g. 5x5 board, max tower height 5 (padding):
* -16 -15 -14
* -1 X 1
* 14 15 16
*/
m_DIRS << -(2 * m_nMaxTower + m_BoardDimensions.x() + 1); // -16
m_DIRS << -(2 * m_nMaxTower + m_BoardDimensions.x()); // -15
m_DIRS << -(2 * m_nMaxTower + m_BoardDimensions.x() - 1); // -14
m_DIRS << -1 << 1; // -1, 1
m_DIRS << -(m_DIRS.at(2)); // 14
m_DIRS << -(m_DIRS.at(1)); // 15
m_DIRS << -(m_DIRS.at(0)); // 16
m_FieldStones.clear();
m_FieldStones.reserve(m_jsBoard.size());
QList<QGraphicsSvgItem *> tmpTower;
for (int i = 0; i < m_jsBoard.size(); i++) {
m_FieldStones.append(tmpTower);
}
return true;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::loadBoard(const QString &sBoard, QList<QString> &tmpBoard) -> bool {
QFile fBoard(sBoard);
if (!fBoard.exists()) {
qWarning() << "Board cannot be loaded:" << sBoard;
QMessageBox::critical(m_pParent, tr("Warning"),
tr("Error while opening board file!"));
return false;
}
if (!fBoard.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't open open board file:" << sBoard;
QMessageBox::critical(m_pParent, tr("Warning"),
tr("Error while opening board file!"));
return false;
}
QByteArray boardData = fBoard.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(boardData));
QJsonObject jso = loadDoc.object();
if (loadDoc.isEmpty() || jso.value(QStringLiteral("Board")).isUndefined() ||
!jso.value(QStringLiteral("Board")).isArray() ||
jso.value(QStringLiteral("Columns")).isUndefined() ||
!jso.value(QStringLiteral("Columns")).isDouble() ||
jso.value(QStringLiteral("Rows")).isUndefined() ||
!jso.value(QStringLiteral("Rows")).isDouble() ||
jso.value(QStringLiteral("PlayersStones")).isUndefined() ||
!jso.value(QStringLiteral("PlayersStones")).isArray()) {
qWarning() << "Board file doesn't contain all required keys:" << sBoard;
qWarning() << "Found json keys:" << jso.keys();
QMessageBox::critical(m_pParent, tr("Warning"),
tr("Error while opening board file!"));
return false;
}
m_BoardDimensions.setX(jso.value(QStringLiteral("Columns")).toInt());
m_BoardDimensions.setY(jso.value(QStringLiteral("Rows")).toInt());
if (0 == m_BoardDimensions.x() || 0 == m_BoardDimensions.y()) {
qWarning() << "Board file contains invalid dimensions:"
<< m_BoardDimensions;
qWarning() << "Board:" << sBoard;
QMessageBox::critical(m_pParent, tr("Warning"),
tr("Error while opening board file!"));
return false;
}
const QJsonArray stones(jso.value(QStringLiteral("PlayersStones")).toArray());
if (stones.size() < m_NumOfPlayers) {
// Use last value as fallback, if number of stones
// not specified for number of players
m_nMaxPlayerStones = stones.last().toInt();
qWarning() << "Player stones not specified for" << m_NumOfPlayers
<< "players. Using fallback:" << m_nMaxPlayerStones;
} else {
m_nMaxPlayerStones = stones.at(m_NumOfPlayers - 2).toInt();
}
if (0 == m_nMaxPlayerStones) {
qWarning() << "Number of player stone is 0!";
QMessageBox::critical(m_pParent, tr("Warning"),
tr("Error while opening board file!"));
return false;
}
tmpBoard.clear();
const QJsonArray jsBoard(jso.value(QStringLiteral("Board")).toArray());
for (const auto &js : jsBoard) {
QString s = js.toString();
if (js.isNull() || s.isEmpty() ||
(m_sOUT != s && m_sIN != s && m_sPAD != s)) {
qWarning() << "Board array contains invalid data:" << s;
qWarning() << "Board:" << sBoard;
QMessageBox::critical(m_pParent, tr("Warning"),
tr("Error while opening board file!"));
return false;
}
tmpBoard << s;
}
qDebug() << "Board file:" << sBoard;
qDebug() << "Board dimensions:" << m_BoardDimensions.x() << "columns x"
<< m_BoardDimensions.y() << "rows";
return true;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::addBoardPadding(const QList<QString> &tmpBoard,
const quint8 nMaxTower) {
// Generate field array
// Top padding
for (int i = 0; i < nMaxTower; i++) {
for (int j = 0; j < (nMaxTower * 2 + m_BoardDimensions.x()); j++) {
m_jsBoard << m_sPAD;
}
}
// Padding left and right per line
for (int i = 0; i < tmpBoard.size(); i++) {
if (0 == i) { // First item of first line, padding left
for (int j = 0; j < nMaxTower; j++) {
m_jsBoard << m_sPAD;
}
} else if (0 == i % m_BoardDimensions.x()) { // First item in row:
for (int j = 0; j < (nMaxTower * 2);
j++) { // Add padding end of previous
m_jsBoard << m_sPAD; // & beginning of current line
}
}
if (m_sIN == tmpBoard[i]) {
m_jsBoard << QString();
} else {
m_jsBoard << m_sOUT;
}
if (tmpBoard.size() - 1 == i) { // Last item of last line, padding right
for (int j = 0; j < nMaxTower; j++) {
m_jsBoard << m_sPAD;
}
}
}
// Bottom padding
for (int i = 0; i < nMaxTower; i++) {
for (int j = 0; j < (nMaxTower * 2 + m_BoardDimensions.x()); j++) {
m_jsBoard << m_sPAD;
}
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::drawBoard(const QList<QString> &tmpBoard) {
int x(0);
int y(-m_nGridSize);
quint8 col(65);
quint8 row(0);
for (int i = 0; i < tmpBoard.size(); i++) {
// m_BoardDimensions.x() = columns, m_BoardDimensions.y() = rows
if (0 == i % m_BoardDimensions.x()) { // First item in row
x = 0;
col = 65;
y += m_nGridSize;
row++;
} else {
x += m_nGridSize;
col++;
}
if (m_sOUT == tmpBoard[i]) {
m_listFields << new QGraphicsRectItem();
// Rect is not visible anyway, but isVisible property is used during zoom
m_listFields.last()->setVisible(false);
continue;
}
m_listFields << new QGraphicsRectItem(x, y, m_nGridSize, m_nGridSize);
m_listFields.last()->setBrush(QBrush(m_pSettings->getBgBoardColor()));
m_listFields.last()->setPen(QPen(m_pSettings->getGridBoardColor()));
m_boardPath.addRect(m_listFields.last()->rect());
this->addItem(m_listFields.last());
// Field captions for debugging
if (qApp->arguments().contains(QStringLiteral("--debug"))) {
m_Captions << this->addSimpleText(QString(static_cast<char>(col)) +
QString::number(row));
m_Captions.last()->setPos(x + 2, y + m_nGridSize - m_nGridSize / 3);
m_Captions.last()->setFont(
QFont(QStringLiteral("Arial"), m_nGridSize / 5));
}
}
// TODO(x): Might need adjustments for non rectangular shapes!
this->setSceneRect(m_boardPath.boundingRect());
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::createHighlighters() {
// Field highlighter
m_pHighlightRect = new QGraphicsRectItem(0, 0, m_nGridSize, m_nGridSize);
m_pHighlightRect->setBrush(QBrush(m_pSettings->getHighlightColor()));
m_pHighlightRect->setPen(QPen(m_pSettings->getHighlightBorderColor()));
m_pHighlightRect->setVisible(false);
m_pHighlightRect->setZValue(0);
this->addItem(m_pHighlightRect);
// Selected field
m_pSelectedField = new QGraphicsRectItem(0, 0, m_nGridSize, m_nGridSize);
m_pSelectedField->setBrush(QBrush(m_pSettings->getSelectedColor()));
m_pSelectedField->setPen(QPen(m_pSettings->getSelectedBorderColor()));
m_pSelectedField->setVisible(false);
m_pSelectedField->setZValue(0);
this->addItem(m_pSelectedField);
// Animation
m_pAnimateField = new QGraphicsRectItem(0, 0, m_nGridSize, m_nGridSize);
m_pAnimateField->setBrush(QBrush(m_pSettings->getAnimateColor()));
m_pAnimateField->setPen(QPen(m_pSettings->getAnimateBorderColor()));
m_pAnimateField->setVisible(false);
m_pAnimateField->setZValue(0);
this->addItem(m_pAnimateField);
// Animation2
m_pAnimateField2 = new QGraphicsRectItem(0, 0, m_nGridSize, m_nGridSize);
m_pAnimateField2->setBrush(QBrush(m_pSettings->getAnimateColor()));
m_pAnimateField2->setPen(QPen(m_pSettings->getAnimateBorderColor()));
m_pAnimateField2->setVisible(false);
m_pAnimateField2->setZValue(0);
this->addItem(m_pAnimateField2);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::createStones() -> bool {
// Load svg as txt for below color exchange.
QFile fStone(QStringLiteral(":/img/stone.svg"));
if (!fStone.open(QFile::ReadOnly | QFile::Text)) {
qDebug() << "Could not open stone.svg";
QMessageBox::critical(
m_pParent, tr("Warning"),
tr("Could not open %1!").arg(QStringLiteral("stone.svg")));
return false;
}
QTextStream in(&fStone);
QString sSvg = in.readAll();
fStone.close();
QList<QGraphicsSvgItem *> tmpSvgList;
// Create a few more than maximum of stones because of wrong
// order during move tower add/remove
for (int nPlayers = 0; nPlayers < m_NumOfPlayers; nPlayers++) {
// stone.svg HAS to be filled with #ff0000, so that below replace can work.
QString sTmpSvg = sSvg;
QByteArray aSvg(sTmpSvg
.replace(QLatin1String("#ff0000"),
m_pSettings->getPlayerColor(nPlayers))
.toUtf8());
auto *pSvgRenderer = new QSvgRenderer(aSvg);
tmpSvgList.clear();
for (int i = 0; i < m_nMaxPlayerStones + 4; i++) {
tmpSvgList.append(new QGraphicsSvgItem());
tmpSvgList.last()->setScale(m_nScale);
tmpSvgList.last()->setSharedRenderer(pSvgRenderer);
this->addItem(tmpSvgList.last());
tmpSvgList.last()->setPos(0, 0);
tmpSvgList.last()->setZValue(5);
tmpSvgList.last()->setVisible(false);
}
m_listPlayerStones << tmpSvgList;
}
return true;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::changeZoom() {
m_nGridSize = m_pSettings->getGridSize();
m_nScale = m_pSettings->getGridSize() / m_pSettings->getDefaultGrid();
qDebug() << "Zoom - new grid size:" << m_nGridSize;
int x(0);
int y(-m_nGridSize);
quint8 col(65);
quint8 row(0);
for (int c = 0; c < m_Captions.size(); c++) {
this->removeItem(m_Captions[c]);
delete m_Captions[c];
m_Captions[c] = nullptr;
}
m_Captions.clear();
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
m_boardPath.clear();
#else
m_boardPath = QPainterPath();
#endif
// Reposition and rescale board fields
for (int i = 0; i < m_listFields.size(); i++) {
// m_BoardDimensions.x() = columns, m_BoardDimensions.y() = rows
if (0 == i % m_BoardDimensions.x()) { // First item in row
x = 0;
col = 65;
y += m_nGridSize;
row++;
} else {
x += m_nGridSize;
col++;
}
if (false == m_listFields.at(i)->isVisible()) { // Is outside
continue;
}
m_listFields[i]->setRect(x, y, m_nGridSize, m_nGridSize);
m_boardPath.addRect(m_listFields[i]->rect());
// Field captions for debugging
if (qApp->arguments().contains(QStringLiteral("--debug"))) {
m_Captions << this->addSimpleText(QString(static_cast<char>(col)) +
QString::number(row));
m_Captions.last()->setPos(x + 2, y + m_nGridSize - m_nGridSize / 3);
m_Captions.last()->setFont(
QFont(QStringLiteral("Arial"), m_nGridSize / 5));
}
}
// Rescale highlighters
m_pHighlightRect->setRect(0, 0, m_nGridSize, m_nGridSize);
m_pSelectedField->setRect(0, 0, m_nGridSize, m_nGridSize);
m_pAnimateField->setRect(0, 0, m_nGridSize, m_nGridSize);
m_pAnimateField2->setRect(0, 0, m_nGridSize, m_nGridSize);
// Not yet placed player stones
for (int nPlayers = 0; nPlayers < m_NumOfPlayers; nPlayers++) {
for (int k = 0; k < m_listPlayerStones[nPlayers].size(); k++) {
m_listPlayerStones[nPlayers][k]->setScale(m_nScale);
}
}
// Stones on board
for (int nIndex = 0; nIndex < m_FieldStones.size(); nIndex++) {
for (int nNum = 0; nNum < m_FieldStones.at(nIndex).size(); nNum++) {
m_FieldStones[nIndex][nNum]->setScale(m_nScale);
// Adjust position
QRectF rect(m_FieldStones[nIndex][nNum]->boundingRect());
m_FieldStones[nIndex][nNum]->setPos(this->getCoordinateFromIndex(nIndex) *
m_nGridSize);
m_FieldStones[nIndex][nNum]->setPos(
m_FieldStones[nIndex][nNum]->x() -
(rect.width() / 4 * m_nScale - 3 * m_nScale) * nNum,
m_FieldStones[nIndex][nNum]->y() -
(rect.height() / 4 * m_nScale - 3 * m_nScale) * nNum);
}
}
// Redraw board
this->update(QRectF(0, 0, m_BoardDimensions.x() * m_nGridSize - 1,
m_BoardDimensions.y() * m_nGridSize - 1));
// TODO(x): Might need adjustments for non rectangular shapes!
this->setSceneRect(m_boardPath.boundingRect());
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::setupSavegame(const QJsonArray &jsBoard) -> bool {
if (jsBoard.size() != m_jsBoard.size()) {
qWarning() << "jsBoard.size() != m_jsBoard.size()";
return false;
}
for (int i = 0; i < jsBoard.size(); i++) {
const QString s = jsBoard.at(i).toString();
if (!s.isEmpty() && m_sPAD != s && m_sOUT != s) {
for (const auto &ch : s) {
if (-1 != ch.digitValue() && m_sPAD != m_jsBoard.at(i).toString() &&
m_sOUT != m_jsBoard.at(i).toString()) {
this->addStone(i, ch.digitValue());
} else {
qWarning() << "Save game data invalid stone at index" << i;
qWarning() << "Character:" << ch;
return false;
}
}
} else if (s.isEmpty()) {
m_jsBoard[i] = "";
} else { // Should be Padding or Out
if (jsBoard.at(i) != m_jsBoard.at(i)) {
qWarning() << "Save game data != board array at index" << i;
qWarning() << "Save game:" << jsBoard.at(i)
<< "- board:" << m_jsBoard.at(i);
return false;
}
}
}
// Redraw board
this->update(QRectF(0, 0, m_BoardDimensions.x() * m_nGridSize - 1,
m_BoardDimensions.y() * m_nGridSize - 1));
return true;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::mousePressEvent(QGraphicsSceneMouseEvent *p_Event) {
if (m_boardPath.contains(p_Event->scenePos())) {
const QList<QGraphicsItem *> items = this->items(p_Event->scenePos());
QJsonArray move;
for (const auto &item : items) {
int field =
m_listFields.indexOf(qgraphicsitem_cast<QGraphicsRectItem *>(item));
if (field > -1) {
/*
qDebug() << "Clicked field:" << field;
qDebug() << "Board index: " << getIndexFromField(field);
qDebug() << "Coordinate: " << getCoordinateFromField(field);
qDebug() << "String coord.:" << getStringCoordFromField(field);
*/
// If debug enabled, use Ctrl + right mouse button to set stone anywhere
if (Qt::RightButton == p_Event->button() &&
Qt::ControlModifier == p_Event->modifiers() &&
qApp->arguments().contains(QStringLiteral("--debug"))) {
this->selectIndexField(-1);
qDebug() << "Following stone set in DEBUG mode:";
move << -999 << 1 << getIndexFromField(field);
emit actionPlayer(move);
break;
}
// Place tower, if field is empty
if (m_jsBoard.at(getIndexFromField(field)).toString().isEmpty()) {
this->selectIndexField(-1);
move << -1 << 1 << getIndexFromField(field);
emit actionPlayer(move);
} else { // Otherwise select / move tower
this->selectIndexField(getIndexFromField(field));
}
break;
}
}
}
QGraphicsScene::mousePressEvent(p_Event);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::mouseMoveEvent(QGraphicsSceneMouseEvent *p_Event) {
static QPointF point;
if (m_boardPath.contains(p_Event->scenePos())) {
m_pHighlightRect->setVisible(true);
point = p_Event->scenePos();
point = QPointF(point.x() - m_nGridSize / 2, point.y() - m_nGridSize / 2);
m_pHighlightRect->setPos(this->snapToGrid(point));
} else {
m_pHighlightRect->setVisible(false);
}
QGraphicsScene::mouseMoveEvent(p_Event);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::snapToGrid(const QPointF point) const -> QPointF {
return QPointF(qRound(point.x() / m_nGridSize) * m_nGridSize,
qRound(point.y() / m_nGridSize) * m_nGridSize);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::getIndexFromField(const int nField) const -> int {
const int nTop = m_nMaxTower * (2 * m_nMaxTower + m_BoardDimensions.x());
const int nFirst = nTop + m_nMaxTower;
int nLeftRight = 2 * m_nMaxTower * (nField / m_BoardDimensions.x());
int nCol = nField % m_BoardDimensions.x();
int nRow = (nField / m_BoardDimensions.x()) * m_BoardDimensions.x();
return nFirst + nLeftRight + nCol + nRow;
}
auto Board::getFieldFromIndex(const int nIndex) const -> int {
const int nTop = m_nMaxTower * (2 * m_nMaxTower + m_BoardDimensions.x());
const int nFirst = nTop + m_nMaxTower;
int nLeftRight =
2 * m_nMaxTower *
((nIndex / (m_BoardDimensions.x() + 2 * m_nMaxTower)) - m_nMaxTower);
return nIndex - nFirst - nLeftRight;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::getCoordinateFromField(const int nField) const -> QPoint {
int x = nField % m_BoardDimensions.x();
int y = nField / m_BoardDimensions.x();
return QPoint(x, y);
}
auto Board::getStringCoordFromField(const int nField) const -> QString {
QPoint point(this->getCoordinateFromField(nField));
return QString(static_cast<char>(point.x() + 65) +
QString::number(point.y() + 1));
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::getCoordinateFromIndex(const int nIndex) const -> QPoint {
return this->getCoordinateFromField(this->getFieldFromIndex(nIndex));
}
auto Board::getStringCoordFromIndex(const int nIndex) const -> QString {
return this->getStringCoordFromField(this->getFieldFromIndex(nIndex));
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::addStone(const int nIndex, const quint8 nStone, const bool bAnim) {
auto nExisting(static_cast<quint8>(m_jsBoard.at(nIndex).toString().size()));
if (nStone < 1 || nStone > m_NumOfPlayers) {
qWarning() << "Trying to set invalid stone type" << nStone;
QMessageBox::warning(m_pParent, tr("Warning"), tr("Something went wrong!"));
return;
}
m_jsBoard[nIndex] = m_jsBoard.at(nIndex).toString() + QString::number(nStone);
m_FieldStones[nIndex].append(m_listPlayerStones.at(nStone - 1).last());
m_listPlayerStones[nStone - 1].removeLast();
if (bAnim) {
this->startAnimation(this->getCoordinateFromIndex(nIndex));
}
QRectF rect(m_FieldStones[nIndex].last()->boundingRect());
m_FieldStones[nIndex].last()->setPos(this->getCoordinateFromIndex(nIndex) *
m_nGridSize);
m_FieldStones[nIndex].last()->setPos(
m_FieldStones[nIndex].last()->x() -
(rect.width() / 4 * m_nScale - 3 * m_nScale) * nExisting,
m_FieldStones[nIndex].last()->y() -
(rect.height() / 4 * m_nScale - 3 * m_nScale) * nExisting);
m_FieldStones[nIndex].last()->setVisible(true);
for (int z = 0; z < m_FieldStones.at(nIndex).size(); z++) {
m_FieldStones[nIndex][z]->setZValue(6 + z);
}
if (bAnim) {
// Redraw board
this->update(QRectF(0, 0, m_BoardDimensions.x() * m_nGridSize - 1,
m_BoardDimensions.y() * m_nGridSize - 1));
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::startAnimation(const QPoint field) {
m_pAnimateField->setPos(this->snapToGrid(field * m_nGridSize));
m_pAnimateField->setVisible(true);
m_pHighlightRect->setVisible(false);
QTimer::singleShot(500, this, &Board::resetAnimation);
}
void Board::resetAnimation() { m_pAnimateField->setVisible(false); }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::startAnimation2(const QPoint field) {
m_pAnimateField2->setPos(this->snapToGrid(field * m_nGridSize));
m_pAnimateField2->setVisible(true);
m_pHighlightRect->setVisible(false);
QTimer::singleShot(500, this, &Board::resetAnimation2);
}
void Board::resetAnimation2() { m_pAnimateField2->setVisible(false); }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::removeStone(const int nIndex, const bool bAll) {
if (m_jsBoard.at(nIndex).toString().isEmpty()) {
qWarning() << "Trying to remove stone from empty field" << nIndex;
QMessageBox::warning(m_pParent, tr("Warning"), tr("Something went wrong!"));
return;
}
if (bAll) { // Remove all (tower conquered)
const QString s(this->getField(nIndex));
for (const auto &ch : s) {
// For starts at the beginning of the list
m_listPlayerStones[ch.digitValue() - 1].append(
m_FieldStones[nIndex].first());
m_FieldStones[nIndex].first()->setVisible(false);
m_FieldStones[nIndex].removeFirst();
}
m_jsBoard[nIndex] = QString();
} else { // Remove only one
int nPlayer(
QString(QStringView{m_jsBoard.at(nIndex).toString()}.last()).toInt());
m_listPlayerStones[nPlayer - 1].append(m_FieldStones[nIndex].last());
m_FieldStones[nIndex].last()->setVisible(false);
m_FieldStones[nIndex].removeLast();
QString s(m_jsBoard.at(nIndex).toString());
s.chop(1);
m_jsBoard[nIndex] = s;
}
// Redraw board
this->update(QRectF(0, 0, m_BoardDimensions.x() * m_nGridSize - 1,
m_BoardDimensions.y() * m_nGridSize - 1));
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::getField(const int index) const -> QString {
return m_jsBoard.at(index).toString();
}
auto Board::getBoard() const -> QJsonArray { return m_jsBoard; }
auto Board::getBoardDimensions() const -> QPoint { return m_BoardDimensions; }
auto Board::getMaxPlayerStones() const -> quint8 { return m_nMaxPlayerStones; }
auto Board::getOut() const -> QString { return m_sOUT; }
auto Board::getPad() const -> QString { return m_sPAD; }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::selectIndexField(const int nIndex) {
static int currentIndex(-1);
QList<int> neighbours;
if (-1 == nIndex) {
currentIndex = -1;
m_pSelectedField->setVisible(false);
this->highlightNeighbourhood(neighbours);
// qDebug() << "Deselected";
return;
}
if (currentIndex == nIndex || m_jsBoard.at(nIndex).toString().isEmpty()) {
currentIndex = -1;
m_pSelectedField->setVisible(false);
this->highlightNeighbourhood(neighbours);
// qDebug() << "Deselected";
return;
}
QJsonArray move;
neighbours = this->checkNeighbourhood(currentIndex);
if (neighbours.contains(nIndex) && m_pSelectedField->isVisible()) { // Move
neighbours.clear();
this->highlightNeighbourhood(neighbours);
m_pSelectedField->setVisible(false);
this->startAnimation2(this->getCoordinateFromIndex(nIndex));
int nStonesToMove = 1;
if (m_jsBoard.at(nIndex).toString().length() > 1) {
bool ok;
nStonesToMove = QInputDialog::getInt(
m_pParent, tr("Move tower"), tr("How many stones shall be moved:"), 1,
1, m_jsBoard.at(nIndex).toString().length(), 1, &ok);
if (!ok) {
return;
}
}
move << nIndex << nStonesToMove << currentIndex;
emit actionPlayer(move);
currentIndex = -1;
} else { // Select
currentIndex = nIndex;
m_pSelectedField->setVisible(true);
m_pSelectedField->setRect(
m_listFields.at(this->getFieldFromIndex(nIndex))->rect());
if (m_pSettings->getShowPossibleMoveTowers()) {
this->highlightNeighbourhood(this->checkNeighbourhood(currentIndex));
}
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::checkNeighbourhood(const int nIndex) const -> QList<int> {
QList<int> neighbours;
if (-1 == nIndex) {
return neighbours;
}
auto nMoves = static_cast<quint8>(m_jsBoard.at(nIndex).toString().size());
// qDebug() << "Sel:" << getStringCoordFromIndex(nIndex) << " Mov:" << nMoves;
QString sField;
for (int dir = 0; dir < m_DIRS.size(); dir++) {
for (int range = 1; range <= nMoves; range++) {
sField = m_jsBoard.at(nIndex + m_DIRS.at(dir) * range).toString();
if (!sField.isEmpty() && range < nMoves) {
// qDebug() << "Route blocked";
break;
}
if (0 != sField.toInt() && range == nMoves) {
neighbours.append(nIndex + m_DIRS.at(dir) * range);
}
}
}
// qDebug() << "Neighbours which can be moved:" << neighbours;
return neighbours;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::highlightNeighbourhood(const QList<int> &neighbours) {
static QList<QGraphicsRectItem *> listPossibleMoves;
for (auto *rect : listPossibleMoves) {
delete rect;
}
listPossibleMoves.clear();
listPossibleMoves.reserve(neighbours.size());
for (auto nIndex : neighbours) {
QPoint point(this->getCoordinateFromIndex(nIndex));
listPossibleMoves << new QGraphicsRectItem(point.x() * m_nGridSize,
point.y() * m_nGridSize,
m_nGridSize, m_nGridSize);
listPossibleMoves.last()->setBrush(
QBrush(m_pSettings->getNeighboursColor()));
listPossibleMoves.last()->setPen(
QPen(m_pSettings->getNeighboursBorderColor()));
listPossibleMoves.last()->setVisible(true);
this->addItem(listPossibleMoves.last());
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Board::getLegalMoves(const QString &sID, const bool bStonesLeft,
const QList<int> &lastMove) const -> QJsonDocument {
QVariantList varMove;
QJsonArray move;
QJsonArray jsMoves;
int nField = -1;
int nTo;
QString s;
for (int nRow = 0; nRow < m_BoardDimensions.y(); nRow++) {
for (int nCol = 0; nCol < m_BoardDimensions.x(); nCol++) {
nField++;
nTo = this->getIndexFromField(nField);
s = m_jsBoard.at(nTo).toString();
if (m_sOUT != s && m_sPAD != s) {
if (s.isEmpty() && bStonesLeft) { // Set stone on empty field
varMove.clear();
varMove << -1 << 1 << nTo;
move = QJsonArray::fromVariantList(varMove);
jsMoves.append(move);
continue;
}
const QList<int> neighbours = this->checkNeighbourhood(nTo);
if (!neighbours.isEmpty()) { // Possible tower moves
for (const auto nFrom : neighbours) {
const QString sTower = m_jsBoard.at(nFrom).toString();
for (int nStones = 1;
nStones <= m_jsBoard.at(nFrom).toString().size(); nStones++) {
// Skip suicide moves (opponent wins) for > 2 players
if (m_NumOfPlayers > 2) {
if ((m_jsBoard.at(nTo).toString().size() + nStones >=
m_nMaxTower) &&
QString(QStringView{sTower}.last()) != sID) {
continue;
}
}
varMove.clear();
varMove << nFrom << nStones << nTo;
move = QJsonArray::fromVariantList(varMove);
// Previous move reverted?
if (lastMove.size() == 3) {
if (!(nFrom == lastMove[2] && // From = previous to
nStones == lastMove[1] && // Same number of stones moved
nTo == lastMove[0])) { // To = previous from
jsMoves.append(move);
}
} else {
jsMoves.append(move);
}
}
}
}
}
}
}
QJsonDocument legalMoves(jsMoves);
if (jsMoves.isEmpty()) {
return QJsonDocument();
}
return legalMoves;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Board::printDebugFields() const {
qDebug() << "BOARD:";
QString sLine;
int nField = -1;
QString s;
for (int nRow = 0; nRow < m_BoardDimensions.y(); nRow++) {
sLine.clear();
for (int nCol = 0; nCol < m_BoardDimensions.x(); nCol++) {
nField++;
s = "(" + m_jsBoard.at(this->getIndexFromField(nField)).toString() + ")";
if (QString("(" + m_sOUT + ")") == s) {
s = m_sOUT + m_sOUT;
}
sLine += s;
if (nCol < m_BoardDimensions.x() - 1) {
sLine += QLatin1String(" ");
}
}
qDebug() << sLine;
}
}