'''
Created on Oct 12
@author: Bhargav Modak
'''
# A dictionary to store the board
theBoard = {'7': ' ', '8': ' ', '9': ' ',
'4': ' ', '5': ' ', '6': ' ',
'1': ' ', '2': ' ', '3': ' '}
boardKeys = []
for key in theBoard:
boardKeys.append(key)
# Function to show board
def ShowBoard(board):
print("\n")
print(" " + board['7'] + " | " + board['8'] + " | " + board['9'])
print(" - + - + -")
print(" " + board['4'] + " | " + board['5'] + " | " + board['6'])
print(" - + - + -")
print(" " + board['1'] + " | " + board['2'] + " | " + board['3'])
print("\n")
# function for Game
def game():
turn = 'X'
count = 0
print("\n----==== Tic Tac Toe Game ====----\n")
print(" The Board locations are as follows.")
print("\n")
print(" " + '7' + " | " + '8' + " | " + '9')
print(" - + - + -")
print(" " + '4' + " | " + '5' + " | " + '6')
print(" - + - + -")
print(" " + '1' + " | " + '2' + " | " + '3')
print("\n")
print("Let's begin:")
for i in range(10):
ShowBoard(theBoard)
print("It is " + turn + "'s turn. Please specify where to input.")
flag = True
while flag:
try:
loc = 20
while loc > 9 or loc < 1:
loc = int(input("\t"))
if loc > 9 or loc < 1:
print("\nNot a Valid location.")
except ValueError:
print("\nNot a Valid location.")
continue
loc = str(loc)
if theBoard[loc] == ' ':
theBoard[loc] = turn
count += 1
flag = False
else:
print("\n================================\n\
Sorry, the location is occupied.\n\
================================")
print("\n\nReminder :")
print("===============")
print(" " + '7' + " | " + '8' + " | " + '9')
print(" - + - + -")
print(" " + '4' + " | " + '5' + " | " + '6')
print(" - + - + -")
print(" " + '1' + " | " + '2' + " | " + '3')
print("===============")
print("\n\nReminder :\n")
print("===============")
print(" " + '7' + " | " + '8' + " | " + '9')
print(" - + - + -")
print(" " + '4' + " | " + '5' + " | " + '6')
print(" - + - + -")
print(" " + '1' + " | " + '2' + " | " + '3')
print("===============")
if count >= 5:
if((theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ') or
(theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ') or
(theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ') or
(theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ') or
(theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ') or
(theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ') or
(theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ') or
(theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ')):
ShowBoard(theBoard)
print("\n----------------------------------------\n")
print("\n| Game Over. [ " + turn + " ] Has won. |\n")
print("\n----------------------------------------\n")
break
if count >= 9:
print("\n----------------------------------------\n")
print("\n Game Over. Match Tied.\n")
print("\n----------------------------------------\n")
break
if turn == 'X':
turn = 'O'
else:
turn = 'X'
contd = input("Do you want to continue the game? (Y/N)")
if contd == 'Y' or contd == 'y':
for key in boardKeys:
theBoard[key] = ' '
game()
if __name__ == '__main__':
game()