2013-04-22 2 views
-1

파이썬에서 지뢰 찾기를 다시 만들려고합니다. 그리고 IDLE에서 코드를 실행하려고합니다. 이제 내 질문은 어떻게 작성해야합니까? 사용자가 좌표를 입력 할 때 위치를 어떻게 지정합니까? 위치가 "X"에서 빈 타일 인 "-"로 바뀝니다. 또한 내 코드를 개선하는 방법에 대한 조언도 신경 쓰지 않습니다. 감사!파이썬으로 지뢰 찾기를 만들고 도움이 필요합니다.

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") 
+2

'print' 명령문이 너무 많지 않게하려면 목록을 사용하는 것이 좋습니다 ('for i in lst_of_lsts : print i'). –

+3

이것에 대해 디버깅을하고 질문의 범위를 좁힐 수 있습니까 – Greg

+0

@ F3AR3DLEGEND 감사합니다. – AAA

답변

1

TypeError: cannot concatinate 'str' and 'int' objects이라고 오류가 있습니까?

당신은 당신은 introw 캐스팅하는 것을 잊었다이 라인

rowA = row + 1 
rowB = row - 1 

를 해결해야합니다.

실제로 코드를 보면 입력 할 때 int으로 전송해야한다고 생각합니다.

+0

나는 그 오류를 받고 있었고 내가 열을 입력 할 때 int로 형변환해야합니까? – AAA

+0

나는 당신의 충고를 들었고 결과적으로 나는이 오류를 얻었습니다. row = int (raw_input ('행 1 - 7의 행을 선택하십시오')) ValueError : 밑이 10 인 int() '' – AAA

+1

자릿수가 아닌 문자를 입력 했습니까? – elssar