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