-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
364 lines (288 loc) · 9.67 KB
/
Game.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
#include "Game.h"
#include "Hex.h"
#include "Button.h"
#include <QGraphicsTextItem>
#include <QBrush>
#include <QGraphicsTextItem>
#include <stdlib.h>
#include <time.h>
#include <QDebug>
Game::Game(QWidget *parent){
//setting up the screen
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(1024,768);
//setting up the scene
scene = new QGraphicsScene();
scene->setSceneRect(0,0,1024,768);
setScene(scene);
cardToPlace = NULL;
this->setWindowTitle(QString("HexWar"));
}
void Game::start(){
cardToPlace = NULL;
numOfCardsPlaced = 0;
//clear the menu
scene->clear();
drawGUI();
hexBoard = new HexBoard();
hexBoard->placeHexes(200,80,7,7);
scene->setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
createInitialCards();
}
void Game::close(){
exit(0);
}
void Game::restartGame(){
//clear some stuff then call start()
Player1Cards.clear();
Player2Cards.clear();
hexBoard->getHexes().clear();
scene->clear();
start();
}
void Game::drawPanel(int x, int y, int width, int height, QColor color, double opacity){
//draws a panel at specified location with specified criteria
QGraphicsRectItem* panel = new QGraphicsRectItem(x,y,width,height);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(color);
panel->setBrush(brush);
panel->setOpacity(opacity);
scene->addItem(panel);
}
void Game::drawGUI(){
//draw left panel
drawPanel(0,0,150,768,Qt::darkBlue,0.5);
//draw right panel
drawPanel(1024-150,0,150,768,Qt::darkRed,0.5);
//Text Styling
QFont font("Arial",20);
//place player 1 text
QGraphicsTextItem* p1 = new QGraphicsTextItem("P1");
p1->setDefaultTextColor(Qt::blue);
p1->setFont(font);
p1->setPos(10,0);
scene->addItem(p1);
//place player 2 text
QGraphicsTextItem* p2 = new QGraphicsTextItem("P2");
p2->setDefaultTextColor(Qt::red);
p2->setFont(font);
p2->setPos(1024-150+25-25,0);
scene->addItem(p2);
//place Turn Text
whosTurnText = new QGraphicsTextItem();
whosTurnText->setDefaultTextColor(Qt::white);
whosTurnText->setFont(font);
setWhoseTurn(QString("PLAYER1"));
//(490,0) is top middle of the screen
whosTurnText->setPos(400,0);
scene->addItem(whosTurnText);
}
void Game::createNewCard(QString player){
Hex* card = new Hex();
card->setOwner(player);
card->setIsPlaced(false);
//set random attack values to each side
for(size_t i=0; i<6 ;i++){
//generate a random number
int randNum = rand() % 6 + 1; // generates any value btw 1 and 6
card->setAttackOf(i,randNum);
}
//make side attacks visible
card->displaySideAttacks();
if(player == QString("PLAYER1"))
Player1Cards.append(card);
else
Player2Cards.append(card);
drawCards();
}
void Game::createInitialCards(){
//create player 1's cards
for(size_t i=0,n = 5 ;i< n ;i++){
createNewCard(QString("PLAYER1"));
createNewCard(QString("PLAYER2"));
}
drawCards();
}
void Game::drawCards(){
//traverse through list and draw cards on the screen
//drawCards() is to be called often,so delete the items in the scene and reprint them
//player 1
for(size_t i=0, n=Player1Cards.size() ; i<n ; i++){
//check if item is in scene or not
if(Player1Cards[i]->scene() != nullptr)
scene->removeItem(Player1Cards[i]);
}
//player 2
for(size_t i=0, n=Player2Cards.size() ; i<n ; i++){
//check if item is in scene or not
if(Player2Cards[i]->scene() != nullptr)
scene->removeItem(Player2Cards[i]);
}
//draw player 1 cards on the left
for(size_t i=0, n=Player1Cards.size() ; i<n ; i++){
Hex * card = Player1Cards[i];
card->setPos(13,70+85*i);
scene->addItem(card);
}
//draw player 2 cards on the right
for(size_t i=0, n=Player2Cards.size() ; i<n ; i++){
Hex * card = Player2Cards[i];
card->setPos(1024-150+13,70+85*i);
scene->addItem(card);
}
}
void Game::displayMainMenu(){
scene->setBackgroundBrush(QBrush(Qt::black, Qt::SolidPattern));
//Creating the game title
QGraphicsTextItem* titleText = new QGraphicsTextItem(QString("HexWar"));
QFont titleFont("verdana",50);
titleText->setDefaultTextColor(Qt::red);
titleText->setFont(titleFont);
int textXPos = this->width()/2 - titleText->boundingRect().width()/2;
int textYPos = this->height()/2 - titleText->boundingRect().height()/2 - 100;
titleText->setPos(textXPos,textYPos);
scene->addItem(titleText);
//creating the play button
Button *playButton = new Button(QString("Play"));
int butXPos = this->width()/2 - playButton->boundingRect().width()/2;
int butYPos = 500;
playButton->setPos(butXPos , butYPos);
connect(playButton, SIGNAL(clicked()),this,SLOT(start()));
scene->addItem(playButton);
//creating the quit Button
Button *quitButton = new Button(QString("Quit"));
butXPos = this->width()/2 - quitButton->boundingRect().width()/2;
butYPos = 600;
quitButton->setPos(butXPos , butYPos);
connect(quitButton, SIGNAL(clicked()),this,SLOT(close()));
scene->addItem(quitButton);
}
QString Game::getWhoseTurn(){
//return whose turn it is
return whosTurn_;
}
void Game::setWhoseTurn(QString Player){
//change turn
whosTurn_ = Player;
qDebug() << whosTurn_ + " Turn";
//change TextOnScreen
whosTurnText->setPlainText(Player + QString(" TURN"));
if(whosTurn_ == QString("PLAYER1"))
whosTurnText->setDefaultTextColor(Qt::blue);
else
whosTurnText->setDefaultTextColor(Qt::red);
}
void Game::pickUpCard(Hex *card){
//picks up the specify Card
if(card->getOwner() == getWhoseTurn() && cardToPlace == NULL){
cardToPlace = card;
originalPos = card->pos();
return;
}
}
void Game::mouseMoveEvent(QMouseEvent *event){
//make the card follow the mouse
if (cardToPlace != NULL){
cardToPlace->setPos(event->pos());
}
QGraphicsView::mouseMoveEvent(event);
}
void Game::mousePressEvent(QMouseEvent *event){
if(event->button() == Qt::RightButton){
if(cardToPlace != NULL){
cardToPlace->setPos(originalPos);
cardToPlace = NULL;
}
}
QGraphicsView::mousePressEvent(event);
}
void Game::placeCard(Hex *hexToReplace){
//if there is a card to place, place it on the board
cardToPlace->setPos(hexToReplace->pos());
hexBoard->append(cardToPlace);
hexBoard->removeAll(hexToReplace);
scene->removeItem(hexToReplace);
cardToPlace->setIsPlaced(true);
removeFromDeck(cardToPlace,getWhoseTurn());
//find neighours
cardToPlace->findNeighbours();
cardToPlace->captureNeigbour();
//make cardToPlace NULL
cardToPlace = NULL;
// replace the card
createNewCard(getWhoseTurn());
//switch turns
nextPlayerTurn();
numOfCardsPlaced++;
if(numOfCardsPlaced >= hexBoard->getHexes().size()){
gameOver();
}
}
void Game::gameOver(){
//count hexes, determine who has more
int p1Hexes = 0;
int p2Hexes = 0;
for(size_t i=0,n=hexBoard->getHexes().size(); i<n; i++){
if(hexBoard->getHexes()[i]->getOwner() == QString("PLAYER1")){
p1Hexes++;
}
else if(hexBoard->getHexes()[i]->getOwner() == QString("PLAYER2")){
p2Hexes++;
}
}
QString message;
if(p1Hexes > p2Hexes){
message = QString("Player 1 has won");
}
else if(p1Hexes < p2Hexes){
message = QString("Player 1 has won");
}
else{
message = QString("Draw");
}
displayGameOverWindow(message);
}
void Game::nextPlayerTurn(){
//Change Player Turns
if(getWhoseTurn() == QString("PLAYER1")){
setWhoseTurn(QString("PLAYER2"));
}
else if(getWhoseTurn() == QString("PLAYER2")){
setWhoseTurn(QString("PLAYER1"));
}
}
void Game::removeFromDeck(Hex *card, QString player){
if(player == QString("PLAYER1")){
//remove all instances of that card
Player1Cards.removeAll(card);
}
if(player == QString("PLAYER2")){
//remove all instances of that card
Player2Cards.removeAll(card);
}
}
void Game::displayGameOverWindow(QString message){
//disable all scene items
for(size_t i=0, n = scene->items().size();i<n;i++){
scene->items()[i]->setEnabled(false);
}
//pop up semi transparent scene
drawPanel(0,0,1024,768,Qt::black,0.65);
//draw panel
drawPanel(312,184,400,400,Qt::lightGray,0.75);
//create PlayAgainButton
Button *playAgain = new Button(QString("Play Again"));
playAgain->setPos(410,300);
scene->addItem(playAgain);
connect(playAgain,SIGNAL(clicked()),this,SLOT(restartGame()));
//create quit button
Button *quit = new Button(QString("Quit"));
quit->setPos(410,350);
scene->addItem(quit);
connect(quit,SIGNAL(clicked()),this,SLOT(close()));
QGraphicsTextItem* overText = new QGraphicsTextItem(message);
overText->setPos(460,225);
scene->addItem(overText);
}