-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe_Ai.py
188 lines (139 loc) · 4.64 KB
/
TicTacToe_Ai.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 21:55:12 2020
@author: Debanjan
"""
import random
bo = [' ' for x in range(10)]
adepth=0
def isBoardEmpty(li1=bo):
return (li1[1]==' ' or li1[2]==' ' or li1[3]==' ' or li1[4]==' ' or
li1[5]==' ' or li1[6]==' ' or li1[7]==' ' or li1[8]==' ' or
li1[9]==' ')
def playerMove():
while True:
x=int(input('Enter your move: '))
if isValidMove(x):
bo[x]='X'
break
else:
print('Wrong Move')
def isValidMove(x):
return (bo[x]==' ')
def isWin(c,li1 = bo):
return ((li1[1]==c and li1[2]==c and li1[3]==c) or
(li1[4]==c and li1[5]==c and li1[6]==c) or
(li1[7]==c and li1[8]==c and li1[9]==c) or
(li1[1]==c and li1[4]==c and li1[7]==c) or
(li1[2]==c and li1[5]==c and li1[8]==c) or
(li1[3]==c and li1[6]==c and li1[9]==c) or
(li1[1]==c and li1[5]==c and li1[9]==c) or
(li1[3]==c and li1[5]==c and li1[7]==c))
def compMove():
print('Its computer\'s turn')
bestMove = []
bestVal=-1000
global bo
li1 = bo
for i in range(1,10):
if li1[i]==' ':
li1[i]='O'
moveVal = minimax(li1,0,False)
'''
if moveVal>0:
moveVal = moveVal-adepth
elif moveVal<0:
moveVal = moveVal+adepth
'''
li1[i]=' '
if moveVal>bestVal:
bestVal=moveVal
bestMove=[]
bestMove.append(i)
print(bestMove,bestVal)
elif moveVal==bestVal:
bestMove.append(i)
print(bestMove,bestVal)
rand_idx = random.randrange(len(bestMove))
finalMove = bestMove[rand_idx]
print(bestMove,bestVal)
bo[finalMove]='O'
def printBoard():
print(bo[1]+' | '+bo[2]+' | '+bo[3])
print('-----------')
print(bo[4]+' | '+bo[5]+' | '+bo[6])
print('-----------')
print(bo[7]+' | '+bo[8]+' | '+bo[9])
def minimax(li1,depth,ismax):
global adepth
if isWin('O',li1):
#adepth=depth
return (10-depth)
elif isWin('X',li1):
#adepth=depth
return (-10+depth)
elif isBoardEmpty(li1):
if ismax:
bestVal= -1000
for i in range(1,10):
if li1[i]==' ':
li1[i]='O'
score=0
score=minimax(li1,depth+1,False)
li1[i]=' '
bestVal=max(bestVal,score)
return bestVal
else:
bestVal= +1000
for i in range(1,10):
if li1[i]==' ':
li1[i]='X'
score=0
score=minimax(li1,depth+1,True)
li1[i]=' '
bestVal=min(bestVal,score)
return bestVal
else:
return 0
while True:
print('Welcome to Tic Tac Toe!')
a=int(input('If you choose first move type \'1\' else choose \'2\': '))
while a==1:
if isBoardEmpty():
playerMove()
printBoard()
else:
print('Its a Tie game!')
break
if isWin('X'):
print('You Won the game!')
break
if isBoardEmpty():
compMove()
printBoard()
else:
print('Its a Tie game!')
break
if isWin('O'):
print('Computer Won the game!')
break
while a==2:
if isBoardEmpty():
compMove()
printBoard()
else:
print('Its a Tie game!')
break
if isWin('O'):
print('Computer Won the game!')
break
if isBoardEmpty():
playerMove()
printBoard()
else:
print('Its a Tie game!')
break
if isWin('X'):
print('You Won the game!')
break
break