forked from azure2103/program-wiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
114 lines (90 loc) · 2.66 KB
/
TicTacToe.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
import random as r
'''
X | X | X
---+---+---
X | X | X
---+---+---
X | X | X
'''
def printlist(l):
for i in range(0,3,1):
for j in range(0,3,1):
if (l[i][j]=='$'):
print(" ",end='')
else:
print("",l[i][j],"",end='')
if (j<2):
print("|",end='')
print()
if (i<2):
print("---+---+---")
def checkWinner(l):
for i in l:
if (i[0]==i[1]==i[2]=='X'):
print("\n************ YOU WIN ************\n")
return 'n'
elif (i[0]==i[1]==i[2]=='O'):
print("\n************ COMPUTER WINS ************\n")
return 'n'
for col in range(0,3,1):
if (l[0][col]==l[1][col]==l[2][col]=='X'):
print("\n************ YOU WIN ************\n")
return 'n'
elif (l[0][col]==l[1][col]==l[2][col]=='O'):
print("\n************ COMPUTER WINS ************\n")
return 'n'
if (l[0][0]==l[1][1]==l[2][2]=='X'):
print("\n************ YOU WIN ************\n")
return 'n'
elif (l[0][0]==l[1][1]==l[2][2]=='O'):
print("\n************ COMPUTER WINS ************\n")
return 'n'
if (l[0][2]==l[1][1]==l[2][0]=='X'):
print("\n************ YOU WIN ************\n")
return 'n'
elif (l[0][2]==l[1][1]==l[2][0]=='O'):
print("\n************ COMPUTER WINS ************\n")
return 'n'
#if nothing worked return y
return 'y'
l= [[0,0],[0,1],[0,2],
[1,0],[1,1],[1,2],
[2,0],[2,1],[2,2],
]
entry = [['$','$','$'],['$','$','$'],['$','$','$']]
op = 'y' ;
print("Simple Tic-Tac-Toe game OX")
print("---You vs the Computer ----")
while( len(l)!=0 and op=='y'):
print("Current matrix :")
printlist(entry)
print("\n--- Enter move ---")
movecheck = True
while(movecheck):
row = int(input("Enter next row index : "))
col = int(input("Enter next column index : "))
m = [row,col]
if (m not in l):
print("\n\n*** Wrong move ***")
print("--- Enter again ---")
else:
movecheck=False
entry[row][col] = 'X'
l.remove([row,col])
print("\n~~~~~~~~~~~~~~~~~~")
printlist(entry)
print("~~~~~~~~~~~~~~~~~~")
op = checkWinner(entry)
if (op=='n'):
break
#check winner
print("\n\n---Computer's turn :")
comp = l[ r.randint(0,len(l)-1) ]
crow = comp[0]
ccol = comp[1]
entry[crow][ccol] = 'O'
l.remove(comp)
print("---Computer chose :",crow,',',ccol,"\n")
#check winner
if (len(l)==0 and op=='y'):
print("No moves left :(")