Search code examples
pythontic-tac-toe

TicTacToe - How do i stop the game if theres a winner,


So this is how the program will received the data

 x_data = []
    def xuser_input():
        while True:
            x = As("Player X, Please enter a number to place: ",int)
            if (x > 10) or (x < 0):
                print("Input must be Bigger than 0 and Smaller than 9")
                continue
            try:
                board[x] = "X"
                x_data.append(x)
            except (IndexError):
                print("Invalid input")
                continue
            t_Board()
            break

There will be another for Y as well. This is the tictactoe board

def t_Board():
    print(f"| {board[0]} | {board[1]} | {board[2]} |\n_____________")
    print(f"| {board[3]} | {board[4]} | {board[5]} |\n_____________")
    print(f"| {board[6]} | {board[7]} | {board[8]} |")

This will stop the game if this condition is met which is the wining formula.

    def stops_board():
        if (board[0] == board[1] == board[2]) or (board[3] == board[4] == board[5]) or (
        board[6] == board[7] == board[8]) or (board[0] == board[3] == board[6]) or (
        board[1] == board[4] == board[7]) or (board[2] == board[5] == board[8]) or (
        board[0] == board[4] == 
        board[8]) or (board[2] == board[4] == board[6]):
            return False

For now this is how i ask the data input and check if theres a winning solution

 while True:
        xuser_input()
        stops_board()
        yuser_input()
        stops_board()

Solution

  • as i see it, stops_board() should return True if the game should stop, and False if it should continue. correct? if so, you could use:

     while True:
            xuser_input()
            if stops_board(): break
            yuser_input()
            if stops_board(): break