-
Notifications
You must be signed in to change notification settings - Fork 0
/
bestMove.py
92 lines (67 loc) · 1.98 KB
/
bestMove.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
from check import check
from check import print2
global countt
countt=0
global d
d=0
def minimax(borad,depth,WhoPlay):
global d
c=check(borad)
if(c==10 or c==-10):
if(c==10):
#print depth
d=d+1
return 10-depth
if c==-10:
d=d+1
return -10+depth
else:
if c==2:
return 0
if WhoPlay=="AI":
best=-1000
for i in range(3):
for j in range(3):
if borad[i][j]==" ":
borad[i][j]="X"
#print i," "," ",j
best=max(minimax(borad,depth+1,"HUM"),best)
#list.append(minimax(borad,depth+1,"HUn"))
#print best,"depth ",depth+1
borad[i][j]=" "
return best
else:
best=10000
for i in range(3):
for j in range(3):
if borad[i][j]==" ":
borad[i][j]="O"
best=min(minimax(borad,depth+1,"AI"),best)
##list.append(minimax(borad,depth+1,"AI"))
borad[i][j]=" "
return best
###############################################
def bestMove(tic):
global countt
countt=0
global d
best=-100000
best_i=0
best_j=0
c=0
for i in range(3):
for j in range(3):
if(tic[i][j]==" "):
tic[i][j]="X"
c=minimax(tic,0,"HUm")
#print i," ",j," C ",c," D",d
countt=d
d=0
tic[i][j]=" "
if(best<=c):
best=c
## print i," ",j," C ",c," D",mi
best_i=i
best_j=j
tic[best_i][best_j]="X"
return best_i,best_j