ShreddedPumpkin
ShreddedPumpkin

Reputation: 15

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()

Upvotes: 0

Views: 440

Answers (3)

Ateequr Rahman
Ateequr Rahman

Reputation: 13

You should first figure out the steps:

  1. Initialise the board( set board list, players' name, players' sign( X or O ) randomly
  2. Set a while loop and perform the following
  3. Ask player for a valid position
  4. Check if position is valid or not
  5. Set the board index to the player's sign
  6. Check if the player has won and break the loop
  7. Check if the board is filled and it is a tie and break if true
  8. Change player and go to step 3

Function to initialise board, check valid postition and draw the board

check if there is a winner or it is a draw

The gameplay

Upvotes: 1

ShreddedPumpkin
ShreddedPumpkin

Reputation: 15

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 True



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

Upvotes: 0

Almog-at-Nailo
Almog-at-Nailo

Reputation: 1182

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

Upvotes: 0

Related Questions