-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe using MiniMax.py
More file actions
76 lines (76 loc) · 1.9 KB
/
TicTacToe using MiniMax.py
File metadata and controls
76 lines (76 loc) · 1.9 KB
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
def emptyCells(l):
c = []
for i in range(3):
for j in range(3):
if l[i][j]=='-':
c.append([i,j])
return c
def wins(l,c):
states = [[l[0][0],l[0][1],l[0][2]],[l[1][0],l[1][1],l[1][2]],[l[2][0],l[2][1],l[2][2]],
[l[0][0],l[1][0],l[2][0]],[l[0][1],l[1][1],l[2][1]],[l[0][2],l[1][2],l[2][2]],
[l[0][0],l[1][1],l[2][2]],[l[2][0],l[1][1],l[0][2]]
]
if [c,c,c] in states:
return True
else : return False
def evaluate(l):
if wins(l,'O'):
return 1
elif wins(l,'X'):
return -1
return 0
def minmax(l,depth,player):
best = []
if depth==0 or wins(l,'O') or wins(l,'X'):
return [-1,-1,evaluate(l)]
if player=='O':
best = [-1,-1,-1000]
else:
best = [-1,-1,1000]
c = emptyCells(l)
for i in c:
r,cl = i[0],i[1]
l[r][cl] = player
score = []
if player == 'O':
score= minmax(l,depth-1,'X')
score[0],score[1] = r,cl
if best[2]<score[2]:
best = score
else:
score= minmax(l,depth-1,'O')
score[0],score[1] = r,cl
if best[2]>score[2]:
best = score
l[r][cl] = '-'
return best
l = [['-','-','-'] for _ in range(3)]
def printl(l):
for i in l:
print(i)
print('*'*30)
turn= True
printl(l)
while 1:
if turn:
turn = not turn
r,c = [int(i) for i in input().split()]
l[r][c] = 'X'
if wins(l,'X'):
printl(l)
print("you won")
break
else:
turn = not turn
nex = minmax(l,len(emptyCells(l)),'O')
print(nex)
if nex[0]==-1:
print("draw")
break
else:
l[nex[0]][nex[1]] = 'O'
printl(l)
if wins(l,'O'):
printl(l)
print("comp won")
break