global_var.py #全局变量 tablemaps = [] gamemaps = [] saolei.py文件 import random import global_var as gl # # 计算雷数 def mine_num ( line , col ): mineNum = 0 # 计算周围8个格子 if line - 1 = 0 and col - 1 = 0 : mineNum += g
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 mineNum
def 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 True
def 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("此处没有旗子")