-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.py
96 lines (83 loc) · 2.39 KB
/
algo.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
class Algo:
def __init__(self):
self.board = ['-','-','-',
'-','-','-',
'-','-','-',
'-','-','-',]
def check_win(self):
win = False
#horizontal win
if self.board[0] == self.board[1] and self.board[1] == self.board[2] and self.board[2] != '-':
win = True
elif self.board[3] == self.board[4] and self.board[4] == self.board[5] and self.board[5] != '-':
win = True
elif self.board[6] == self.board[7] and self.board[7] == self.board[8] and self.board[8] != '-':
win = True
#vertical win
elif self.board[0] == self.board[3] and self.board[3] == self.board[6] and self.board[6] != '-':
win = True
elif self.board[1] == self.board[4] and self.board[4] == self.board[7] and self.board[7] != '-':
win = True
elif self.board[2] == self.board[5] and self.board[5] == self.board[8] and self.board[8] != '-':
win = True
#diagonal win
elif self.board[0] == self.board[4] and self.board[4] == self.board[8] and self.board[8] != '-':
win = True
elif self.board[2] == self.board[4] and self.board[4] == self.board[6] and self.board[6] != '-':
win = True
return win
def check_draw(self):
draw = True
for x in range(0,9):
if self.board[x] == '-':
draw = False
break
return draw
def minimax(self, isAI):
if self.check_win():
if isAI:
score = 1
else:
score = -1
elif self.check_draw():
score = 0
else:
#The next move is for the player. The player will minimize the score
if isAI:
bestScore = 1000
bestMove = -1
for x in range(0,9):
if self.board[x]=="-":
self.board[x] = 'X'
moveScore = self.minimax(False)
self.board[x] = '-'
if moveScore<bestScore:
bestScore = moveScore
bestMove = x
score = bestScore
#The next move is for the computer. The computer will maximize the score
else:
bestScore = -1000
bestMove = -1
for x in range(0,9):
if self.board[x]=="-":
self.board[x] = 'O'
moveScore = self.minimax(True)
self.board[x] = '-'
if moveScore>bestScore:
bestScore = moveScore
bestMove = x
score = bestScore
return score
def ai_choice(self):
bestScore = -1000
bestMove = -1
for x in range(0,9):
if self.board[x]=="-":
self.board[x] = 'O'
moveScore = self.minimax(True)
self.board[x] = '-'
if moveScore>bestScore:
bestScore = moveScore
bestMove = x
return bestMove