《python实现扫雷,不一样的体验!》, 一起来围观吧 https://blog.csdn.net/weixin_52255338/article/details/114795735?utm_source=app&app_version=5.0.1&code=app_1562916241&uLinkId=usr1mkqgl919blen
提供个思路把以B5为起点,往上遍历有没有地雷或者旗子,没有就处于点击状态;再往下遍历。然后判断隔壁的A5有没有地雷或者旗子,重复B5的动作;左边操作完了,就操作C5……
遇到地雷或者旗子就停止遍历
print("欢迎来到扫雷游戏。")
difficulty = input("请选择难度(1 简单(易卡顿) 2 中等 3 困难 4 自定义):")
if difficulty == str(1):
mine = 10
if difficulty == str(2):
mine = 20
if difficulty == str(3):
mine = 30
if difficulty == str(4):
mine = int(input("请输入数量(80以内):"))
import random
t = 0
leis = []
while t < mine:
t += 1
a = random.randint(1,9)
b = random.randint(1,9)
leis.append(a)
leis.append(b)
class block:
def __init__(self,condition,x,y,safety):
self.condition = condition
self.x = x
self.y = y
self.safety = safety
a1 = block("■ ",1,1,"safe")
a2 = block("■ ",2,1,"safe")
a3 = block("■ ",3,1,"safe")
a4 = block("■ ",4,1,"safe")
a5 = block("■ ",5,1,"safe")
a6 = block("■ ",6,1,"safe")
a7 = block("■ ",7,1,"safe")
a8 = block("■ ",8,1,"safe")
a9 = block("■ ",9,1,"safe")
b1 = block("■ ",1,2,"safe")
b2 = block("■ ",2,2,"safe")
b3 = block("■ ",3,2,"safe")
b4 = block("■ ",4,2,"safe")
b5 = block("■ ",5,2,"safe")
b6 = block("■ ",6,2,"safe")
b7 = block("■ ",7,2,"safe")
b8 = block("■ ",8,2,"safe")
b9 = block("■ ",9,2,"safe")
c1 = block("■ ",1,3,"safe")
c2 = block("■ ",2,3,"safe")
c3 = block("■ ",3,3,"safe")
c4 = block("■ ",4,3,"safe")
c5 = block("■ ",5,3,"safe")
c6 = block("■ ",6,3,"safe")
c7 = block("■ ",7,3,"safe")
c8 = block("■ ",8,3,"safe")
c9 = block("■ ",9,3,"safe")
d1 = block("■ ",1,4,"safe")
d2 = block("■ ",2,4,"safe")
d3 = block("■ ",3,4,"safe")
d4 = block("■ ",4,4,"safe")
d5 = block("■ ",5,4,"safe")
d6 = block("■ ",6,4,"safe")
d7 = block("■ ",7,4,"safe")
d8 = block("■ ",8,4,"safe")
d9 = block("■ ",9,4,"safe")
e1 = block("■ ",1,5,"safe")
e2 = block("■ ",2,5,"safe")
e3 = block("■ ",3,5,"safe")
e4 = block("■ ",4,5,"safe")
e5 = block("■ ",5,5,"safe")
e6 = block("■ ",6,5,"safe")
e7 = block("■ ",7,5,"safe")
e8 = block("■ ",8,5,"safe")
e9 = block("■ ",9,5,"safe")
f1 = block("■ ",1,6,"safe")
f2 = block("■ ",2,6,"safe")
f3 = block("■ ",3,6,"safe")
f4 = block("■ ",4,6,"safe")
f5 = block("■ ",5,6,"safe")
f6 = block("■ ",6,6,"safe")
f7 = block("■ ",7,6,"safe")
f8 = block("■ ",8,6,"safe")
f9 = block("■ ",9,6,"safe")
g1 = block("■ ",1,7,"safe")
g2 = block("■ ",2,7,"safe")
g3 = block("■ ",3,7,"safe")
g4 = block("■ ",4,7,"safe")
g5 = block("■ ",5,7,"safe")
g6 = block("■ ",6,7,"safe")
g7 = block("■ ",7,7,"safe")
g8 = block("■ ",8,7,"safe")
g9 = block("■ ",9,7,"safe")
h1 = block("■ ",1,8,"safe")
h2 = block("■ ",2,8,"safe")
h3 = block("■ ",3,8,"safe")
h4 = block("■ ",4,8,"safe")
h5 = block("■ ",5,8,"safe")
h6 = block("■ ",6,8,"safe")
h7 = block("■ ",7,8,"safe")
h8 = block("■ ",8,8,"safe")
h9 = block("■ ",9,8,"safe")
i1 = block("■ ",1,9,"safe")
i2 = block("■ ",2,9,"safe")
i3 = block("■ ",3,9,"safe")
i4 = block("■ ",4,9,"safe")
i5 = block("■ ",5,9,"safe")
i6 = block("■ ",6,9,"safe")
i7 = block("■ ",7,9,"safe")
i8 = block("■ ",8,9,"safe")
i9 = block("■ ",9,9,"safe")
i10 = block("■ ",9,9,"safe")
blocks = [0,0,0,0,0,0,0,0,0,0,0,a1,a2,a3,a4,a5,a6,a7,a8,a9,0,b1,b2,b3,b4,b5,b6,b7,b8,b9,0,c1,c2,c3,c4,c5,c6,c7,c8,c9,0,d1,d2,d3,d4,d5,d6,d7,d8,d9,0,e1,e2,e3,e4,e5,e6,e7,e8,e9,0,f1,f2,f3,f4,f5,f6,f7,f8,f9,0,g1,g2,g3,g4,g5,g6,g7,g8,g9,0,h1,h2,h3,h4,h5,h6,h7,h8,h9,0,i1,i2,i3,i4,i5,i6,i7,i8,i9,]
def site(x,y):
if x > 0 and x < 10 and y > 0 and y < 10:
p = 10*y + x
return blocks[p]
else:
return i10
t = 0
a = 0
b = 1
while t < mine:
t += 1
site(int(leis[a]),int(leis[b])).safety = "boom"
a += 2
b += 2
def printblocks():
print(" 1 2 3 4 5 6 7 8 9")
print("1 "+a1.condition+a2.condition+a3.condition+a4.condition+a5.condition+a6.condition+a7.condition+a8.condition+a9.condition+"1")
print('2 '+b1.condition+b2.condition+b3.condition+b4.condition+b5.condition+b6.condition+b7.condition+b8.condition+b9.condition+"2")
print('3 '+c1.condition+c2.condition+c3.condition+c4.condition+c5.condition+c6.condition+c7.condition+c8.condition+c9.condition+"3")
print('4 '+d1.condition+d2.condition+d3.condition+d4.condition+d5.condition+d6.condition+d7.condition+d8.condition+d9.condition+"4")
print('5 '+e1.condition+e2.condition+e3.condition+e4.condition+e5.condition+e6.condition+e7.condition+e8.condition+e9.condition+"5")
print('6 '+f1.condition+f2.condition+f3.condition+f4.condition+f5.condition+f6.condition+f7.condition+f8.condition+f9.condition+"6")
print('7 '+g1.condition+g2.condition+g3.condition+g4.condition+g5.condition+g6.condition+g7.condition+g8.condition+g9.condition+"7")
print('8 '+h1.condition+h2.condition+h3.condition+h4.condition+h5.condition+h6.condition+h7.condition+h8.condition+h9.condition+"8")
print('9 '+i1.condition+i2.condition+i3.condition+i4.condition+i5.condition+i6.condition+i7.condition+i8.condition+i9.condition+"9")
print(" 1 2 3 4 5 6 7 8 9")
printblocks()
#计时
import time
start = time.time()
#操作
def detect_win_or_not():
l = 0
for i in blocks:
if type(i) != type(1):
if i.condition == "■ ":
l += 1
return l
while detect_win_or_not() > mine:
op1 = input("请输入横坐标")
op2 = input("请输入纵坐标")
if site(int(op1),int(op2)).safety == "boom":
print("挖到地雷,游戏失败")
t = 0
a = -2
b = -1
while t < mine:
t += 1
a += 2
b += 2
site(int(leis[a]), int(leis[b])).condition = "◎ "
printblocks()
exit()
#检测
def detect(a,b):
num = 0
if site(a-1,b-1).safety == "boom":
num += 1
if site(a,b-1).safety == "boom":
num += 1
if site(a+1,b-1).safety == "boom":
num += 1
if site(a-1,b).safety == "boom":
num += 1
if site(a+1,b).safety == "boom":
num += 1
if site(a-1,b+1).safety == "boom":
num += 1
if site(a,b+1).safety == "boom":
num += 1
if site(a+1,b+1).safety == "boom":
num += 1
return num
#调色板
numbers = ["0","\033[0;34m1\033[0m","\033[0;32m2\033[0m","\033[0;31m3\033[0m","\033[0;35m4\033[0m","\033[0;36m5\033[0m","\033[0;33m6\033[0m","\033[0;30m7\033[0m","\033[0;37m8\033[0m"]
# 自动机制
def auto_ppppppprepare(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
def auto_pppppprepare(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
auto_ppppppprepare(x - 1, y - 1)
auto_ppppppprepare(x, y - 1)
auto_ppppppprepare(x + 1, y - 1)
auto_ppppppprepare(x - 1, y)
auto_ppppppprepare(x + 1, y)
auto_ppppppprepare(x - 1, y + 1)
auto_ppppppprepare(x, y + 1)
auto_ppppppprepare(x + 1, y + 1)
def auto_ppppprepare(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
auto_pppppprepare(x - 1, y - 1)
auto_pppppprepare(x, y - 1)
auto_pppppprepare(x + 1, y - 1)
auto_pppppprepare(x - 1, y)
auto_pppppprepare(x + 1, y)
auto_pppppprepare(x - 1, y + 1)
auto_pppppprepare(x, y + 1)
auto_pppppprepare(x + 1, y + 1)
def auto_pppprepare(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
auto_ppppprepare(x - 1, y - 1)
auto_ppppprepare(x, y - 1)
auto_ppppprepare(x + 1, y - 1)
auto_ppppprepare(x - 1, y)
auto_ppppprepare(x + 1, y)
auto_ppppprepare(x - 1, y + 1)
auto_ppppprepare(x, y + 1)
auto_ppppprepare(x + 1, y + 1)
def auto_ppprepare(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
auto_pppprepare(x - 1, y - 1)
auto_pppprepare(x, y - 1)
auto_pppprepare(x + 1, y - 1)
auto_pppprepare(x - 1, y)
auto_pppprepare(x + 1, y)
auto_pppprepare(x - 1, y + 1)
auto_pppprepare(x, y + 1)
auto_pppprepare(x + 1, y + 1)
def auto_pprepare(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
auto_ppprepare(x - 1, y - 1)
auto_ppprepare(x, y - 1)
auto_ppprepare(x + 1, y - 1)
auto_ppprepare(x - 1, y)
auto_ppprepare(x + 1, y)
auto_ppprepare(x - 1, y + 1)
auto_ppprepare(x, y + 1)
auto_ppprepare(x + 1, y + 1)
def auto_prepare(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
auto_pprepare(x - 1, y - 1)
auto_pprepare(x, y - 1)
auto_pprepare(x + 1, y - 1)
auto_pprepare(x - 1, y)
auto_pprepare(x + 1, y)
auto_pprepare(x - 1, y + 1)
auto_pprepare(x, y + 1)
auto_pprepare(x + 1, y + 1)
def auto(x, y):
if site(x, y).condition == "0" + " " and x > 0 and x < 10 and y > 0 and y < 10:
site(x - 1, y - 1).condition = numbers[detect(x - 1, y - 1)] + " "
site(x, y - 1).condition = numbers[detect(x, y - 1)] + " "
site(x + 1, y - 1).condition = numbers[detect(x + 1, y - 1)] + " "
site(x - 1, y).condition = numbers[detect(x - 1, y)] + " "
site(x + 1, y).condition = numbers[detect(x + 1, y)] + " "
site(x - 1, y + 1).condition = numbers[detect(x - 1, y + 1)] + " "
site(x, y + 1).condition = numbers[detect(x, y + 1)] + " "
site(x + 1, y + 1).condition = numbers[detect(x + 1, y + 1)] + " "
auto_prepare(x - 1, y - 1)
auto_prepare(x, y - 1)
auto_prepare(x + 1, y - 1)
auto_prepare(x - 1, y)
auto_prepare(x + 1, y)
auto_prepare(x - 1, y + 1)
auto_prepare(x, y + 1)
auto_prepare(x + 1, y + 1)
#展示结果
site(int(op1), int(op2)).condition =numbers[detect(int(op1),int(op2))] + " "
auto(int(op1), int(op2))
printblocks()
end = time.time()
cost = end - start
print("游戏胜利!你的用时为" + str(int(round(cost,0))) + "秒")
#排行榜
if difficulty == str(1):
grades = []
grades.append((str(int(round(cost,0)))))
file_handle = open('eazy.txt',mode='a')
for num in grades:
file_handle.write("," + str(num))
file_handle.close()
file_handle = open('eazy.txt',mode='r')
contents = file_handle.readlines()
import re
print("简单模式最小用时为:" + str(min(map(int,re.findall("\d+",str(contents))))) + "秒")
if difficulty == str(2):
grades = []
grades.append(str(int(round(cost,0))))
file_handle = open('medium.txt',mode='a')
for num in grades:
file_handle.write("," + str(num))
file_handle.close()
file_handle = open('medium.txt',mode='r')
contents = file_handle.readlines()
import re
print("中等模式最小用时为:" + str(min(map(int, re.findall("\d+", str(contents)))))+ "秒")
if difficulty == str(3):
grades = []
grades.append(str(int(round(cost,0))))
file_handle = open('hard.txt',mode='a')
for num in grades:
file_handle.write("," + str(num))
file_handle.close()
file_handle = open('hard.txt',mode='r')
contents = file_handle.readlines()
import re
print("困难模式最小用时为:" + str(min(map(int, re.findall("\d+", str(contents)))))+ "秒")