Tic Tac Toe checking winner function

Issue

This Content is from Stack Overflow. Question asked by Joelinton

i have built Tic Tac Toe project.Function check_win() i don’t understand well why it should be implemented like that give me other ways if better , check_tie() function don’t work also well for example when we got the winner and its not available “-” it prints me tie,and suggest me what should i add more functions to make project better or something else.Here’s code if you don’t understand well what i am saying just check the code.

import random
print("Welcome to our Tic Tac Toe Game!")
board = ['-','-','-',
        '-','-','-',
        '-','-','-']
#Create Board
def printBoard():
    print(board[0] + "|" + board[1] + "|" + board[2] + "|")
    print("-----")
    print(board[3] + "|" + board[4] + "|" + board[5] + "|")
    print("-----")
    print(board[6] + "|" + board[7] + "|" + board[8] + "|")

winner = None
tie = None
Gamerunning = False 
current_player = "X"

#Winner variable False
#Check if tie variable False
#Game is RUnning False

#user input
def user_input():
    user_input = int(input("Enter a place between 0-8: ")) #5
    if board[user_input] == "-":
        board[user_input] = current_player
    else:
        print("This place is already taken!")
#computer input
def computer_input():
    while current_player == "Y":
        random_place = random.randint(0,8)
        if board[random_place] == "-":
            board[random_place] = "Y"
            switchplayer()
def check_vertical():
    global winner 
    if board[0] == board[1] == board[2] != "-":
        winner = board[0]
        return True 
    elif board[3] == board[4] == board[5] != "-":
        winner = board[3]
        return True 
    elif board[6] == board[7] == board[8] != "-":
        winner = board[6]
        return True 
def check_horizontal():
    global winner 
    if board[0] == board[3] == board[6] != "-":
        winner = board[0]
        return True 
    elif board[1] == board[4] == board[7] != "-":
        winner = board[1]
        return True 
    elif board[2] == board[5] == board[8] != "-":
        winner = board[2]
        return True 



def check_sideways():
    global winner 
    if board[0] == board[4] == board[8] != "-":
        winner = board[0]
        return True 
    elif board[2] == board[4] == board[6] != "-":
        winner = board[2]
        return True 
def check_win():
    global Gameisrunning
    if check_vertical():
        printBoard()
        print(f"The winner is" , {winner})
        Gameisrunning = False 
    elif check_horizontal():
        printBoard()
        print(f"The winner is" , {winner})
        Gameisrunning = False 
    elif check_sideways():
        printBoard()
        print(f"The winner is" , {winner})
        Gameisrunning = False 
    


def check_tie():
    global tie
    global Gamerunning
    if "-" not in board and Gameisrunning != False:
        print("Tie")
        tie = True 


def switchplayer():
    global current_player 
    if current_player == "X":
        current_player = "Y"
    else:
        current_player = "X"

    
while winner != True or tie != True:
    printBoard()
    user_input()
    switchplayer()
    computer_input()
    check_horizontal()
    check_sideways()
    check_vertical()
    check_win()
    check_tie()



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?