-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicRule.py
177 lines (154 loc) · 6.71 KB
/
BasicRule.py
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
from random import sample, choice
class MineSweeper:
def __init__(self, length, width, number):
self.length = length
self.width = width
self.number = number
self.boom = 0
self.g_map = [[0 for _i in range(length)] for _j in range(width)] # 纯方格:0
self.step = 0
self.b_num = number
self.places = [(x, y) for x in range(length) for y in range(width)]
self.mines = sample(self.places, self.number)
for coord in self.mines:
self.g_map[coord[1]][coord[0]] = 1 # 地雷:1
def refresh(self):
self.boom = 0
self.b_num = self.number
self.g_map = [[0 for _i in range(self.length)] for _j in range(self.width)]
self.step = 0
self.places = [(x, y) for x in range(self.length) for y in range(self.width)]
self.mines = sample(self.places, self.number)
for coord in self.mines:
self.g_map[coord[1]][coord[0]] = 1 # 地雷:1
def click(self, pos_x=None, pos_y=None):
"""玩家点击"""
def count_around(x, y):
num_of_mines = 0
for i, j in zip([-1, 0, 1, -1, 1, -1, 0, 1], [1, 1, 1, 0, 0, -1, -1, -1]):
if 0 <= x + i < self.length and 0 <= y + j < self.width:
if self.g_map[y + j][x + i] == 1 or self.g_map[y + j][x + i] == '1$':
num_of_mines += 1
return num_of_mines
def chain_blank(x, y):
for i, j in zip([-1, 0, 1, -1, 1, -1, 0, 1], [1, 1, 1, 0, 0, -1, -1, -1]):
if 0 <= x + i < self.length and 0 <= y + j < self.width and \
(isinstance(self.g_map[y + j][x + i], int) or self.g_map[y + j][x + i] == '0$'):
if self.g_map[y + j][x + i] == '0$':
self.b_num += 1
self.g_map[y + j][x + i] = str(count_around(x + i, y + j))
if not count_around(x + i, y + j):
chain_blank(x + i, y + j)
while True:
try:
if 0 <= pos_x < self.length and 0 <= pos_y < self.width:
if self.g_map[pos_y][pos_x] == 0:
self.g_map[pos_y][pos_x] = str(count_around(pos_x, pos_y))
if not count_around(pos_x, pos_y):
chain_blank(pos_x, pos_y)
self.step += 1
return
elif self.g_map[pos_y][pos_x] == 1:
if self.step == 0:
while True:
new_mine = choice(self.places)
if new_mine in self.mines:
continue
else:
break
self.g_map[new_mine[1]][new_mine[0]] = 1
self.mines.remove((pos_x, pos_y))
self.mines.append(new_mine)
self.g_map[pos_y][pos_x] = str(count_around(pos_x, pos_y))
if not count_around(pos_x, pos_y):
chain_blank(pos_x, pos_y)
self.step += 1
return
else:
for coord in self.mines:
self.g_map[coord[1]][coord[0]] = '*'
self.boom = 1
return
else:
return
except ValueError:
continue
def click_around(self, pos_x=None, pos_y=None):
def count_around(p_x, p_y):
num_of_flags = 0
for i, j in zip([-1, 0, 1, -1, 1, -1, 0, 1], [1, 1, 1, 0, 0, -1, -1, -1]):
if 0 <= p_x + i < self.length and 0 <= p_y + j < self.width:
if self.g_map[p_y + j][p_x + i] in ('0$', '1$'):
num_of_flags += 1
return str(num_of_flags)
if self.g_map[pos_y][pos_x] == count_around(pos_x, pos_y) \
and not self.g_map[pos_y][pos_x] in ('0$', '1$'):
for x, y in zip([-1, 0, 1, -1, 1, -1, 0, 1], [1, 1, 1, 0, 0, -1, -1, -1]):
if (0 <= pos_x + x < self.length and 0 <= pos_y + y < self.width
and not self.g_map[pos_y + y][pos_x + x] in ('0$', '1$')):
self.click(pos_x + x, pos_y + y)
def mark_mine(self, flag_x=None, flag_y=None):
while True:
try:
if 0 <= flag_x < self.length and 0 <= flag_y < self.width:
if self.g_map[flag_y][flag_x] == 0:
self.g_map[flag_y][flag_x] = '0$'
self.b_num -= 1
return
elif self.g_map[flag_y][flag_x] == 1:
self.g_map[flag_y][flag_x] = '1$'
self.b_num -= 1
return
elif self.g_map[flag_y][flag_x] == '0$':
self.g_map[flag_y][flag_x] = 0
self.b_num += 1
return
elif self.g_map[flag_y][flag_x] == '1$':
self.g_map[flag_y][flag_x] = 1
self.b_num += 1
return
else:
return
except ValueError:
continue
def game_judge(self):
"""判断游戏是否继续"""
for i in range(self.length):
for j in range(self.width):
if self.g_map[j][i] == 0 or self.g_map[j][i] == '0$':
return 0
else:
return 1
def show(self):
"""显示游戏内容"""
for row in self.g_map:
for elem in row:
if isinstance(elem, int):
print('█', end=' ')
elif isinstance(elem, str):
if elem == '0':
print(' ', end=' ')
else:
print('{}'.format(elem), end=' ')
print()
def play(self):
while True:
self.click()
if self.boom:
self.show()
print('GameOver')
return
elif self.game_judge():
self.show()
print('You Win!!!')
return
self.show()
class EasyMode(MineSweeper):
def __init__(self):
super().__init__(9, 9, 10)
class MediumMode(MineSweeper):
def __init__(self):
super().__init__(16, 16, 40)
class HardMode(MineSweeper):
def __init__(self):
super().__init__(30, 16, 99)