forked from CS340/PuzzleApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
executable file
·219 lines (185 loc) · 6.42 KB
/
mainwindow.cpp
File metadata and controls
executable file
·219 lines (185 loc) · 6.42 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tile.h"
#include "timerthread.h"
#include <QLabel>
#include <QImageReader>
#include <QGridLayout>
#include <QGraphicsView>
#include <QTimer>
#include <math.h>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::display(int screenWidth, int screenHeight)
{
int grid = 5;
numMoves = 0;
seconds = 0;
//set up scene and view
QGraphicsScene *gScene = new QGraphicsScene(this);
gView = new QGraphicsView(gScene);
gView->setFixedSize(screenWidth, screenHeight);
gScene->setBackgroundBrush(Qt::black);
//set up all GridLayouts
layout = new QGridLayout(gView);
playGrid = new 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("Number of moves: " + QString::number(numMoves));
menuGrid->addWidget(movesLabel,0,0);
timerLabel = new QLabel("Duration of game(m:s): 0:0");
menuGrid->addWidget(timerLabel,1,0);
//import image
QImageReader reader(":/elephant.gif");
reader.setScaledSize(QSize(screenWidth, screenWidth));
QImage elephant = reader.read();
int eHeight = elephant.height();
int eWidth = elephant.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(elephant.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(grid);
qDebug("Shuffled");
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
//screen switch testing//
test = new Tile("Test");
menuGrid->addWidget(test, 2, 0);
connect(test, SIGNAL(tileClicked(Tile*)), this, SLOT(handleTileClick(Tile*)));
gView->show();
}
void MainWindow::shuffle(int grid)
{
for(int i = 0; i < grid - 1; i++)
{
for(int j = 0; j < grid - 1; 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);
}
}
}
}
void MainWindow::swapTiles(Tile *tile1, Tile *tile2){
playGrid->removeWidget(tile1);
playGrid->removeWidget(tile2);
playGrid->addWidget(tile1, tile2->getY(), tile2->getX());
playGrid->addWidget(tile2, tile1->getY(), tile1->getX());
int tmpx = tile1->getX();
int tmpy = tile1->getY();
tile1->setX(tile2->getX());
tile1->setY(tile2->getY());
tile2->setX(tmpx);
tile2->setY(tmpy);
}
void MainWindow::update()
{
int minutes = (++seconds) / 60;
timerLabel->setText("Duration of game(m:s): " + QString::number(minutes) + ":" + QString::number(seconds-(minutes*60)));
}
void MainWindow::handleTileClick(Tile* t)
{
//TEST STUFF//
if(t==test)
{
//switch screens
}
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("Number of moves: " + QString::number(++numMoves));
qDebug() << (int)(10000 - ((log(seconds)-log(1))/(log(1000)-log(1)) + (log(numMoves)-log(1))/(log(1000)-log(1)))*1000);
}
}
void MainWindow::setOrientation(ScreenOrientation orientation)
{
Qt::WidgetAttribute attribute;
switch (orientation) {
#if QT_VERSION < 0x040702
// Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
case ScreenOrientationLockPortrait:
attribute = static_cast<Qt::WidgetAttribute>(128);
break;
case ScreenOrientationLockLandscape:
attribute = static_cast<Qt::WidgetAttribute>(129);
break;
default:
case ScreenOrientationAuto:
attribute = static_cast<Qt::WidgetAttribute>(130);
break;
#else // QT_VERSION < 0x040702
case ScreenOrientationLockPortrait:
attribute = Qt::WA_LockPortraitOrientation;
break;
case ScreenOrientationLockLandscape:
attribute = Qt::WA_LockLandscapeOrientation;
break;
default:
case ScreenOrientationAuto:
attribute = Qt::WA_AutoOrientation;
break;
#endif // QT_VERSION < 0x040702
};
setAttribute(attribute, true);
}
void MainWindow::showExpanded()
{
#if defined(Q_WS_SIMULATOR)
showFullScreen();
#elif defined(Q_WS_MAEMO_5)
showMaximized();
#else
show();
#endif
}