-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplayer.py
62 lines (45 loc) · 1.47 KB
/
player.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
#!/usr/bin/env python
""" player.py Humberto Henrique Campos Pinheiro
Human and Computer classes
"""
from evaluator import Evaluator
from config import WHITE, BLACK
from minimax import Minimax
import random
def change_color(color):
if color == BLACK:
return WHITE
else:
return BLACK
class Human:
""" Human player """
def __init__(self, gui, color="black"):
self.color = color
self.gui = gui
def get_move(self):
""" Uses gui to handle mouse
"""
validMoves = self.current_board.get_valid_moves(self.color)
while True:
move = self.gui.get_mouse_input()
if move in validMoves:
break
self.current_board.apply_move(move, self.color)
return 0, self.current_board
def get_current_board(self, board):
self.current_board = board
class Computer(object):
def __init__(self, color, prune=3):
self.depthLimit = prune
evaluator = Evaluator()
self.minimaxObj = Minimax(evaluator.score)
self.color = color
def get_current_board(self, board):
self.current_board = board
def get_move(self):
return self.minimaxObj.minimax(self.current_board, None, self.depthLimit, self.color,
change_color(self.color))
class RandomPlayer (Computer):
def get_move(self):
x = random.sample(self.current_board.get_valid_moves(self.color), 1)
return x[0]