试图在python中制作简单的扫雷游戏

时间:2012-03-28 21:44:44

标签: python

尝试在python中制作简单的扫雷游戏,但有一个问题。 我有一个7x7的x板,当玩家输入一行和一列时,它会删除该列 并用 - 替换它。如果球员猜出一个空位,我也试图让1出现,但它不起作用,我无法弄清楚为什么。相反,它结束了循环。以下是我的所作所为。它可能是一个简单的修复,但我无法看到它。谢谢您的帮助! 打印(“欢迎来到Minesweeper / 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")

1 个答案:

答案 0 :(得分:2)

我认为你的问题是在循环条件下:

while row != numa and column != numb:

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

结合使用
while row != numa or column != numb:

这样它就会进入循环,除非行和列都对应矿井的位置。