2011-05-23 34 views
2
I've pasted the code here: 

X = "X" 
O = "O" 
board = [] 
EMPTY = "" 

def instructions_prompt(): 
    print "\t\t\tNoughts and Crosses" 
    print \ 
     """Foolish human. Now that you've entered this Python program, 
     there is no exit. None! (At any point in the game if you feel 
     like you are intimidated by my presence, hit 0 to exit) 

     This challenge of wits will be one of many failures in your life. 

     The instructions are as follows: 

     1. Select a number from the following key: 

        0 | 1 | 2 
        --------- 
        3 | 4 | 5 
        --------- 
        6 | 7 | 8 

     to place X or O which is predetermined by whether or not you start 
     the game. 

     2. Try to save face while failing. And don't talk about Fight Club.""" 

def start_prompt(): 
    choice = raw_input ("Would you like to go first (Y/N)?") 
    lower = choice.lower() 
    if lower == "y": 
     human = X 
     computer = O 
     print "You're",human 
     print "I am",computer 
    elif lower == "n": 
     computer = X 
     human = O 
     print "You're",human 
     print "I'm in",computer 
    return human, computer 

def display_board (board): 
    print "",board[0],"|",board[1],"|",board[2],"" 
    print "---------" 
    print "",board[3],"|",board[4],"|",board[5],"" 
    print "---------" 
    print "",board[6],"|",board[7],"|",board[8],"" 

def turn_switcher (turn): 
    if turn == X: 
     return O 
    else: 
     return X 

def fresh_board(): 
    for i in range (9): 
     board.append (EMPTY) 
    return board 

def request_move(): 
    try: 
     square = int(raw_input("Where'd you like your square to be?")) 
     if square>8 or square<0: 
      print "This program can't proceed as that number is not on the board." 
     else: 
      return square 
    except: 
     print "That's not a number on the board. This program can't proceed." 

def legal_moves (board): 
    legal_moves = [] 
    for i in range (9): 
     if board [i] == EMPTY: 
      legal_moves.append(i) 
    return legal_moves 

def winner (board): 
    if board[0] == board [1] == board [2] != EMPTY: 
     winner = board [0] 
     return winner 
    elif board [0] == board [3] == board [6] != EMPTY: 
     winner = board [0] 
     return winner 
    elif board [0] == board [4] == board [8] != EMPTY: 
     winner = board [0] 
     return winner 
    elif board [1] == board [4] == board [7] != EMPTY: 
     winner = board [1] 
     return winner 
    elif board [2] == board [5] == board [8] != EMPTY: 
     winner = board [2] 
     return winner 
    elif board [2] == board [4] == board [6] != EMPTY: 
     winner = board [2] 
     return winner 
    elif board [6] == board [7] == board [8] != EMPTY: 
     winner = board [8] 
     return winner 
    elif EMPTY not in board: 
     return None 

def human_move (board, square): 
    legal = legal_moves(board) 
    if square not in legal: 
     print "This is not a legal move" 
     sys.exit() 
    else: 
     return square 

def computer_move (computer, board, human): 
    best = (4,0,8,2,6,1,3,5,7) 
    board = board [:] 
    legal = legal_moves(board) 
    for i in legal: 
     board[i] = computer 
     if winner(board) == computer: 
      return i 
     board = EMPTY 

#Stopping human from winning 
    for i in legal_moves(board): 
     board [i] = human 
     if winner(board) == human: 
      return i 

    for i in best: 
     if i in legal_moves(board): 
      return i 

def main(): 
    instructions_prompt() 
    human, computer = start_prompt() 
    turn = X 
    board = fresh_board() 
    request_move() 
    display_board (board) 

    while not winner(board): 
     if turn == human: 
      square = request_move() 
      move = human_move (board, square) 
      board[move] = human 
     else: 
      move = computer_move(computer, board, human) 
      board[move] = computer 
     display_board(board) 
     turn = turn_switcher(turn) 

main() 
raw_input ("Enter a key to end.") 

引用:IndexErrors在井字棋程序我有問題與

Traceback (most recent call last): 
    File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 152, in <module> 
    main() 
    File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 147, in main 
    move = computer_move(computer, board, human) 
    File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 118, in computer_move 
board[i] = computer 
TypeError: 'str' object does not support item assignment 
+0

儘量讓問題自成一體。它使答覆者更容易,即使網絡上的其他網站在一夜之間消失,它也應該保持有用!_ – paxdiablo 2011-05-23 04:32:19

回答

5

你有你的論點混了功能computer_move。在第113行,參數的順序爲computerboard,human。但是,在線147上,其中調用computer_move,順序爲board,computer,human

Python給你帶來了令人困惑的索引錯誤,因爲Python字符串實際上只是字符列表。例如。

>>> "foo"[2] == "o" 
True 

更新:你得到,因爲computer_move最後一行的這種新的錯誤。它應該是board[i] = EMPTY而不是board = EMPTY

+1

這很有趣,不知道。但是,當對參數進行更改時,會出現相同的片段並出現錯誤。我已將這些更改添加到原始問題中。感謝您再次查看1 – Louis93 2011-05-23 07:09:11

+0

@ user765409請參閱我的答案更新。謝謝。 – 2011-05-23 17:18:18