-1

I am attempting to recreate minesweeper in Python and as I attempt to run the code in IDLE My question is now that how do I make it to where when the user inputs their coordinates the location changes from the "X" to "-" which is a blank tile. Also I wouldn't mind any tips of how to improve my code. Thanks!

import random

Field = [
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 (Feild)
print("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 (Field)
    print("Enter a row")
    row = raw_input('Please select a row from rows 1 - 7: ')
    column = raw_input('Please enter a column from columns 1 - 7: ')
    columA = int(column) + 1
    columB = int(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 (Field)
print("Game over")
AAA
  • 39
  • 2
  • 6

1 Answers1

1

Do you get a error saying TypeError: cannot concatinate 'str' and 'int' objects?

You need to fix these lines

rowA = row + 1
rowB = row - 1

You forgot to cast row to int.

Actually, looking at your code, I think you should cast to int when you take input.

row = int(raw_input())
elssar
  • 5,651
  • 7
  • 46
  • 71
  • I was getting that error and should I cast to int when i take input for the column as well? – AAA Apr 22 '13 at 02:28
  • I took your advice and as a result I got this error in return row = int(raw_input('Please select a row from rows 1 - 7: ')) ValueError: invalid literal for int() with base 10: '' – AAA Apr 22 '13 at 03:02
  • 1
    Did you input a non digit character? – elssar Apr 22 '13 at 05:25
  • 1
    The error itself is pretty simple; you should be able to debug this yourself by looking at the traceback... – Rushy Panchal Apr 22 '13 at 14:49