-2

I am figuring out how to tie the game. I'm having difficulties. Can anyone provide a code to replace it or fix it?. Thank You.

I programmed a tic tac toe game in python and it works perfectly fine and everything. So i was wondering, can you somehow program the game to say, "Do you wish to play again?" after the computer wins or if you win?

In my game, once the computer wins or you win, it doesn't say anything and i want the game to ask the player if they want to play again.

Thank you.

import os
import time
import random

# Define the board
board = [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]


# Print the header
def print_header():
    print("""                                                                         
 _____  _  ____     _____  ____  ____     _____  ____  _____                 _________   _______   _______   
/__ __\/ \/   _\   /__ __\/  _ \/   _\   /__ __\/  _ \/  __/    1 | 2 | 3    |___    /  |      /  |  ____|
  / \  | ||  / _____ / \  | / \||  / _____ / \  | / \||  \      4 | 5 | 6      /   /    |   _/    | |   ___ 
  | |  | ||  \_\____\| |  | |-|||  \_\____\| |  | \_/||  /_     7 | 8 | 9    /   /____  |     \   | |___\ |
  \_/  \_/\____/     \_/  \_/ \|\____/     \_/  \____/\____\                 |_______|  |______|  |_______|

 To play Tic-Tac-Toe, you need to get three in a row...
 Your choices are defined, they must be from 1 to 9...

""")


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


def is_winner(board, player):
    if (board[1] == player and board[2] == player and board[3] == player) or \
            (board[4] == player and board[5] == player and board[6] == player) or \
            (board[7] == player and board[8] == player and board[9] == player) or \
            (board[1] == player and board[4] == player and board[7] == player) or \
            (board[2] == player and board[5] == player and board[8] == player) or \
            (board[3] == player and board[6] == player and board[9] == player) or \
            (board[1] == player and board[5] == player and board[9] == player) or \
            (board[3] == player and board[5] == player and board[7] == player):
        return True
    else:
        return False


def is_board_full(board):
    count = 0
    for a in range(9):
        if board[a] == "X" or board[a] == "O":
            count += 1
            return False
        if count == 9:
            print("The game ends in a Tie\n")
            return True


while True:
    os.system("clear")
    print_header()
    print_board()

    choice = input("Please choose an empty space for X. ")
    choice = int(choice)

    if board[choice] == " ":
        board[choice] = "X"
    else:
        print("Sorry, that space has been checked!")
        time.sleep(1)

    if is_winner(board, "X"):
        os.system("clear")
        print_header()
        print_board()
        print("X wins! Congratulations")
        break

    os.system("clear")
    print_header()
    print_board()

    if is_board_full(board):
        print("Tie!")
        break

    choice = input("Please choose an empty space for O.")
    choice = int(choice)

    if board[choice] == " ":
        board[choice] = "O"

    else:
        print("Sorry, that space has been checked!")
        time.sleep(1)

    if is_winner(board, "O"):
        os.system("clear")
        print_header()
        print_board()
        print("O win! Congratulations")
        break
  • 1
    Hi Zelandini. Please post the code you have so far. – Peter David Carter Nov 12 '17 at 11:01
  • Is this text based or GUI based? You can either prompt the user with a pop-up or a text question once the game is over. Also, what problem do you have with forcing a tie? I guess you check for win condition for each player once their turn is over. Count the turns and force a tie once all 9 cells are filled, or even better, once there are no more plays that can cause a win. – Tgilgul Nov 12 '17 at 11:09
  • Hi people, thank you to respond my question. I already sent the code. – Zelandini Borromeu Guterres Nov 12 '17 at 11:26

1 Answers1

-1

Found the problem(s) in your "tie" code. The issue is with the is_board_full function. This works for me:

def is_board_full(board):
    count = 0
    for a in range(1,10):
        if board[a] == "X" or board[a] == "O":
            count += 1
    if count == 9:
        print("The game ends in a Tie\n")
        return True
    return False

The problem is that you set the board from index 1 to 9. range(9) returns the numbers 0 to 8 so you never check the last cell. Also, not sure why you put the "return false" statement there.

Now for asking the user if they want another game, just change the while condition to check a variable, let's say you call it "play" after the game ends simply add this code or anything similar:

play = False
var = raw_input("If you want to play again press 'y'. If not press any other key.")
if var == 'y':
    play = True

Make sure to add this as a function and use it every place where the game ends in the code.

See more about Reading input in Python

Tgilgul
  • 1,614
  • 1
  • 20
  • 35