forked from UWOneLaptop/Checkers
/
Player.py
146 lines (118 loc) · 5.15 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
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
145
146
from Move import Move
import copy
from GameState import GameState
import random
WHITE = 1
BLACK = 2
# The Player classes take turns in games of checkers. They are called by checkers.py
# The human player recieves input from a human player telling it where the human
# wants to move. This class makes sure the move is valid and checks for jumps
class Human_Player():
color = None
#Constructor
def __init__(self, color):
self.color = color
# The turn method should receive the start and end points of the desired move
# and the board object to make the move on. This method should check that the move
# is valid (by using the board's methods) and then update the board with that move.
#
# Returns a code indicating if the turn is over, and the reason if it is not
def turn(self, start, end, board, state):
if not board.checkMove(start, end, state):
print "returning move invalid"
return Move.MOVE_INVALID
return board.move(start, end, state)
# The AI player searches the board for the best move available. It only takes the board
# as a parameter and will return the updated board and the move made (so that the GUI
# can be updated)
class AI_Player():
color = None
checkers = None
#Constructor
def __init__(self, color, checkers):
self.color = color
self.checkers = checkers
def turn(self, board, state):
moves = board.getAllMoves(state)
if not moves:
return
# variables for finding the best move for this turn
board_orig = copy.deepcopy(board.board)
value_best = -99
end_best = 0
start_best = 0
updateGUI = 0
# search for the next best move
random.shuffle(moves)
for move in moves:
board.board = copy.deepcopy(board_orig)
end = move.pop()
start = move.pop()
last_cell = board.board[start.row][start.column]
return_code = board.move(start, end, state)
self.jumpAgain(board, state, return_code, updateGUI)
# now play the other player's move
if (state.get_state() == 2):
state_other = GameState(GameState.WhitesTurn)
self.turn_other(board, state_other)
elif (state.get_state() == 1):
state_other = GameState(GameState.BlacksTurn)
self.turn_other(board, state_other)
else:
print "Invalid player state"
value = board.getValue(state)
print value
if value > value_best:
value_best = value
end_best = end
start_best = start
# we found it so actually make the move
updateGUI = 1
board.board = copy.deepcopy(board_orig)
print "CHOOSE THIS MOVE"
print value_best
last_cell = board.board[start_best.row][start_best.column]
return_code = board.move(start, end, state)
self.checkers.move(start, end, last_cell, return_code)
self.jumpAgain(board, state, return_code, updateGUI)
board.printBoard()
print "AI turn complete"
def turn_other(self, board, state):
moves = board.getAllMoves(state)
if not moves:
return
# variables for finding the best move for this turn
board_orig = copy.deepcopy(board.board)
value_best = -99
end_best = 0
start_best = 0
updateGUI = 0
# search for the next best move
for move in moves:
board.board = copy.deepcopy(board_orig)
end = move.pop()
start = move.pop()
last_cell = board.board[start.row][start.column]
return_code = board.move(start, end, state)
self.jumpAgain(board, state, return_code, updateGUI)
value = board.getValue(state)
if value > value_best:
value_best = value
end_best = end
start_best = start
# we found it so actually make the move
board.board = copy.deepcopy(board_orig)
return_code = board.move(start, end, state)
self.jumpAgain(board, state, return_code, updateGUI)
def jumpAgain(self, board, state, return_code, updateGUI):
while return_code == Move.JUMP_AVAILABLE:
moves = board.getAllMoves(state)
if not moves:
break
move = moves.pop()
end = move.pop()
start = move.pop()
last_cell = board.board[start.row][start.column]
return_code = board.move(start, end, state)
if updateGUI:
self.checkers.move(start, end, last_cell, return_code)