-
Notifications
You must be signed in to change notification settings - Fork 1
/
Minesweeper.cpp
76 lines (63 loc) · 1.67 KB
/
Minesweeper.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
//Minesweeper
#include <graphics.h>
#include <time.h>
using namespace std;
int main()
{
srand(time(0));
RenderWindow app(VideoMode(400, 400), "Minesweeper!");
int w=32;
int grid[12][12];
int sgrid[12][12]; //for showing
Texture t;
t.loadFromFile("images/tiles.jpg");
Sprite s(t);
for (int i=1;i<=10;i )
for (int j=1;j<=10;j )
{
sgrid[i][j]=10;
if (rand()%5==0) grid[i][j]=9;
else grid[i][j]=0;
}
for (int i=1;i<=10;i )
for (int j=1;j<=10;j )
{
int n=0;
if (grid[i][j]==9) continue;
if (grid[i 1][j]==9) n ;
if (grid[i][j 1]==9) n ;
if (grid[i-1][j]==9) n ;
if (grid[i][j-1]==9) n ;
if (grid[i 1][j 1]==9) n ;
if (grid[i-1][j-1]==9) n ;
if (grid[i-1][j 1]==9) n ;
if (grid[i 1][j-1]==9) n ;
grid[i][j]=n;
}
while (app.isOpen())
{
Vector2i pos = Mouse::getPosition(app);
int x = pos.x/w;
int y = pos.y/w;
Event e;
while (app.pollEvent(e))
{
if (e.type == Event::Closed)
app.close();
if (e.type == Event::MouseButtonPressed)
if (e.key.code == Mouse::Left) sgrid[x][y]=grid[x][y];
else if (e.key.code == Mouse::Right) sgrid[x][y]=11;
}
app.clear(Color::White);
for (int i=1;i<=10;i )
for (int j=1;j<=10;j )
{
if (sgrid[x][y]==9) sgrid[i][j]=grid[i][j];
s.setTextureRect(IntRect(sgrid[i][j]*w,0,w,w));
s.setPosition(i*w, j*w);
app.draw(s);
}
app.display();
}
return 0;
}