-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragonmover.cpp
126 lines (122 loc) · 3.29 KB
/
dragonmover.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
#include "dragonmover.h"
DragonMover::DragonMover(QWidget *parent, Table *t, Piece *d, QScrollArea *a, int ps, int p, int s) : table(t), dragon(d), scroll(a), players(ps), currentPlayer(p), size(s) {
moves = 0;
layout = new QVBoxLayout(); // Deleted in destructor
buttonLayout = new QGridLayout();
label = new QLabel();
north = new QPushButton("^");
east = new QPushButton(">");
south = new QPushButton("v");
west = new QPushButton("<");
this->setLayout(layout);
layout->addWidget(label);
layout->addLayout(buttonLayout);
buttonLayout->addWidget(north, 0, 1);
buttonLayout->addWidget(east, 1, 2);
buttonLayout->addWidget(south, 2, 1);
buttonLayout->addWidget(west, 1, 0);
connect(north, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(east, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(south, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(west, SIGNAL(clicked()), this, SLOT(buttonClicked()));
this->show();
update();
}
DragonMover::~DragonMover() {
delete north;
north = NULL;
delete east;
east = NULL;
delete south;
south = NULL;
delete west;
west = NULL;
delete buttonLayout;
buttonLayout = NULL;
delete label;
label = NULL;
delete layout;
layout = NULL;
}
void DragonMover::update() {
north->setEnabled(false);
east->setEnabled(false);
south->setEnabled(false);
west->setEnabled(false);
QString str("Player ");
str.append(QString::number(currentPlayer));
str.append(": Please move the dragon.");
label->setText(str);
table->getDragon(&x, &y);
dragonHistory.push_back(table->getTile(x, y));
scroll->ensureWidgetVisible(table->getLabel(x, y), size, size);
vector<Tile*> neighbors = table->getNeighbors(x, y);
bool deadEnd = true;
for (int i = 0; i < neighbors.size(); ++i) {
// Can not move dragon to place that
// a) is empty, or
// b) has the fairy, or
// c) the dragon has already visited during this set of moves
if (neighbors.at(i) == NULL || neighbors.at(i)->hasFairy() || std::find(dragonHistory.begin(), dragonHistory.end(), neighbors.at(i)) != dragonHistory.end()) {
continue;
}
deadEnd = false;
switch(i) {
case 0:
north->setEnabled(true);
break;
case 1:
east->setEnabled(true);
break;
case 2:
south->setEnabled(true);
break;
case 3:
west->setEnabled(true);
break;
default:
break;
}
}
if (deadEnd) {
QMessageBox::information(this, "Dead end", "The dragon has reached a dead end.", QMessageBox::Ok);
this->close();
}
}
void DragonMover::buttonClicked() {
QPushButton *button = (QPushButton*)QObject::sender();
int oldX = x;
int oldY = y;
if (button == north) {
--y;
}
else if (button == east) {
++x;
}
else if (button == south) {
++y;
}
else {
--x;
}
table->setDragon(x, y);
table->getTile(x, y)->getImageContainer()->setPiece(dragon);
table->getTile(x, y)->getImageContainer()->removeFollowers();
QPixmap *pxm = table->getTile(x, y)->getPixmap(size);
table->getLabel(x, y)->setPixmap(*pxm);
delete pxm;
pxm = table->getTile(oldX, oldY)->getPixmap(size);
table->getLabel(oldX, oldY)->setPixmap(*pxm);
delete pxm;
++moves;
if (moves == 6) {
QMessageBox::information(this, "Out of moves", "The dragon has moved 6 times and will now stop.", QMessageBox::Ok);
this->close();
return;
}
++currentPlayer;
if (currentPlayer > players) {
currentPlayer = 1;
}
update();
}