-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIPlayer.py
114 lines (100 loc) · 3.52 KB
/
AIPlayer.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
#
# ps10pr4.py (Problem Set 10, Problem 4)
#
# AI Player for use in Connect Four
#
import time
import random
from ps10pr3 import *
class AIPlayer(Player):
""" Implements an AIPlayer class with methods
inherited from the Player class
"""
def __init__(self, checker, tiebreak, lookahead):
""" AIPlayer constructor with checker,
tiebreaking strategy,and look-ahead value attributes
"""
assert(checker == 'X' or checker == 'O')
assert(tiebreak == 'LEFT' or tiebreak == 'RIGHT' or tiebreak == 'RANDOM')
assert(lookahead >= 0)
super().__init__(checker)
self.tiebreak = tiebreak
self.lookahead = lookahead
def __repr__(self):
""" returns a string with the attributes of the AIPlayer
"""
inh = super().__repr__()
rep = inh + ' (' + self.tiebreak + ', ' + str(self.lookahead) + ')'
return rep
def max_score_column(self, scores):
""" Determines the best column from a list of scores and breaks
ties according to the called object's tiebreaking strategy
"""
columns = []
best = max(scores)
for col in range(len(scores)):
if scores[col] == best:
columns += [col]
if self.tiebreak == 'LEFT':
return columns[0]
elif self.tiebreak == 'RIGHT':
return columns[-1]
else:
return random.choice(columns)
def scores_for(self, board):
""" returns the scores for each column for an AIPlayer
with a given board state
"""
if self.lookahead > 0:
opp = AIPlayer(self.opponent_checker(), self.tiebreak, (self.lookahead-1))
scores = [50]*board.width
for col in range(board.width):
if not(board.can_add_to(col)):
scores[col] = -1
elif board.is_win_for(self.checker):
scores[col] = 100
elif board.is_win_for(self.opponent_checker()):
scores[col] = 0
elif self.lookahead == 0:
scores[col] = 50
else:
board.add_checker(self.checker, col)
oppscores = opp.scores_for(board)
scores[col]= 100 - max(oppscores)
board.remove_checker(col)
return scores
def next_move(self, board):
""" returns the column of the best move for the called AIPlayer
and increments the number of moves made
"""
t = time.clock()
col = self.max_score_column(self.scores_for(board))
self.num_moves += 1
t=time.clock()-t
print(t)
return col
def test():
""" method testing
"""
scores = [0, 0, 50, 0, 50, 50, 0]
p1 = AIPlayer('X', 'LEFT', 1)
p2 = AIPlayer('X', 'RIGHT', 1)
p3 = AIPlayer('O', 'RANDOM', 2)
print(p1.max_score_column(scores))
print(p2.max_score_column(scores))
print(p3.max_score_column(scores))
print('-----------------------')
b = Board(6,7)
b.add_checkers('3042564121354')
print(b)
for i in range(7):
p3.lookahead = i
print(p3.scores_for(b))
print('-------------------------------------------')
def testAI():
""" Play Testing
"""
p1 = AIPlayer('X', 'RANDOM', 6)
p2 = AIPlayer('O', 'RANDOM', 6)
p3 = Player('X')
connect_four(p1, p2)