Skip to content

Commit 5341615

Browse files
committed
added Tic Tac Toe game
1 parent 4eef6cc commit 5341615

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

.vs/slnx.sqlite

108 KB
Binary file not shown.

Tic Tac Toe .py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Implementation of Two Player Tic-Tac-Toe game in Python.
2+
3+
''' We will make the board using dictionary
4+
in which keys will be the location(i.e : top-left,mid-right,etc.)
5+
and initialliy it's values will be empty space and then after every move
6+
we will change the value according to player's choice of move. '''
7+
8+
theBoard = {'1': ' ', '2': ' ', '3': ' ',
9+
'4': ' ', '5': ' ', '6': ' ',
10+
'7': ' ', '8': ' ', '9': ' '}
11+
12+
board_keys = []
13+
14+
for key in theBoard:
15+
board_keys.append(key)
16+
17+
''' We will have to print the updated board after every move in the game and
18+
thus we will make a function in which we'll define the printBoard function
19+
so that we can easily print the board everytime by calling this function. '''
20+
21+
22+
def printBoard(board):
23+
print(board['1'] + '|' + board['2'] + '|' + board['3'])
24+
print('-+-+-')
25+
print(board['4'] + '|' + board['5'] + '|' + board['6'])
26+
print('-+-+-')
27+
print(board['7'] + '|' + board['8'] + '|' + board['9'])
28+
29+
30+
# Now we'll write the main function which has all the gameplay functionality.
31+
def game():
32+
turn = 'X'
33+
count = 0
34+
35+
for i in range(10):
36+
printBoard(theBoard)
37+
print("It's your turn," + turn + ".Move to which place?")
38+
39+
move = input()
40+
41+
if theBoard[move] == ' ':
42+
theBoard[move] = turn
43+
count += 1
44+
else:
45+
print("That place is already filled.\nMove to which place?")
46+
continue
47+
48+
# Now we will check if player X or O has won,for every move after 5 moves.
49+
if count >= 5:
50+
if theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the top
51+
printBoard(theBoard)
52+
print("\nGame Over.\n")
53+
print(" **** " + turn + " won. ****")
54+
break
55+
elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle
56+
printBoard(theBoard)
57+
print("\nGame Over.\n")
58+
print(" **** " + turn + " won. ****")
59+
break
60+
elif theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the bottom
61+
printBoard(theBoard)
62+
print("\nGame Over.\n")
63+
print(" **** " + turn + " won. ****")
64+
break
65+
elif theBoard['7'] == theBoard['4'] == theBoard['1'] != ' ': # down the left side
66+
printBoard(theBoard)
67+
print("\nGame Over.\n")
68+
print(" **** " + turn + " won. ****")
69+
break
70+
elif theBoard['8'] == theBoard['5'] == theBoard['2'] != ' ': # down the middle
71+
printBoard(theBoard)
72+
print("\nGame Over.\n")
73+
print(" **** " + turn + " won. ****")
74+
break
75+
elif theBoard['9'] == theBoard['6'] == theBoard['3'] != ' ': # down the right side
76+
printBoard(theBoard)
77+
print("\nGame Over.\n")
78+
print(" **** " + turn + " won. ****")
79+
break
80+
elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal
81+
printBoard(theBoard)
82+
print("\nGame Over.\n")
83+
print(" **** " + turn + " won. ****")
84+
break
85+
elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal
86+
printBoard(theBoard)
87+
print("\nGame Over.\n")
88+
print(" **** " + turn + " won. ****")
89+
break
90+
91+
# If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
92+
if count == 9:
93+
print("\nGame Over.\n")
94+
print("It's a Tie!!")
95+
96+
# Now we have to change the player after every move.
97+
if turn == 'X':
98+
turn = 'O'
99+
else:
100+
turn = 'X'
101+
102+
# Now we will ask if player wants to restart the game or not.
103+
restart = input("Do want to play Again?(y/n)")
104+
if restart == "y" or restart == "Y":
105+
for key in board_keys:
106+
theBoard[key] = " "
107+
108+
game()
109+
110+
111+
if __name__ == "__main__":
112+
game()

0 commit comments

Comments
 (0)