简体   繁体   中英

How do I compare an object in python to an integer

This is everything I wrote. I know that Increment.count is equal to 9 as I have put print statements and I have checked everything, but when I set Increment.count equal to 9, it is not ending the game for some reason. The way to end the game is to see if every space on the board is full and no one won yet.

import random

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


class Increment:
    count = 0

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


def board():
    print(board_layout[0] + '|' + board_layout[1] + '|' + board_layout[2])
    print('-' + '+' + '-' + '+' + '-')
    print(board_layout[3] + '|' + board_layout[4] + '|' + board_layout[5])
    print('-' + '+' + '-' + '+' + '-')
    print(board_layout[6] + '|' + board_layout[7] + '|' + board_layout[8])


def human_turn():
    position = ' '
    position = input('Enter a position from 1-9\n')
    position = int(position) - 1
    board_layout[position] = 'O'
    Increment.count += 1
    board()


def computer_turn():
    while True:
        try:
            position = random.randint(1, 9)
            if is_board_empty(position):
                board_layout[position] = 'X'
                board()
                Increment.count += 1
                break
        except:
            pass


def is_board_empty(position):
    return board_layout[position] == ' '


def is_winner():
    if board_layout[0] != ' ' and board_layout[1] != ' ' and board_layout[2] != ' ' and \
            board_layout[0] == board_layout[1] == board_layout[2]:
        board()
        print(board_layout[0] + ' won!')
        return board_layout[0]
    elif board_layout[3] != ' ' and board_layout[4] != ' ' and board_layout[5] != ' ' and \
            board_layout[3] == board_layout[4] == board_layout[5]:
        board()
        print(board_layout[3] + ' won!')
        return board_layout[3]
    elif board_layout[6] != ' ' and board_layout[7] != ' ' and board_layout[8] != ' ' and \
            board_layout[6] == board_layout[7] == board_layout[8]:
        board()
        print(board_layout[6] + ' won!')
        return board_layout[6]
    elif board_layout[0] != ' ' and board_layout[3] != ' ' and board_layout[6] != ' ' and \
            board_layout[0] == board_layout[3] == board_layout[6]:
        board()
        print(board_layout[0] + ' won!')
        return board_layout[0]
    elif board_layout[1] != ' ' and board_layout[4] != ' ' and board_layout[7] != ' ' and \
            board_layout[1] == board_layout[4] == board_layout[7]:
        board()
        print(board_layout[1] + ' won!')
        return board_layout[1]
    elif board_layout[2] != ' ' and board_layout[5] != ' ' and board_layout[8] != ' ' and \
            board_layout[2] == board_layout[5] == board_layout[8]:
        board()
        print(board_layout[2] + ' won!')
        return board_layout[2]
    elif board_layout[0] != ' ' and board_layout[4] != ' ' and board_layout[8] != ' ' and \
            board_layout[0] == board_layout[4] == board_layout[8]:
        board()
        print(board_layout[0] + ' won!')
        return board_layout[0]
    elif board_layout[2] != ' ' and board_layout[4] != ' ' and board_layout[6] != ' ' and \
            board_layout[2] == board_layout[4] == board_layout[6]:
        board()
        print(board_layout[2] + ' won!')
        return board_layout[2]
    elif Increment.count == 9:
        print('draw')


while is_winner() is None and Increment.count != 9:
    human_turn()
    print('\n')
    computer_turn()
    print('\n')


if Increment.count == 9 and is_winner is None:
    print('draw')

It seems like while the computer is taking its last turn the board is full and the computer goes into a forever loop so we will have to change the computer code to fix it.

replace this

def computer_turn():
    while True:
        try:
            position = random.randint(1, 9)
            if is_board_empty(position):
                board_layout[position] = 'X'
                board()
                Increment.count += 1
                break
        except:
            pass

with

def computer_turn():
    while True:
        try:
            if Increment.count == 9:  # If the board is already full
                break
            position = random.randint(1, 9)
            if is_board_empty(position):
                board_layout[position] = 'X'
                board()
                Increment.count += 1
                break
        except:
            pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM