-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe.py
116 lines (100 loc) · 1.97 KB
/
Tic-Tac-Toe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
winner = -1
c=0
s='1 2 3 4 5 6 7 8 9 '
def printboard(n):
'''
The function prints the board, and also the user who has the next turn.
The logic of adding a symbol includes searching the number in the string 's'
and placing it in place if the next character is a space.
'''
global s, winner
global c
if n != 0:
if c%2==0:
symb='0'
else:
symb='X'
i=s.find(n)
if s[i+1]==' ':
s=s[:i+1]+symb+s[i+2:]
else:
print()
print()
print('!!!!!Already taken!!!!!!')
c-=1
sv=' | | '
sh='-----------'
s3=' {} | {} | {} '.format(s[13],s[15],s[17])
s2=' {} | {} | {} '.format(s[7],s[9],s[11])
s1=' {} | {} | {} '.format(s[1],s[3],s[5])
print(sv)
print(s3)
print(sv)
print(sh)
print(sv)
print(s2)
print(sv)
print(sh)
print(sv)
print(s1)
print(sv)
if s[13] == s[15] == s[17] :
if s[13] == 'X':
winner = 2
elif s[13] == '0':
winner = 1
if s[7] == s[9] == s[11] :
if s[7] == 'X':
winner = 2
elif s[7] == '0':
winner = 1
if s[3] == s[5] == s[1] :
if s[1] == 'X':
winner = 2
elif s[1] == '0':
winner = 1
if s[13] == s[9] == s[5] :
if s[13] == 'X':
winner = 2
elif s[13] == '0':
winner = 1
if s[1] == s[9] == s[17] :
if s[1] == 'X':
winner = 2
elif s[1] == '0':
winner = 1
if s[1] == s[13] == s[7] :
if s[1] == 'X':
winner = 2
elif s[1] == '0':
winner = 1
if s[3] == s[9] == s[15] :
if s[3] == 'X':
winner = 2
elif s[3] == '0':
winner = 1
if s[11] == s[5] == s[17] :
if s[11] == 'X':
winner = 2
elif s[11] == '0':
winner = 1
ans=['y']
if ' ' not in [s[1], s[3], s[5], s[7], s[9], s[11], s[13], s[15], s[17]]:
ans = ['n']
return ans
if __name__=='__main__':
ans=['y']
os.system('clear')
printboard(0)
while ans==['y']:
n=input('Choose your next position (1-9) player {} :'.format((c%2)+1))
os.system('clear')
ans=printboard(n)
if winner != -1:
break
c+=1
if winner != -1:
print('WINNER IS PLAYER:'+str(winner))
else:
print('DRAW!!!')