尝试用Python制作简单的扫雷游戏,但有一个问题。 我有一个 7x7 的 x 棋盘,当玩家输入行和列时,它会删除该列 并将其替换为 -。我还尝试过,如果玩家猜出一个空格,就会出现 1,但它不起作用,我不明白为什么。相反,它结束了循环。以下是我所做的。这可能是一个简单的修复,但我看不到它。谢谢您的帮助! print("欢迎来到扫雷/n")
import random
LA=["X","X","X","X","X","X","X"]
LB=["X","X","X","X","X","X","X"]
LC=["X","X","X","X","X","X","X"]
LD=["X","X","X","X","X","X","X"]
LE=["X","X","X","X","X","X","X"]
LF=["X","X","X","X","X","X","X"]
LG=["X","X","X","X","X","X","X"]
print("", LA, "\n" , LB, "\n" , LC, "\n" , LD, "\n" , LE, "\n" ,
LF, "\n" , LG, "\n")
print("\n select row starting from top = 1 and column from left = 0")
numa = random.randint(1,7)
numb = random.randint(0,6)
MINE = "O"
row=9
column = 9
one = "1"
blank = "-"
while row != numa and column != numb:
print("", LA, "\n" , LB, "\n" , LC, "\n" , LD, "\n" , LE, "\n" ,
LF, "\n" , LG, "\n")
#cheeter
print(numa , "" , numb)
row = int(input("\nEnter row"))
column = int(input("\nEnter column"))
columA = column + 1
columB = column - 1
rowA = row + 1
rowB = row - 1
if rowA == numa and column == numb:
if row ==1:
del LA[column]
LA.insert(column, one)
if row ==2:
del LB[column]
LB.insert(column, one)
if row ==3:
del LC[column]
LC.insert(column, one)
if row ==4:
del LD[column]
LD.insert(column, one)
if row ==5:
del LE[column]
LE.insert(column, one)
if row ==6:
del LF[column]
LF.insert(column, one)
if row ==7:
del LG[column]
LG.insert(column, one)
elif rowB == numa and column == numb:
if row ==1:
del LA[column]
LA.insert(column, one)
if row ==2:
del LB[column]
LB.insert(column, one)
if row ==3:
del LC[column]
LC.insert(column, one)
if row ==4:
del LD[column]
LD.insert(column, one)
if row ==5:
del LE[column]
LE.insert(column, one)
if row ==6:
del LF[column]
LF.insert(column, one)
if row ==7:
del LG[column]
LG.insert(column, one)
elif row == numa and columA == numb:
if row ==1:
del LA[column]
LA.insert(column, one)
if row ==2:
del LB[column]
LB.insert(column, one)
if row ==3:
del LC[column]
LC.insert(column, one)
if row ==4:
del LD[column]
LD.insert(column, one)
if row ==5:
del LE[column]
LE.insert(column, one)
if row ==6:
del LF[column]
LF.insert(column, one)
if row ==7:
del LG[column]
LG.insert(column, one)
elif row == numa and columB == numb:
if row ==1:
del LA[column]
LA.insert(column, one)
if row ==2:
del LB[column]
LB.insert(column, one)
if row ==3:
del LC[column]
LC.insert(column, one)
if row ==4:
del LD[column]
LD.insert(column, one)
if row ==5:
del LE[column]
LE.insert(column, one)
if row ==6:
del LF[column]
LF.insert(column, one)
if row ==7:
del LG[column]
LG.insert(column, one)
else:
if row ==1:
del LA[column]
LA.insert(column, blank)
if row ==2:
del LB[column]
LB.insert(column, blank)
if row ==3:
del LC[column]
LC.insert(column, blank)
if row ==4:
del LD[column]
LD.insert(column, blank)
if row ==5:
del LE[column]
LE.insert(column, blank)
if row ==6:
del LF[column]
LF.insert(column, blank)
if row ==7:
del LG[column]
LG.insert(column, blank)
if row ==1:
del LA[column]
LA.insert(column, MINE)
if row ==2:
del LB[column]
LB.insert(column, MINE)
if row ==3:
del LC[column]
LC.insert(column, MINE)
if row ==4:
del LD[column]
LD.insert(column, MINE)
if row ==5:
del LE[column]
LE.insert(column, MINE)
if row ==6:
del LF[column]
LF.insert(column, MINE)
if row ==7:
del LG[column]
LG.insert(column, MINE)
print("", LA, "\n" , LB, "\n" , LC, "\n" , LD, "\n" , LE, "\n" ,
LF, "\n" , LG, "\n")
print("Game over")
input("Press enter to quit")
最佳答案
我认为你的问题出在循环条件中:
while row != numa and column != numb:
只有当行或列中没有我的时候才会进入循环。您需要将它们与 or 结合起来,而不是与 and 结合起来:
while row != numa or column != numb:
这样就会进入循环,除非该行和列都对应于地雷所在的位置。
关于python - 尝试用python制作简单的扫雷游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9916120/