0

I am trying to write a tic-tac-toe game. In this game, I am trying to see if a specific space is empty. If it is, then the def is_board_empty should return True. However, it is returning False. I guess that it considers an empty space ' ' to be full, but I specifically told the program to consider something to be full if it has an 'X' or 'O' in it. I tried board_layout[position].strip(), but the IDE said "list indices must be integers or slices, not str". I thought board_layout[0] is a slice of a list.

board_layout = [
    ' ', ' ', ' ',
    ' ', ' ', ' ',
    ' ', ' ', ' '
]

class Increment:
    count = 0

    def __init__(self):
        Increment.count += 1

def human_turn():
    position = ''

    while True:
        try:
            position = int(input("Enter a position from 1-9\n"))
        except ValueError:
            continue
        if position > 9 or position < 1:
            continue
        if is_board_empty(position):
            print('That space is taken')
            continue
        else:
            position = int(position) - 1
            board_layout[position] = 'O'
            Increment.count += 1
            print(Increment.count)
            board()
            break
def computer_turn():
    while True:
        try:
            if Increment.count == 9:
                break
            position = random.randint(1, 9)
            if is_board_empty(position): # this line is the problem
                board_layout[position] = 'X'
                board()
                Increment.count += 1
                print(Increment.count)
                break
            else:
                continue
        except:
            continue


def is_board_empty(position):
    if 'X' or 'O' in board_layout[position]: # this returns False, even with an empty space
        return False
    else:
        return True

while is_winner() is None and Increment.count != 9: # this is the game loop
    human_turn()
    print('\n')
    computer_turn()
    print('\n')
James
  • 83
  • 1
  • 6

0 Answers0