python - 尝试用python制作简单的扫雷游戏

标签 python

尝试用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/

相关文章:

python - 护士调度问题或工具,在某些日子添加不同的轮类时间

python - 用于反转字符串中单词顺序的正则表达式

python - 如何使用 python 将用户添加到 keystoneclient api v3 中的 openstack 项目

python - 如何将 UTC 时间戳字符串转换为 pandas 日期时间?

python - 如何在 django 的两边设置多对多字段空白=真

python - matplotlib 绘制多个散点图,每个散点图由不同的第三变量着色

python - 在 Python 中通过服务器与可执行文件交互?

Python 3 类型错误 : utf_8_encode() must be str not bytes using Requests

python - Pycharm的代码风格检查: ignore/switch off specific rules

python - python是如何实现以下数据格式转换的?