代码很简单,注释很详细。附上。
#coding:utf-8
import sys
import random
"""
地图标记: MAP_
"""
MAP_BLANK = 0 # 地图空白
MAP_MINE = 1 # 有雷存在
MAP_USED = 2 # 已经排除
"""
错误码:ERROR_
"""
ERROR_OK = 5 # 结果ok
ERROR_FAIL = 6 # 结果失败
ERROR_NULL = 7 # 无效的输入
ERROR_PASS = 8 # 游戏胜利
class mine_sweep():
def __init__(self, row = 10, column = 10, mine_num = 20):
self.row = row # 地图 行
self.column = column # 地图 列
self.mine_num = mine_num # 地雷数量
# map_list row * column 的地图
self.map_list = [[0 for i in range(self.row)] for j in range(self.column)]
# 用来显示的地图,在显示时隐藏地雷
self.map_list_show = [[0 for i in range(self.row)] for j in range(self.column)]
def init_data(self):
"""初始化玩家数据, 随机生成地雷"""
_map_list_ = [[0 for i in range(self.row)] for j in range(self.column)]
self.map_list = self.map_list or _map_list_
mine_num = self.mine_num # 确保重开不会影响到self.mine_num的值
while mine_num > 0:
map_x = random.randint(0, self.row) # 随机生成列和行
map_y = random.randint(0, self.column)
if self.map_list[map_x][map_y] == MAP_BLANK: # 如果是随机出相同的点了,重新随机。
self.map_list[map_x][map_y] = MAP_MINE
mine_num = mine_num - 1
def printf_map_list(self):
for i in range(self.row):
print(self.map_list[i])
# def game_show(self):
# print("欢迎来到扫雷游戏")
# self.run()
def set_pos(self, x, y):
if self.map_list[x][y] == MAP_BLANK:
self.derived_mark(x, y, True)
self.derived_mark(x, y, False)
return ERROR_OK
elif self.map_list[x][y] == MAP_MINE:
print("你踩到雷了,game over")
self.map_list_show[x][y] = MAP_MINE
return ERROR_FAIL
else:
print("该处已经踩过了, 请选择其它地方")
return ERROR_NULL
def safe_input(self):
while True:
try:
pos = raw_input("x, y = ")
pos = pos.replace(' ', '')
x = int(pos[0:1])
y = int(pos[2:3])
if x >= 0 and x <= self.row \
and y >= 0 and y <= self.column:
break
else:
print("输入的x y不在区间(0-7)中,超出了地图的大小,请重新输入")
except :
print("输入的参数不是数字,请检查重新输入")
return x, y
def check_win(self):
for i in range(self.row):
for j in range(self.column):
if self.map_list[i][j] == MAP_BLANK:
return ERROR_FAIL
return ERROR_PASS
def run(self):
status = MAP_BLANK
self.printf_map_list()
while status != ERROR_FAIL:
print("请输入x y做坐标,逗号隔开,如果输入多个参数,只选择前两个参数")
x, y = self.safe_input()
status = self.set_pos(x,y)
# 默认每次输入之后会及时刷新map_list,然后打印出来
self.printf_map_list()
res = self.check_win()
if res == ERROR_PASS:
print("恭喜你获得胜利")
break
"""衍生标记,当输入一个点后,横纵坐标没有雷的地方也都被扫空"""
def derived_mark(self, row, col, add):
x, y = row, col
while x >= 0 and x < self.row:
if self.map_list[x][y] != MAP_MINE:
self.map_list[x][y] = MAP_USED
self.map_list_show[x][y] = MAP_USED
else:
break
if add:
x = x + 1
else:
x = x - 1
x, y = row, col
while y >= 0 and y < self.column:
if self.map_list[x][y] != MAP_MINE:
self.map_list[x][y] = MAP_USED
self.map_list_show[x][y] = MAP_USED
else:
break
if add:
y = y + 1
else:
y = y - 1
def mine_game():
print(
"================ 扫雷 ==============\n"
"1:游戏开始\t"
"2:游戏介绍\t"
"3:退出游戏\n"
"====================================\n"
)
while True:
try:
player = input("请输入:")
if player >=1 and player <= 3:
break
else:
print("超出界限,无可选项,请重试。")
except:
print("请检测输如是否为数字(1-3)")
if player == 1:
obj = mine_sweep(8,8,16)
obj.init_data()
obj.run()
elif player == 2:
print("x,y输入要选定的位置\n"
"如果触发地雷,游戏结束\n"
"非雷会衍生标记横纵坐标没有雷的地方\n"
"祝游戏愉快\n")
elif player == 3:
print("再见")
sys.exit()
if __name__ == "__main__":
while True:
mine_game()
有什么建议和改进的地方,请留言。