2017-12-21 41 views
1

아래 코드는 Tic tac toe 게임의 코드입니다 (Player1과 Player2는 사람입니다). "# 플레이어 1 재생"및 "# 플레이어 2 재생"섹션의 if else 문에 문제가 있습니다. 정확하게 말하면, 컴퓨터는 항상 모든 상자가 이미 1,2,3,4,5,6,7,8,9 이외의 다른 값으로 채워져 있다고 생각하고 "그 셀은 이미 표시되어 있습니다. 다른 셀을 시도하십시오 "는 중첩 된 else 문에서 정의됩니다. 누군가이 문제를 해결하는 방법을 알려줄 수 있습니까?Tic tac toe 프로그램 (Python)의 오류

board = [0,1,2,3,4,5,6,7,8,9] 
print type(board[1]) 
def board_invoke(): 
print "| ",board[1], " | ", board[2], " | ", board[3], " | ", "\n", "-------------------", "\n", "| ", board[4], " | ",board[5], " | ", board[6], " | ", "\n", "-------------------", "\n", "| ", board[7], " | ",board[8], " | ", board[9], " | " 

def game_start(): 
Player1= raw_input("Select Player 1 between X or O : ") 
if Player1 not in ('X','x','O','o'): 
    print "That is not an expected player" 
    game_start() 
else: 
    print "\nSince you have selected Player 1 as %s" %Player1 
    print "\nThe Player 2 is assigned :", 
    if Player1 in ('X','x'): 
     Player2 = ("O") 
    else: 
     Player2 = ("X") 
    print Player2 
    print "\nThe game Starts now....\n" 
    board_invoke() 
    while (1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9) in board: 

     # Winning Condition 
     if board[1]==board[2]==board[3]==Player1: 
      print Player1," wins" 
      break 
     elif board[4]==board[5]==board[6]==Player1: 
      print Player1, " wins" 
      break 
     elif board[7]==board[8]==board[9]==Player1: 
      print Player1, " wins" 
      break 
     elif board[1]==board[2]==board[3]==Player2: 
      print Player2, " wins" 
      break 
     elif board[4]==board[5]==board[6]==Player2: 
      print Player2, " wins" 
      break 
     elif board[7]==board[8]==board[9]==Player2: 
      print Player2, " wins" 
      break 
     elif board[1]==board[5]==board[9]==Player1: 
      print Player1, " wins" 
      break 
     elif board[3]==board[5]==board[7]==Player1: 
      print Player1, " wins" 
      break 
     elif board[1]==board[5]==board[9]==Player2: 
      print Player2, " wins" 
      break 
     elif board[3]==board[5]==board[7]==Player2: 
      print Player2, " wins" 
      break 
     elif board[1]==board[4]==board[7]==Player1: 
      print Player1, " wins" 
      break 
     elif board[2]==board[5]==board[8]==Player1: 
      print Player1, " wins" 
      break 
     elif board[3]==board[6]==board[9]==Player1: 
      print Player1, " wins" 
      break 
     elif board[1]==board[4]==board[7]==Player2: 
      print Player2, " wins" 
      break 
     elif board[2]==board[5]==board[8]==Player2: 
      print Player2, " wins" 
      break 
     elif board[3]==board[6]==board[9]==Player2: 
      print Player2, " wins" 
      break 

     # Player 1 Plays now 

     Cell_no = raw_input("Player 1 : Please select a number you want to mark....") 
     Cell_no = int(Cell_no) 

     if Cell_no not in board: 
      print "Please enter a cell number within the scope of available cells" 
     else: 
      if 'X' or 'x' or 'O' or 'o' in board[Cell_no]: 
       print "That cell is already marked. Please try another cell" 
       continue 
      else: 
       board[Cell_no] = Player1 
       board_invoke() 

     # Player 2 Plays now 

     Cell_no = raw_input("Player 2 : Please select a number you want to mark....") 
     Cell_no = int(Cell_no) 

     if Cell_no not in board: 
      print "Please enter a cell number within the scope of available cells" 
     else: 
      if 'X' or 'x' or 'O' or 'o' in board[Cell_no]: 
       print "That cell is already marked. Please try another cell" 
       continue 
      else: 
       board[Cell_no] = Player2 
       board_invoke() 

    print "Do you want to play again ?" 
    user_decision = raw_input("Please type Yes or No : ") 
    if user_decision == ('YES' or 'Yes' or 'yes'): 
     game_start() 
    else: 
     print "Ok. I take it that we will wrap up !" 
     print "See you again !" 



game_start() 

답변

1

if 문이 완전히 올바르지 않습니다. 당신은 평가하고있다 : 항상 true입니다

else: 
if 'X' or 'x' or 'O' or 'o' in board[Cell_no]: 
    print "That cell is already marked. Please try another cell" 

. or 키워드는 각 비교를 논리적으로 결합합니다. 첫 x 째 비교 명령.은 단순히 'X'이되며, 널 또는 0이 아니므로 항상 참입니다. 첫 번째 문이 참이므로 if 문 전체가 true입니다.

나는 프로그래밍을 배우기 시작한 사람들에게 이것을 아주 자주 본다. If 문은 인간의 문장과 똑같이 작성할 수 없습니다. 모든 단일 문자를 변수와 비교해야합니다. 여기 는 간단 문제와 솔루션 (특정 꽤 추한를 파이썬되지 않음) :

else: 
if 'X' == board[Cell_no] or 'x' == board[Cell_no] or 'O' == board[Cell_no] or 'o' == board[Cell_no]: 
     print "That cell is already marked. Please try another cell" 

더 좋은 방법하지만 주위에 그 수표를 돌려 목록과 비교하는 것입니다 :

else: 
if board[Cell_no] in ['X','x', 'O', 'o']: 
     print "That cell is already marked. Please try another cell"