1

I've been trying to program a simple Tic-Tac-Toe game for about 1 week now >.<'

Full source: http://pastebin.com/6dgjen9u

When testing my program in main():

I'm getting the error:

File "x:\programming\python\tac.py", line 64, in display_board
    print "\n\t", board[0], "  |", board[1], " |", board[2]
TypeError: 'int' object has no attribute '__getitem__'

The responsible functions here:

def display_board(board):
    """ Display game board on screen."""
    print "\n\t", board[0], "  |", board[1], " |", board[2]
    print "\t", "------------"
    print "\t", board[3], "  |", board[4], " |", board[5]
    print "\t", "------------"
    print "\t", board[6], "  |", board[7], " |", board[8], "\n"

def cpu_move(board, computer, human):
    """ Takes computer's move + places on board."""

    # make a copy of the board
    board = board[:]
    bestmoves = (0,2,6,8,4,3,5,1,7)

    # if computer can win, play that square:
    for move in legal_moves(board):
        board[move] = computer
        if winning_board(board) == computer:
            print "[debug] cpu win:", move
            return move
        # undo the move because it was empty before
        board[move] = EMPTY

    # if player can win, block that square:
    for move in legal_moves(board):
        board[move] = human
        if winning_board(board) == human:
            print "[debug] block human:", move
            return move
        board[move] = EMPTY

        # chose first best move that is legal
    for move in bestmoves:
        if move in legal_moves(board):
            board[move] = computer
            print "[debug] next best move:", move
            return move


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

def main():
    human, computer = go_first()
    board = new_board()
    display_board(board)
    turn = X
    while winning_board(board) == None:
        if human == turn:
            board = human_move(board, human)
            turn = change_turn(turn)
            display_board(board)
        else:
            board = cpu_move(board, computer, human)
            turn = change_turn(turn)
            display_board(board)

I have no idea what is causing the error because display_board(board) works fine for the human's move. It just fails when the computer takes its move.

3 Answers 3

1

cpu_move returns an integer here:

return move

Change it to return a list.

Sign up to request clarification or add additional context in comments.

2 Comments

That's right. I got confused about return values. I should've done return board
@user2071506 yes, the traceback you didn't post may already told you which line introduces that error
1

I don't see the human_move function, but anyways, your problem is that you're trying to index an integer.

When you return board, from cpu_move you are returning an int. Look at what you're returning: you're returning move, which is an integer, and doesn't have a __getitem__ method (which is the method that indexing calls).

Comments

1

It is possible that your cpu_move function return an int here:

for move in bestmoves:
    if move in legal_moves(board):
        board[move] = computer
        print "[debug] next best move:", move
        return move

This may be the problem as you are using this value for the display_board function:

board = cpu_move(board, computer, human)
turn = change_turn(turn)
display_board(board)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.