forked from arthurvergacas/tic-tac-toe-AI
/
ai.py
74 lines (61 loc) · 2.25 KB
/
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
import random
from gameManager import GameManager
class Ai:
def __init__(self):
self.manager = GameManager()
def minimax(self, board, OPlayer, alpha, beta, depth=float('inf')):
# return the static evaluation of the final possible game
# X wins: -1
# O wins: +1
# tie: 0
if depth == 0 or self.manager.checkWin(board) or self.manager.checkTie(board):
if self.manager.checkWin(board, True) == 'X':
return -1
elif self.manager.checkWin(board, True) == 'O':
return 1
elif self.manager.checkTie(board):
return 0
else:
# depth not working properly
return 0
if OPlayer:
maxValue = float('-inf')
for cell in board:
if cell.state == '':
cell.state = 'O'
value = self.minimax(
board, False, alpha, beta, depth=depth - 1)
cell.state = ''
maxValue = max(maxValue, value)
alpha = max(alpha, value)
if beta <= alpha:
break
return maxValue
if OPlayer == False:
minValue = float('inf')
for cell in board:
if cell.state == '':
cell.state = 'X'
value = self.minimax(
board, True, alpha, beta, depth=depth - 1)
cell.state = ''
minValue = min(minValue, value)
beta = min(beta, value)
if beta <= alpha:
break
return minValue
def play(self, board):
# choose a cell to play
# initializing move to store the best cell to move
bestCell = None
bestScore = float('-inf')
for cell in board:
if cell.state == '':
cell.state = 'O'
score = self.minimax(board, False, float(
'-inf'), float('inf'))
cell.state = ''
if score > bestScore:
bestScore = score
bestCell = cell
bestCell.changeState('O')