-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicswindow.cpp
243 lines (198 loc) · 4.59 KB
/
graphicswindow.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
#include "graphicswindow.h"
#include <QTimer>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/** Nothing */
void GraphicsWindow::handleTimer() {
counter++;
if(counter == 100)
{
timer->stop();
}
}
//clicking will make the timer stop or start
/** Nothing */
void GraphicsWindow::clicker()
{
}
/** Default Constructor*/
GraphicsWindow::GraphicsWindow() {
//We need a scene and a view to do graphics in QT
scene = new QGraphicsScene();
setScene(scene);
setWindowTitle( "Programming Assignment #4: Math Puzzles");
//view = new QGraphicsView( scene );
//To fill a rectangle use a QBrush. To draw the border of a shape, use a QPen
QBrush redBrush(Qt::red);
QPen blackPen(Qt::black);
QBrush blueBrush(Qt::blue);
QBrush yellowBrush(Qt::yellow);
QBrush greenBrush(Qt::green);
}
/** Show
* Displays the graphicsWindow */
void GraphicsWindow::show() {
//This is only called once by main. Timers don't start working until
//we call start
//timer->start();
//This is how we get our view displayed.
this->show();
}
/** getScene
* Scene Accessor */
QGraphicsScene *GraphicsWindow::getScene()
{
return scene;
}
/** moveTile
* @param piece1 The GUITile being moved */
void GraphicsWindow::moveTile(GUITile* piece1)
{
if(!b->solved())
{
int zeroX = 0;
int zeroY = 0;
int tempX = piece1->getxCord();
int tempY = piece1->getyCord();
int zero_index = 0;
int value_ = piece1->getValue();
b->move(value_);
for(int j=0; j< b->getSize(); j++)
{
if(tiles[j]->getValue() == 0)
{
zeroX = tiles[j]->getxCord();
zeroY = tiles[j]->getyCord();
zero_index = j;
}
}
if(tempX == zeroX || tempY == zeroY)
{
if((tempX+100) == zeroX || (tempX-100) == zeroX)
{
// piece1->setPos(zeroX, zeroY);
// piece1->setxCord(zeroX);
// piece1->setyCord(zeroY);
piece1->setDestinationX(zeroX);
piece1->setDestinationY(zeroY);
piece1->setVelocityX((zeroX-tempX)/10);
piece1->setVelocityY((zeroY-tempY)/10);
piece1->getTimer()->start();
// piece1->moveTilesAnimate();
// piece1->setxCord(zeroX);
// piece1->setyCord(zeroY);
tiles[zero_index]->setPos(tempX, tempY);
tiles[zero_index]->setxCord(tempX);
tiles[zero_index]->setyCord(tempY);
// QPointF p(zeroX, zeroY);
// QRectF r(piece1);
// r.moveTo(p);
// setRect( r );
}
else if((tempY+100) == zeroY || (tempY-100) == zeroY)
{
// piece1->setPos(zeroX, zeroY);
// piece1->setxCord(zeroX);
// piece1->setyCord(zeroY);
piece1->setDestinationX(zeroX);
piece1->setDestinationY(zeroY);
piece1->setVelocityY((zeroY-tempY)/10);
piece1->setVelocityX((zeroX-tempX)/10);
piece1->getTimer()->start();
/*
timer = new QTimer(this);
timer->setInterval(1);
// connect(timer, SIGNAL(timeout()), this, SLOT(handletimer()));
timer->start();
while (true)
{
if((tempY+100) == zeroY)
{
piece1->setPos(tempX, tempY+1);
}
if((tempY-100) == zeroY)
{
piece1->setPos(tempX, tempY-1);
}
counter++;
if(counter == 100)
{
timer->stop();
break;
}
}
// piece1->getTimer()->start();
// piece1->moveTilesAnimate();
for(int i=0; i<100; i++)
{
if((tempY+100) == zeroY)
{
piece1->setPos(tempX+i, tempY);
}
else if((tempY-100) == zeroY)
{
piece1->setPos(tempX-i, tempY);
}
wait(1);
}
*/
// piece1->setxCord(zeroX);
// piece1->setyCord(zeroY);
tiles[zero_index]->setPos(tempX, tempY);
tiles[zero_index]->setxCord(tempX);
tiles[zero_index]->setyCord(tempY);
}
}
zeroX = 0;
zeroY = 0;
tempX = 0;
tempY = 0;
zero_index = 0;
}
if (b->solved())
{
QMessageBox messagebox;
messagebox.about(0, "Congratulations!", "You have won the game.");
messagebox.setFixedSize(500,200);
}
}
/** getTiles
* @return tiles Returns Array of tiles */
GUITile** GraphicsWindow::getTiles()
{
return tiles;
}
/** getTilesAt
* @param index The index of the array being accessed
* @return tiles[index] The tile you want to access */
GUITile* GraphicsWindow::getTilesAt(int index)
{
return tiles[index];
}
/** getBoard
* @return b Returns your gameboard */
Board* GraphicsWindow::getBoard()
{
return b;
}
/** setBoard
* @param *b_ The Board you want to set your game to*/
void GraphicsWindow::setBoard(Board *b_)
{
b = b_;
}
/** setTilesAt
* @param index The index of the GUITile in the array
* @param *piece The GUITile being set to in the array */
void GraphicsWindow::setTilesAt(int index, GUITile *piece)
{
tiles[index] = piece;
}
/** Destructor */
GraphicsWindow::~GraphicsWindow()
{
timer->stop();
delete timer;
delete scene;
}