-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
144 lines (118 loc) · 4.63 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
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
import asyncio
from board import Board
import random
class AI :
NEG_INFINITY = -999999999
POS_INFINITY = 999999999
def __init__(self):
self.win = False
self.board = Board()
def isGame(self):
return self.board.checkWin()
def printBoard(self):
self.board.printBoard()
def opponentMove(self, column):
if self.board.playMove('x', column) == False:
return False
if self.board.checkWin() != False:
print("You have won!")
self.win = True
return True
# TODO: Make the AI make an intelligent move instead
# of a random move
def searchBest(self, depth, alpha, beta, maximizing):
opponentWillWin = -1
for i in range(7):
winningPlayer = self.board.winningMove(i)
if winningPlayer == 'o' and maximizing:
return (i, self.POS_INFINITY)
elif winningPlayer == 'o' and not maximizing:
opponentWillWin = i
elif winningPlayer == 'x' and maximizing:
opponentWillWin = i
elif winningPlayer == 'x' and not maximizing:
return (opponentWillWin, self.NEG_INFINITY)
if opponentWillWin != -1 and maximizing:
return (opponentWillWin, self.POS_INFINITY)
elif opponentWillWin != -1 and not maximizing:
return (opponentWillWin, self.NEG_INFINITY)
if self.board.isFull():
return (False, 0)
if depth == 0:
return (-1, self.scoreBoard())
moveOrder = [3, 2, 4, 1, 5, 0, 6]
if maximizing:
score = self.NEG_INFINITY
bestColumn = 3
for i in moveOrder:
if self.board.playMove('o', i) == False:
continue
nextMoveScore = self.searchBest(depth - 1, alpha, beta, False)[1]
self.board.undoMove()
if nextMoveScore > score:
score = nextMoveScore
alpha = score
bestColumn = i
if alpha <= beta:
break
return (bestColumn, score)
else:
score = self.POS_INFINITY
bestColumn = 3
for i in moveOrder:
if self.board.playMove('x', i) == False:
continue
nextMoveScore = self.searchBest(depth - 1, alpha, beta, True)[1]
self.board.undoMove()
if nextMoveScore < score:
score = nextMoveScore
alpha = score
bestColumn = i
if alpha <= beta:
break
return (bestColumn, score)
def scoreBoard(self):
score = 0
for col in range(7):
for row in range(self.board.currentHeight[col] + 1, 6):
if (col == 3 and self.board.data[row][col] == 'o'):
score += 3
score += self.scorePoint(row, col)
return score
def scorePoint(self, row, col):
score = 0
directions = ((1, 0), (1, 1), (1, -1), (0, -1), (0, 1), (-1, 0), (-1, 1), (-1, -1))
for dx, dy in directions:
pieces = []
for i in range(0, 4):
if (col + (dx * i) < 0 or col + (dx * i) > 6
or row + (dy * i) < 0 or row + (dy * i) > 5):
break
pieces.append(self.board.data[row + (dy*i)][col + (dx*i)])
if len(pieces) != 4:
continue
if pieces.count('o' == 4):
score += 100
elif (pieces.count('o') == 3 and pieces.count(self.board.BLANK) == 1):
score += 9
elif (pieces.count('o') == 2 and pieces.count(self.board.BLANK) == 2):
score += 4
elif (pieces.count('x') == 3 and pieces.count(self.board.BLANK) == 1):
score -= 2
elif (pieces.count('x') == 2 and pieces.count(self.board.BLANK) == 2):
score -= 9
elif (pieces.count('x') == 4):
return self.NEG_INFINITY
return score
def makeRandomMove(self):
options = []
for i in range(7):
if self.board.currentHeight != -1:
options.append(i)
col = options[random.randint(0, len(options) - 1)]
self.board.playMove('o', col)
return col
def makeMove(self):
nextMove, score = self.searchBest(5, self.POS_INFINITY, self.NEG_INFINITY, True)
self.board.playMove('o', nextMove)
return nextMove, score