-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
156 lines (148 loc) · 4.19 KB
/
mainwindow.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
#include<QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "box.h"
#include "utils.h"
#include <cstdlib>
#include <ctime>
#define SIZE 10
#define BOMBLEVEL 5
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
boxSize=37;
isGameOngoing=true;
srand(time(NULL));
ui->setupUi(this);
//=======================
setupGrid();
setupMenu();
}
void MainWindow::setupMenu(){
connect(ui->actionNew_Game, SIGNAL(triggered()),this, SIGNAL(newGame()));
connect(ui->actionExit_2, SIGNAL(triggered()),qApp,SLOT(quit()));
}
void MainWindow::setupGrid(){
//mapper definitions
QSignalMapper *mouseLeftMapper;
QSignalMapper *mouseRightMapper;
mouseLeftMapper = new QSignalMapper(this);
mouseRightMapper = new QSignalMapper(this);
connect(mouseLeftMapper, SIGNAL(mapped(int)),this, SLOT(boxClicked(int)));
connect(mouseRightMapper, SIGNAL(mapped(int)),this, SLOT(boxRightClicked(int)));
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
bool bomb=(utils::randbool(BOMBLEVEL));
Box *b=new Box("Yeah sure",bomb);
grid[i][j]=b;
b->setSizePolicy(QSizePolicy ::Expanding , QSizePolicy ::Expanding );
b->setFocusPolicy(Qt::NoFocus);
b->setMinimumSize(boxSize,boxSize);
b->setMaximumSize(boxSize,boxSize);
QFont font = b->font();
font.setPointSize(18);
b->setFont(font);
ui->mineField->addWidget(b,i,j);
//event binding
mouseLeftMapper->setMapping(b,(i*1000)+j);
mouseRightMapper->setMapping(b,(i*1000)+j);
connect(b,SIGNAL(clicked()),mouseLeftMapper,SLOT(map()));
connect(b,SIGNAL(rightClicked()),mouseRightMapper,SLOT(map()));
}
}
}
int MainWindow::findBombCount(int x,int y){
int count=0;
for(int i=-1;i<=1;i++){
if(x+i<0 || x+i>SIZE-1)continue;
for(int j=-1;j<=1;j++){
if(y+j<0 || y+j>SIZE-1 || (i==0 && j==0))continue;
printf("x=%d y=%d\n",x+i,y+j);
Box* box=grid[x+i][y+j];
if(box->isBomb())count++;
}
}
return count;
}
bool MainWindow::winCheck(){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
Box *b=grid[i][j];
if(b->isBomb() && !b->isFlagged())return false;
if(!b->isBomb() && !b->isClicked())return false;
if(!b->isBomb() && b->isFlagged())return false;
}
}
printf("You won\n");
isGameOngoing=false;
showMsg("Congratulations, You Won!");
return true;
}
void MainWindow::showMsg(QString s){
QMessageBox msgbox;
msgbox.setWindowTitle("Minesweeper");
msgbox.setText(s);
msgbox.exec();
}
void MainWindow::explodeAll(){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
Box *b=grid[i][j];
if(b->isBomb()){
b->boxClicked();
}
}
}
isGameOngoing=false;
showMsg("Oops! You lose.");
}
void MainWindow::boxClicked(int id){
if(!isGameOngoing)return;
int xy[2];
xy[0]=id/1000;
xy[1]=id%1000;
//printf("clicked - (%d,%d)\n",
//xy[0],xy[1]);
Box* b=grid[xy[0]][xy[1]];
if(b->isBomb()){
explodeAll();
b->boxClicked();
return;
}
b->boxClicked();
recursiveClean(xy[0],xy[1]);
winCheck();
}
void MainWindow::boxRightClicked(int id){
if(!isGameOngoing)return;
int xy[2];
xy[0]=id/1000;
xy[1]=id%1000;
printf("R-clicked - (%d,%d)\n",
xy[0],xy[1]);
Box* b=grid[xy[0]][xy[1]];
b->boxRightClicked();
winCheck();
}
void MainWindow::recursiveClean(int x,int y){
grid[x][y]->boxClicked();
grid[x][y]->setBombCount(findBombCount(x,y));
if(findBombCount(x,y)==0){
for(int i=-1;i<=1;i++){
if(x+i<0 || x+i>SIZE-1)continue;
for(int j=-1;j<=1;j++){
if(y+j<0 || y+j>SIZE-1 || (i==0 && j==0))continue;
if(!grid[x+i][y+j]->isClicked()){
recursiveClean(x+i,y+j);
}
}
}
}else{
return;
}
}
MainWindow::~MainWindow()
{
delete ui;
}