-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.cs
141 lines (118 loc) · 4.14 KB
/
minesweeper.cs
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
using System;
using System.IO;
namespace GameBox
{
class Minesweeper
{
struct Minesweeper_data
{
public int near_bombs;
public bool bomb_here;
public bool visible;
}
public bool Minesweeper_game()
{
int mapx = 9;
int mapy = 9;
int posx, posy;
int bomb_count = 10;
const string char_bomb = "X";
const string char_cursor = "O";
bool run = true;
Minesweeper_data[,] bomb_map = new Minesweeper_data[mapx, mapy];
Screen_data[,] map = new Screen_data[mapx, mapy];
Random cord = new Random();
ConsoleKey key = new ConsoleKey();
Border border = new Border();
Screen screen = new Screen();
Console.Clear();
Console.CursorVisible = false;
border.border_print(mapx, mapy);
Console.Clear();
for (int i = 0; i < bomb_count; i++)
{
do{
posx = cord.Next(0, mapx);
posy = cord.Next(0, mapy);
}while(bomb_map[posx, posy].bomb_here);
bomb_map[posx, posy].bomb_here = true;
for (int cy = Math.Max(posy-1, 0); cy <= Math.Min(posy+1, mapy-1); cy++)
{
for (int cx = Math.Max(posx-1, 0); cx <= Math.Min(posx+1, mapx-1); cx++)
{
bomb_map[cx, cy].near_bombs++;
}
}
}
posx = 0;
posy = 0;
/* for (int cy = 0; cy < mapy; cy++)
{
for (int cx = 0; cx < mapy; cx++)
{
if(bomb_map[cx, cy].bomb_here)
{
map[cx, cy].screen_char = char_bomb;
map[cx, cy].visible = true;
}
else
{
map[cx, cy].screen_char = Convert.ToString(bomb_map[cx, cy].near_bombs);
map[cx, cy].visible = true;
}
}
} */
while(run)
{
for (int cy = 0; cy < mapy; cy++)
{
for (int cx = 0; cx < mapy; cx++)
{
if(bomb_map[cx, cy].bomb_here && bomb_map[cx, cy].visible)
{
map[cx, cy].screen_char = char_bomb;
map[cx, cy].visible = true;
}
else if(bomb_map[cx, cy].visible)
{
map[cx, cy].screen_char = Convert.ToString(bomb_map[cx, cy].near_bombs);
map[cx, cy].visible = true;
}
else
{
map[cx, cy].visible = false;
}
}
}
map[posx, posy].screen_char = char_cursor;
map[posx, posy].visible = true;
key = Console.ReadKey().Key;
switch(key)
{
case ConsoleKey.A:
if(posx > 0) posx--;
break;
case ConsoleKey.D:
if(posx < mapx-1) posx++;
break;
case ConsoleKey.W:
if(posy > 0) posy--;
break;
case ConsoleKey.S:
if(posy < mapy-1) posy++;
break;
case ConsoleKey.Enter:
bomb_map[posx, posy].visible = true;
break;
case ConsoleKey.Escape:
return false;
case ConsoleKey.M:
return true;
}
screen.Print(map);
Console.Write("x: {0}, y: {1}", posx, posy);
}
return true;
}
}
}