扫雷python实现

网友投稿 264 2022-08-25


扫雷python实现

global_var.py

#全局变量tablemaps = []gamemaps = []

saolei.py文件

import randomimport global_var as gl# # 计算雷数def mine_num(line, col): mineNum = 0 # 计算周围8个格子 if line - 1 >= 0 and col - 1 >= 0: mineNum += gl.tablemaps[line - 1][col - 1] if line - 1 >= 0: mineNum += gl.tablemaps[line - 1][col] if line - 1 >= 0 and col + 1 < 9: mineNum += gl.tablemaps[line - 1][col + 1] if col - 1 >= 0: mineNum += gl.tablemaps[line][col - 1] if col + 1 < 9: mineNum += gl.tablemaps[line][col + 1] if line + 1 < 9 and col - 1 >= 0: mineNum += gl.tablemaps[line + 1][col - 1] if line + 1 < 9: mineNum += gl.tablemaps[line + 1][col] if line + 1 < 9 and col + 1 < 9: mineNum += gl.tablemaps[line + 1][col + 1] return mineNumdef create_mine(): gl.gamemaps = [['?'] * 9 for i in range(9)] gl.tablemaps = [[0] * 9 for i in range(9)] for i in range(15): x = random.randrange(0, 9) y = random.randrange(0, 9) if gl.tablemaps[x][y] == 0: gl.tablemaps[x][y] = 1 else: x = random.randrange(1, 8) y = random.randrange(1, 8) gl.tablemaps[x][y] = 1 # 展示雷分布的图 注释掉可隐藏 for a in range(9): print(gl.tablemaps[a])def create_table(): for line in range(9): if line == 0: print(" 0 1 2 3 4 5 6 7 8") for column in range(9): if column == 0: print(line, end=" ") print(gl.gamemaps[line][column], end=" ") print("") print("输入例如:0,0,s 表示安全") print("输入例如:0,0,c 表示插旗子") print("输入例如:0,0,d 表示拔旗子")def checkvictory(): for line in range(9): for column in range(9): if gl.gamemaps[line][column] == '?': return False return Truedef base_count(line, col): mineNum = 0 mineNum = mine_num(line, col) if mineNum == 0: gl.gamemaps[line][col] = ' ' main_count(line, col) else: gl.gamemaps[line][col] = mineNum# 计算九宫格def main_count(line, col): # 计算周围8个格子 if line - 1 >= 0 and col - 1 >= 0 and gl.gamemaps[line - 1][col - 1] == "?": base_count(line - 1, col - 1) if line - 1 >= 0 and gl.gamemaps[line - 1][col] == "?": base_count(line - 1, col) if line - 1 >= 0 and col + 1 < 9 and gl.gamemaps[line - 1][col + 1] == "?": base_count(line - 1, col + 1) if col - 1 >= 0 and gl.gamemaps[line][col - 1] == "?": base_count(line, col - 1) if col + 1 < 9 and gl.gamemaps[line][col + 1] == "?": base_count(line, col + 1) if line + 1 < 9 and col - 1 >= 0 and gl.gamemaps[line + 1][col - 1] == "?": base_count(line + 1, col - 1) if line + 1 < 9 and gl.gamemaps[line + 1][col] == "?": base_count(line + 1, col) if line + 1 < 9 and col + 1 < 9 and gl.gamemaps[line + 1][col + 1] == "?": base_count(line + 1, col + 1)if __name__ == '__main__': create_mine() while True: create_table() operation = input("请输入:") temp = operation.split(',') if str(operation) == "r": create_mine() continue elif str(operation) == "e": break if len(temp) != 3: print("输入格式有误请重新输入!") continue # 掀开 if str(temp[2]) == "s": if gl.gamemaps[int(temp[0])][int(temp[1])] == "?": if gl.tablemaps[int(temp[0])][int(temp[1])] == 0: mineNum = mine_num(int(temp[0]), int(temp[1])) if mineNum != 0: gl.gamemaps[int(temp[0])][int(temp[1])] = mineNum else: main_count(int(temp[0]), int(temp[1])) if checkvictory(): print("恭喜胜利") a = input("输入r重新开始,输入e退出。") if a == "r": create_mine() elif a == "e": break else: print("输入错误请重新输入") elif gl.tablemaps[int(temp[0])][int(temp[1])] == 1: print("-------失败,将重新开始------") a = input("输入r重新开始,输入e退出。") if a == "r": create_mine() elif a == "e": break else: print("输入错误请重新输入") else: print("此处不能掀开") # 插旗 elif temp[2] == "c": if gl.gamemaps[int(temp[0])][int(temp[1])] == "?": gl.gamemaps[int(temp[0])][int(temp[1])] = "*" if checkvictory(): print("恭喜胜利") a = input("输入r重新开始,输入e退出。") if a == "r": create_mine() elif a == "e": break else: print("输入错误请重新输入") else: print("此处不能插旗") # 拔旗 elif temp[2] == "d": if gl.gamemaps[int(temp[0])][int(temp[1])] == "*": gl.gamemaps[int(temp[0])][int(temp[1])] = "?" else: print("此处没有旗子")


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:# yyds干货盘点 # 请问dataframe里面的这个我怎么变成6/27/2022或者2022-6-27
下一篇:python报错Error:‘xxx’ object is not callable(python报错处理)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~