扫雷python实现
【摘要】
global_var.py
#全局变量
tablemaps = []
gamemaps = []
123
saolei.py文件
import random
import global_var as...
global_var.py
#全局变量
tablemaps = []
gamemaps = []
- 1
- 2
- 3
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 += 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("此处没有旗子")
- 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
文章来源: andyguo.blog.csdn.net,作者:山顶夕景,版权归原作者所有,如需转载,请联系作者。
原文链接:andyguo.blog.csdn.net/article/details/116357824
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)