forked from zengj8/pyconsolegame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mine.py
246 lines (214 loc) · 5.54 KB
/
Mine.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
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
# -*- coding: utf-8 -*-
# chr(17)
import random
import os
import sys
class Mine(object):
"""docstring for Mine"""
def __init__(self):
super(Mine, self).__init__()
# print 'init'
self.x, self.y, self.mineNum = 9, 9, 9
self.map = [['?' for col in xrange(self.y)] for row in xrange(self.x)] # 玩家眼中的棋盘
# print 'self'
self.data = [[0 for col in xrange(self.y)] for row in xrange(self.x)] # 真实棋盘
self.lose = False
self.openNum = 0
count = 0
# 造棋盘,9为雷
while count < self.mineNum:
x = random.randint(0, 8)
y = random.randint(0, 8)
if self.data[x][y] == 9:
continue
self.data[x][y] = 9
count += 1
for i in xrange(self.x):
for j in xrange(self.y):
if self.data[i][j] == 9:
continue
else:
self.countMine(i, j)
@property
def openNum(self):
return self.__openNum
@openNum.setter
def openNum(self, value):
self.__openNum = value
@property
def lose(self):
return self.__lose
@lose.setter
def lose(self, value):
self.__lose = value
@property
def x(self):
return self.__x
@x.setter
def x(self, value):
self.__x = value
@property
def y(self):
return self.__y
@y.setter
def y(self, value):
self.__y = value
@property
def mineNum(self):
return self.__mineNum
@mineNum.setter
def mineNum(self, value):
self.__mineNum = value
# 造棋盘,给每个风格赋值为周围的雷数
def countMine(self, x, y):
count = 0
if x > 0:
count += (1 if self.data[x - 1][y] == 9 else 0)
if y > 0:
count += (1 if self.data[x - 1][y - 1] == 9 else 0)
if y + 1 < self.y:
count += (1 if self.data[x - 1][y + 1] == 9 else 0)
if x + 1 < self.x:
count += (1 if self.data[x + 1][y] == 9 else 0)
if y > 0:
count += (1 if self.data[x + 1][y - 1] == 9 else 0)
if y + 1 < self.y:
count += (1 if self.data[x + 1][y + 1] == 9 else 0)
if y > 0:
count += (1 if self.data[x][y - 1] == 9 else 0)
if y + 1 < self.y:
count += (1 if self.data[x][y + 1] == 9 else 0)
self.data[x][y] = count
@property
def data(self):
return self.__data
@data.setter
def data(self, value):
# print 'setter'
self.__data = value
@property
def map(self):
return self.__map
@map.setter
def map(self, value):
# print 'setter'
self.__map = value
@property
def error(self):
return self.__error
@error.setter
def error(self, value):
self.__error = value
# 输出棋盘、操作、游戏胜负在控制台
def show(self):
print ' ',
for i in xrange(self.x):
print ' ' + str(i),
print ''
print ' ',
for i in xrange(self.x):
print '--',
print ''
for i in xrange(self.x):
print str(i) + '|',
for j in xrange(self.y):
print self.map[i][j] + ' ',
print ''
print '1. Input a position like 0,0,s to insure the land is safety'
print '2. Input a position like 0,0,c to insure the land is mine -> ' + chr(17)
print '3. Input a position like 0,0,d to distinct the near land -> ?'
print '-->',
if self.lose:
print 'boom'
sys.exit()
# 如果打开数 + 雷数 == 总数,说明不是雷的都打开了,判定为success
if self.openNum + self.mineNum == self.x * self.y:
print 'success'
sys.exit()
# 判断是否出界
def check(self, x, y):
if x < 0 or x >= self.x or y < 0 or y >= self.y:
return False
return True
# 打开空格周围所有的空格
def dfs(self, x, y):
mp = self.map[x][y]
num = self.data[x][y]
if mp.isdigit() or mp == chr(17):
return
if num == 9:
return
if num > 0:
self.map[x][y] = str(num)
self.openNum += 1
return
self.map[x][y] = str(num)
self.openNum += 1
for i in xrange(-1, 2, 1):
for j in xrange(-1, 2, 1):
nx, ny = x + i, y + j
if self.check(nx, ny):
self.dfs(nx, ny)
# 开格子
def insureSafety(self, x, y):
# 如果被开过或者被插过红旗,就不能打开
if self.map[x][y] == chr(17) or self.map[x][y].isdigit():
return
# 雷,判定为lose
elif self.data[x][y] == 9:
self.map[x][y] = '*'
self.lose = True
return
# 打开非0数字格
elif self.data[x][y] > 0:
self.map[x][y] = str(self.data[x][y])
self.openNum += 1
else:
self.dfs(x, y)
# 插红旗
def insureMine(self, x, y):
# 已打开过,不能插红旗
if not self.map[x][y].isdigit():
self.map[x][y] = chr(17)
# 移除红旗
def unsureMine(self, x, y):
# 已打开过,没有红旗可移除
if not self.map[x][y].isdigit():
self.map[x][y] = '?'
# 操作
def play(self, operation):
params = operation.split(',')
# 输入不合法
if len(params) != 3:
self.show()
print operation + ' illeagel input'
print '-->',
return
if (params[0].isdigit() == False) or (params[1].isdigit() == False) or (params[2] != 's' and params[2] != 'c' and params[2] != 'd'):
self.show()
print operation + ' illeagel input'
print '-->',
return
# 具体操作
x, y, op = int(params[0]), int(params[1]), params[2]
out = 'null'
if op == 's':
self.insureSafety(x, y)
out = 'insure safe'
elif op == 'c':
self.insureMine(x, y)
out = 'insure mine'
elif op == 'd':
self.unsureMine(x, y)
out = 'unsure mine'
self.show()
print 'last operation: ' + out + ' at (' + params[0] + ', ' + params[1] + ')'
print '-->',
if __name__ == '__main__':
mine = Mine()
os.system('cls')
mine.show()
while True:
s = raw_input()
os.system('cls')
mine.play(s)