-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.py
More file actions
105 lines (78 loc) · 2.72 KB
/
player.py
File metadata and controls
105 lines (78 loc) · 2.72 KB
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
import board
#from agent import Agent
import random
class RandPlayerUtils:
def PlayMove(player, board):
legal_moves = list(board.legal_moves)
output = random.choice(legal_moves)
return output
class Player:
### Built-in Class Methods ###
def __init__(self, color):
self.color = color
pass
### Public Methods ###
def PlayMove(self, b):
pt = RandPlayerUtils.PlayMove(self, b)
return pt
def Evaluate(self, b):
(is_end, winner_color) = b.DetectGameEnd()
score = 0
if (is_end):
if (winner_color == self.color):
score = 1
else:
score = 0
else:
score = 0.5
return score
'''
def minimax(self, curr_position, b, end_node_depth, maximizing_player):
# tutorial from https://www.youtube.com/watch?v=l-hh51ncgDI&ab_channel=SebastianLague
# current player looks to maximize (temp heuristic -> see if win)
# opponent looks to minimize
# need to perform static heuristic to get last node values
if end_node_depth == 0 or self.Evaluate(b) == 1:
# perform heuristic on position
#(left, right, bottom) = b.board_edges
#print(left)
#print(right)
#print(bottom)
# calculate heuristic here
#return heuristic
return 0
# get the list of legal moves after current move has been played
legal_moves = list(b.legal_moves)
if maximizing_player:
# current player
maxVal = -99999
for move in legal_moves:
# call minimax on child node, one up from final checked node
# set maximizing_player to false, as this is opponent's turn
# and is looking to minimize
cur_val = self.minimax(move, b, end_node_depth - 1, False)
maxVal = max(maxVal, val)
return maxVal
else:
# opponent
minVal = 99999
for move in legal_moves:
cur_val = self.minimax(move, b, end_node_depth - 1, True)
minVal = min(minVal, val)
return minVal
'''
### Private Methods ###
if __name__ == "__main__":
b = Board(4)
w_player = Agent(b.WHITE)
b_player = Player(b.BLACK)
w_move = w_player.FirstTurn(b)
#b_move = RandPlayerUtils.PlayMove(b_player, b)
while(self.Evaluate(b) == 0.5):
w_move = w_player.PlayTurn(b)
#b_move = RandPlayerUtils.PlayMove(b_player, b)
#w_move = RandPlayerUtils.PlayMove(w_player, b)
#b_move = RandPlayerUtils.PlayMove(b_player, b)
#print(w_move)
#print(b_move)
pass