Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 30 additions & 43 deletions Tic-Tac-Toe-Game/Tic-Tac-Toe-Game.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,38 @@
'4': ' ' , '5': ' ' , '6': ' ' ,
'1': ' ' , '2': ' ' , '3': ' ' }

board_keys = []

for key in theBoard:
board_keys.append(key)
board_keys = [key for key in theBoard]

''' We will have to print the updated board after every move in the game and
thus we will make a function in which we'll define the printBoard function
so that we can easily print the board everytime by calling this function. '''

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

metrix = f"""
board['7'] + '|' + board['8'] + '|' + board['9']
-+-+-
board['4'] + '|' + board['5'] + '|' + board['6']
-+-+-
board['1'] + '|' + board['2'] + '|' + board['3']
""""
print(metrix)

#lets define a win function in order not to write it all over again when checking who won.
def win_message(winner):
printBoard(theBoard)
print("\nGame Over.\n")
print(f" **** {winner} won. ****")

# Now we'll write the main function which has all the gameplay functionality.
def game():

turn = 'X'
count = 0


for i in range(10):
printBoard(theBoard)
print("It's your turn," + turn + ".Move to which place?")

move = input()
move = input(f"It's your turn, {turn}. Move to which place?")

if theBoard[move] == ' ':
theBoard[move] = turn
Expand All @@ -48,64 +52,47 @@ def game():
# Now we will check if player X or O has won,for every move after 5 moves.
if count >= 5:
if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break
elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break
elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the bottom
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break
elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the left side
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break
elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': # down the middle
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break
elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the right side
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break
elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break
elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
win_message(turn)
break

# If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
if count == 9:
print("\nGame Over.\n")
print("It's a Tie!!")
print("\nGame Over.\nIt's a Tie!!")

# Now we have to change the player after every move.
if turn =='X':
if turn == 'X':
turn = 'O'
else:
turn = 'X'

# Now we will ask if player wants to restart the game or not.
restart = input("Do want to play Again?(y/n)")
if restart == "y" or restart == "Y":
restart = input("Do want to play Again?(y/n) ")
if restart in ('y', 'Y'):
for key in board_keys:
theBoard[key] = " "

game()

if __name__ == "__main__":
game()
game()