首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试用python制作简单的扫雷游戏

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

Stack Overflow用户
提问于 2012-03-29 05:44:44
回答 1查看 8.9K关注 0票数 2

尝试用python制作简单的扫雷游戏,但有一个问题。我有一个7x7的x板,当玩家输入一行和一列时,它会删除这一列,并将其替换为-。我也试着让一个1出现,如果玩家猜测一个空格,但它不工作,我不知道为什么。相反,它会结束循环。下面是我所做的。这可能是一个简单的修复方法,但我看不出来。谢谢你的帮助!print(“欢迎使用扫雷程序/n”)

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-29 06:02:51

我认为您的问题出在循环条件中:

代码语言:javascript
复制
while row != numa and column != numb:

只有当行或列的中没有时,它才会进入循环。您需要将它们与或结合使用,而不是与和结合:

代码语言:javascript
复制
while row != numa or column != numb:

这样,它将进入循环,除非行和列都对应于mine所在的位置。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9916120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档