Skip to content

Commit 63f9b3f

Browse files
author
sudarshan412
authored
Added TicTacToe program
1 parent 101dcc5 commit 63f9b3f

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

ttictactoe.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# https://www.facebook.com/suyash.ssp/posts/2629284860654153
2+
# posted by suyash
3+
board = {'T1': ' ', 'T2': ' ', 'T3': ' ',
4+
'M1': ' ', 'M2': ' ', 'M3': ' ',
5+
'D1': ' ', 'D2': ' ', 'D3': ' '}
6+
player = 1 # to initialise first player
7+
total_moves = 0 # to count the moves
8+
end_check = 0
9+
print('T1|T2|T3')
10+
print('--+--+--')
11+
print('M1|M2|M3')
12+
print('--+--+--')
13+
print('D1|D2|D3')
14+
print('***************************')
15+
def check():
16+
# checking the moves of player one
17+
# for horizontal(start)
18+
if board['T1'] == 'X' and board['T2'] == 'X' and board['T3'] == 'X':
19+
print('Player one won !')
20+
return 1
21+
if board['M1'] == 'X' and board['M2'] == 'X' and board['M3'] == 'X':
22+
print('Player One Won!!')
23+
return 1
24+
if board['D1'] == 'X' and board['D2'] == 'X' and board['D3'] == 'X':
25+
print('Player One Won!!')
26+
return 1
27+
# for horizontal(end)
28+
# for diagonal(start)
29+
if board['T1'] == 'X' and board['M2'] == 'X' and board['D3'] == 'X':
30+
print('Player One Won!!')
31+
return 1
32+
# for diagonal(end)
33+
# for vertical(start)
34+
if board['T1'] == 'X' and board['M1'] == 'X' and board['D1'] == 'X':
35+
print('Player One Won!!')
36+
return 1
37+
if board['T2'] == 'X' and board['M2'] == 'X' and board['D2'] == 'X':
38+
print('Player One Won!!')
39+
return 1
40+
if board['T3'] == 'X' and board['M3'] == 'X' and board['D3'] == 'X':
41+
print('Player One Won!!')
42+
return 1
43+
# for vertical(end)
44+
45+
# checking the moves of player two
46+
if board['T1'] == 'O' and board['T2'] == 'O' and board['T3'] == 'O':
47+
print('Player Two Won!!')
48+
return 1 # used to end the game
49+
if board['M1'] == 'O' and board['M2'] == 'O' and board['M3'] == 'O':
50+
print('Player Two Won!!')
51+
return 1
52+
if board['D1'] == 'O' and board['D2'] == 'O' and board['D3'] == 'O':
53+
print('Player Two Won!!')
54+
return 1
55+
if board['T1'] == 'O' and board['M2'] == 'O' and board['D3'] == 'O':
56+
print('Player Two Won!!')
57+
return 1
58+
if board['T1'] == 'O' and board['M1'] == 'O' and board['D1'] == 'O':
59+
print('Player Two Won!!')
60+
return 1
61+
if board['T2'] == 'O' and board['M2'] == 'O' and board['D2'] == 'O':
62+
print('Player Two Won!!')
63+
return 1
64+
if board['T3'] == 'O' and board['M3'] == 'O' and board['D3'] == 'O':
65+
print('Player Two Won!!')
66+
return 1
67+
return 0
68+
while True:
69+
print(board['T1']+'|'+board['T2']+'|'+board['T3'])
70+
print('-+-+-')
71+
print(board['M1'] + '|' + board['M2'] + '|' + board['M3'])
72+
print('-+-+-')
73+
print(board['D1'] + '|' + board['D2'] + '|' + board['D3'])
74+
end_check = check()
75+
if total_moves == 9 or end_check == 1:
76+
break
77+
while True: # input from players
78+
if player == 1: # choose player
79+
p1_input = input('player one')
80+
if p1_input.upper() in board and board[p1_input.upper()] == ' ':
81+
board[p1_input.upper()] = 'X'
82+
player = 2
83+
break
84+
# on wrong input
85+
else:
86+
print('Invalid input, please try again')
87+
continue
88+
else:
89+
p2_input = input('player two')
90+
if p2_input.upper() in board and board[p2_input.upper()] == ' ':
91+
board[p2_input.upper()] = 'O'
92+
player = 1
93+
break
94+
else: # on wrong input
95+
print('Invalid input, please try again')
96+
continue
97+
total_moves += 1
98+
print('***************************')
99+
print()
100+

0 commit comments

Comments
 (0)