-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplayscreen.cpp
More file actions
328 lines (281 loc) · 9.94 KB
/
playscreen.cpp
File metadata and controls
328 lines (281 loc) · 9.94 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
//AUTHORS: Matt Dumford, Anthony Phelps
#include "playscreen.h"
#include "tile.h"
#include "win_menu.h"
#include "mainmenu.h"
#include <QLabel>
#include <QImageReader>
#include <QGridLayout>
#include <QGraphicsView>
#include <QTimer>
#include <math.h>
#include <QDebug>
// the playScreen is the screen for the single player game.
// it takes in the file path to the image the user wants to use
// and cuts it up into tiles and displays them on the screen with various
// menu buttons and statistics. It also handles tile clicks and tile swapping.
PlayScreen::PlayScreen(QString imgPath, MainWindow *mainWindow, QWidget *parent) : QWidget(parent)
{
this->imgPath = imgPath;
this->mainWindow = mainWindow;
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
}
//create all sub-widgets and display everything
void PlayScreen::display(int screenWidth, int screenHeight, int gridSize)
{
this->screenWidth = screenWidth;
this->screenHeight = screenHeight;
grid = gridSize;
numMoves = 0;
seconds = 0;
percentComplete = grid*grid;
QFont font("Helvectica", 13);
//set up scene and view
QGraphicsScene *gScene = new QGraphicsScene(this);
QGraphicsView *gView = new QGraphicsView(gScene);
gView->setFixedSize(screenWidth, screenHeight);
gScene->setBackgroundBrush(Qt::black);
//set up all GridLayouts
QGridLayout *layout = new QGridLayout(gView);
playGrid = new QGridLayout();
QGridLayout *menuGrid = new QGridLayout();
layout->setContentsMargins(0,0,0,0);
playGrid->setContentsMargins(0,0,0,0);
menuGrid->setContentsMargins(0,0,0,0);
gView->setLayout(layout);
layout->addLayout(playGrid, 0, 0);
layout->addLayout(menuGrid, 1, 0);
//timer and move labels
movesLabel = new QLabel("Moves: " + QString::number(numMoves));
timerLabel = new QLabel("Time: 0:0");
percentLabel = new QLabel("Percent: 0%");
menuGrid->addWidget(movesLabel,0,0);
menuGrid->addWidget(timerLabel,1,0);
menuGrid->addWidget(percentLabel,2,0);
movesLabel->setFont(font);
timerLabel->setFont(font);
percentLabel->setFont(font);
//menu buttons
QPushButton *winButton = new QPushButton("DEBUG WIN");
QPushButton *pauseButton = new QPushButton("Pause/Play");
QPushButton *giveUpButton = new QPushButton("Give Up");
menuGrid->addWidget(winButton, 0, 1);
menuGrid->addWidget(pauseButton, 1, 1);
menuGrid->addWidget(giveUpButton, 2, 1);
winButton->setFont(font);
pauseButton->setFont(font);
giveUpButton->setFont(font);
connect(winButton, SIGNAL(clicked()), this, SLOT(winButtonClicked()));
connect(pauseButton, SIGNAL(clicked()), this, SLOT(pauseButtonClicked()));
connect(giveUpButton, SIGNAL(clicked()), this, SLOT(giveUpButtonClicked()));
//import image
QImageReader reader(imgPath);
reader.setScaledSize(QSize(screenWidth, screenWidth));
QImage image = reader.read();
int eHeight = image.height();
int eWidth = image.width();
//cut image into tiles and position them
for(int i = 0; i < grid; i++){
for(int j = 0; j < grid; j++){
if(!(i==grid-1 && j==grid-1)){
QPixmap pixmap = QPixmap::fromImage(image.copy(i*(eWidth/grid), j*(eHeight/grid), eWidth/grid, eHeight/grid));
QIcon icon(pixmap);
Tile *button = new Tile(i, j, icon);
button->setIconSize(QSize(eWidth/grid, eHeight/grid));
playGrid->addWidget(button, j, i);
connect(button, SIGNAL(tileClicked(Tile*)), this, SLOT(handleTileClick(Tile*)));
}
}
}
//make hidden tile
reader.setFileName(":/black.png");
QIcon black(QPixmap::fromImage(reader.read()));
hiddenTile = new Tile(grid-1, grid-1, black);
hiddenTile->setIconSize(QSize(eWidth/grid, eHeight/grid));
playGrid->addWidget(hiddenTile, grid-1, grid-1);
//grid sizing
for(int i=0; i<layout->columnCount(); i++)
layout->setColumnMinimumWidth(i, screenWidth);
layout->setRowMinimumHeight(1, screenHeight-screenWidth);
for(int i=0; i<playGrid->rowCount(); i++){
playGrid->setColumnMinimumWidth(i, screenWidth/grid);
playGrid->setRowMinimumHeight(i, screenWidth/grid);
}
shuffle();
//checks if the shuffle resulted in a winning board
if(percentComplete == grid*grid)
{
playerWin();
}
//create and set up the timer
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
percentLabel->setText("Percent: " + QString::number(calculatePercent()) + "%");
gView->show();
}
//calculate the percent of the puzzle that is complete
int PlayScreen::calculatePercent()
{
if(percentComplete == 0)
{
return 0;
}
return (int)(100.00 * ((float)percentComplete/(float)(grid*grid))) + 1;
}
//shuffles the tiles into random positions
void PlayScreen::shuffle()
{
for(int i = 0; i < grid; i++)
{
for(int j = 0; j < grid; j++)
{
if(!(i == grid - 1 && j == grid - 1))
{
int a = grid - 1;
int b = grid - 1;
while(a == grid - 1 && grid - 1)
{
a = rand() % grid;
b = rand() % grid;
}
Tile *g = dynamic_cast<Tile*>(playGrid->itemAtPosition(i,j)->widget());
Tile *h = dynamic_cast<Tile*>(playGrid->itemAtPosition(a,b)->widget());
swapTiles(g,h);
}
}
}
}
//swaps the positions of two tiles
void PlayScreen::swapTiles(Tile *tile1, Tile *tile2)
{
playGrid->removeWidget(tile1);
playGrid->removeWidget(tile2);
int x1start = tile1->getX();
int y1start = tile1->getY();
int x2start = tile2->getX();
int y2start = tile2->getY();
//swap the tiles
playGrid->addWidget(tile1, y2start, x2start);
playGrid->addWidget(tile2, y1start, x1start);
tile1->setX(x2start);
tile1->setY(y2start);
tile2->setX(x1start);
tile2->setY(y1start);
//update the stats for tiles in the correct positions
if(x1start == tile1->getInitX() && y1start == tile1->getInitY())
{
percentComplete--;
}
if(x2start == tile1->getInitX() && y2start == tile1->getInitY())
{
percentComplete++;
}
if(x2start == tile2->getInitX() && y2start == tile2->getInitY())
{
percentComplete--;
}
if(x1start == tile2->getInitX() && y1start == tile2->getInitY())
{
percentComplete++;
}
//checks if the player won
if(calculatePercent() >= 100)
playerWin();
}
//update the timer
void PlayScreen::update()
{
int minutes = (++seconds) / 60;
timerLabel->setText("Time: " + QString::number(minutes) + ":" + QString::number(seconds-(minutes*60)));
}
//checks if the tile is next to the blank tile and swaps them if it is.
void PlayScreen::handleTileClick(Tile* t)
{
if(timer->isActive())
{
if((t->getX()-1 == hiddenTile->getX() && t->getY() == hiddenTile->getY()) || (t->getX() == hiddenTile->getX() && t->getY()-1 == hiddenTile->getY()) || (t->getX()+1 == hiddenTile->getX() && t->getY() == hiddenTile->getY()) || (t->getX() == hiddenTile->getX() && t->getY()+1 == hiddenTile->getY()))
{
//qDebug("BEFORE :: T:(%d, %d) H:(%d,%d)", t->getX(), t->getY(), hiddenTile->getX(), hiddenTile->getY());
swapTiles(t, hiddenTile);
//qDebug("AFTER :: T:(%d, %d) H:(%d,%d)", t->getX(), t->getY(), hiddenTile->getX(), hiddenTile->getY());
movesLabel->setText("Moves: " + QString::number(++numMoves));
percentLabel->setText("Percent: " + QString::number(calculatePercent()) + "%");
qDebug() << (int)(10000 - ((log(seconds)-log(1))/(log(1000)-log(1)) + (log(numMoves)-log(1))/(log(1000)-log(1)))*1000);
}
}
}
//DEBUG PURPOSES ONLY! displays the win menu so we don't have to solve the puzzle in our presentation.
void PlayScreen::winButtonClicked()
{
qDebug() << "main menu button clicked.";
makeCon();
win_menu *wm = new win_menu(mainWindow, mainWindow);
wm->display(screenWidth, screenHeight);
}
//pauses the timer and prevents the user from moving tiles
void PlayScreen::pauseButtonClicked()
{
qDebug() << "pause button clicked.";
if(timer->isActive())
{
timer->stop();
}
else
{
timer->start();
}
}
//takes the user back to the main menu
void PlayScreen::giveUpButtonClicked()
{
MainMenu *mm = new MainMenu(mainWindow, mainWindow);
mm->display(screenWidth, screenHeight);
mm->raise();
}
//called if the winning conditions are met. creates a win menu
void PlayScreen::playerWin()
{
qDebug() << "player won.";
makeCon();
win_menu *wm = new win_menu(mainWindow, mainWindow);
wm->display(screenWidth, screenHeight);
}
void PlayScreen::makeCon()
{
qDebug() << "Connecting...";
socket->connectToHost("ganymede.sytes.net", 4848);
if(!socket->waitForConnected(1000))
{
qDebug() << "ERROR: " << socket->errorString();
}
}
//uploading high scores
void PlayScreen::connected()
{
qDebug() << "Connected";
socket->write(QString("score:new:" + mainWindow->getUserName() + ":" + QString::number((int)(10000 - ((log(seconds)-log(1))/(log(1000)-log(1)) + (log(numMoves)-log(1))/(log(1000)-log(1)))*1000)) + ":" + "\n").toUtf8());
}
void PlayScreen::disconnected()
{
qDebug() << "Disconnected";
}
void PlayScreen::bytesWritten(qint64 bytes)
{
qDebug() << "Wrote Something: " << bytes << "bytes";
}
void PlayScreen::readyRead()
{
qDebug() << "Reading...";
parseResponse(socket->readLine(1024));
}
QString PlayScreen::parseResponse(QString s)
{
QStringList parts = s.split(":");
QString result;
return result;
}