Try to code a simple Tic Tac Toe from a Udemy course. What did I do wrong?

Question:

Below is the code I used for my simple Tic Tac Toe game. I receive the following error I have no idea how to get around. This program does work in Jupyter notebook, but when I try to run the script in VScode, the error appears. I need guidance on how I can fix this error.

***THE ERROR IS AS FOLLOWS: ***


Traceback (most recent call last):
  File "x:PythonPython_BootcampComplete-Python-3-Bootcamp-master4-Milestone Project - 1TIC_TAC_TOE.py", line 105, in <module>   
    display_board(board)
  File "x:PythonPython_BootcampComplete-Python-3-Bootcamp-master4-Milestone Project - 1TIC_TAC_TOE.py", line 8, in display_board
    print(board[1] + '|' + board[2] + '|' + board[3])
          ~~~~~^^^
IndexError: list index out of range

TIC TAC TOE GAME

`
import random
`

Display the board for the game

`def display_board(board):
    # print('n'*100) # Lets you only see one version of the board
    print(board[1] + '|' + board[2] + '|' + board[3])
    print('-----')
    print(board[4] + '|' + board[5] + '|' + board[6])
    print('-----')
    print(board[7] + '|' + board[8] + '|' + board[9])`

Choose whether player 1 is X or O

`def player_input():
    marker = ''

    while marker != 'X' and marker != 'O':
        marker = input('Player 1 choose X or O: ')
        
        player1 = marker.upper()

        if player1 == 'X':
            player2 = 'O'
        else:
            player2 = 'X'

    return(player1, player2)`

Takes a position on the board and puts a marker on position

`def place_marker(board,marker,position):
    board[position] = marker`

Check to see if win

`def win_check(board, mark):
    return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the bottom
    (board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
    (board[1] == mark and board[2] == mark and board[3] == mark) or # across the top
    (board[7] == mark and board[4] == mark and board[1] == mark) or # down the left column
    (board[2] == mark and board[5] == mark and board[8] == mark) or # down the middle
    (board[3] == mark and board[6] == mark and board[9] == mark) or # down the right side
    (board[3] == mark and board[5] == mark and board[7] == mark) or # diagonal
    (board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal`

Random flip to see if Player 1 or Player 2 goes first

`def choose_first():
    flip = random.randint(0,1)

    if flip == 0:
        return ('Player 1')
    else:
        return ('Player 2')`

Check to see if position freely available

`def space_check(board, position):
    return board[position] == ' ' # If space is empty the return value will be True`

Check to see if the board is full

`def full_board_check(board):
    for i in range(1,10):
        if space_check(board,i):
            return False # Fales meaning the space is empty of marker
        else:
            return True # True meaning the board is full`

Function for player to choose their next position on the board

`def player_choice(board):
    position = 0

    while position not in [1,2,3,4,5,6,7,8,9]:
        position = int(input('Choose your position: '))

    return position

def replay():
    
    choice = input('Do you want to play again?')

    return choice == 'Yes'`

GAME SETUP

`print('Welcome to Tic Tac Toe!')

while True:
    board = [' '*10]
    player1_marker,player2_marker = player_input()
    turn = choose_first()
    print(turn + ' will go first')
    game_on = ''
    
    play_game = input('Are you ready to play Yes or No?: ')

    if play_game.lower()[0] == 'y':
        game_on == True
    else:
        game_on == False

        # Player 1 Turn
    if turn == 'Player 1':

        # Show board

        display_board(board)

        # Choose a position

        position = player_choice(board)

        # Place marker on choosen position

        place_marker(board,player1_marker,position)

        # Check if they won

        if win_check(board,player1_marker) == True:
            display_board(board)
            print('Player 1 has won the game!')
            game_on = False

        # Check to see if Tie
        else: 
            if full_board_check(board):
                display_board(board)
                print('TIE GAME')
                break

    # If there's no tie turn to Player 2        
    else:
        turn = 'Player 2'

        # Player2's turn.
    if turn == 'Player 2':

        # Show board
        # display_board(board)

        # Show position
        position = player_choice(board)

        #Place marker on position
        place_marker(board,player2_marker,position)

        # Check to see if win
        if win_check(board,player2_marker) == True:
            display_board(board)
            print('Player 2 has won the game!')
            game_on = False
        
        # Check to see if Tie
        else:
            full_board_check(board)
            display_board(board)
            print('TIE GAME')
            break
    else:
        turn = 'Player 1'
    
    # If players do not want to play again, quit
    if not replay():
        break`

I’ve tried to comment out different lines of code to test some of the board configurations. I can get the board to show, but once I try to play a full game I get an error.

Answers:

You have init

board = [' '*10]

so board is a list of only one element (one string with 10 spaces). So index 1 does not exists.

maybee you wanted to write:

board = 10*[' ']

so you have a list with 10 elements ‘ ‘ (10 times one blank/space).

I tested it and it works with your code, and I managed to play my first move.

Answered By: Malo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.