-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlayer.py
148 lines (115 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
147
import numpy as np
class AIPlayer:
def __init__(self, player_number):
self.player_number = player_number
self.type = 'ai'
self.player_string = 'Player {}:ai'.format(player_number)
def get_alpha_beta_move(self, board):
"""
Given the current state of the board, return the next move based on
the alpha-beta pruning algorithm
This will play against either itself or a human player
INPUTS:
board - a numpy array containing the state of the board using the
following encoding:
- the board maintains its same two dimensions
- row 0 is the top of the board and so is
the last row filled
- spaces that are unoccupied are marked as 0
- spaces that are occupied by player 1 have a 1 in them
- spaces that are occupied by player 2 have a 2 in them
RETURNS:
The 0 based index of the column that represents the next move
"""
raise NotImplementedError('Whoops I don\'t know what to do')
def get_expectimax_move(self, board):
"""
Given the current state of the board, return the next move based on
the expectimax algorithm.
This will play against the random player, who chooses any valid move
with equal probability
INPUTS:
board - a numpy array containing the state of the board using the
following encoding:
- the board maintains its same two dimensions
- row 0 is the top of the board and so is
the last row filled
- spaces that are unoccupied are marked as 0
- spaces that are occupied by player 1 have a 1 in them
- spaces that are occupied by player 2 have a 2 in them
RETURNS:
The 0 based index of the column that represents the next move
"""
raise NotImplementedError('Whoops I don\'t know what to do')
def evaluation_function(self, board):
"""
Given the current stat of the board, return the scalar value that
represents the evaluation function for the current player
INPUTS:
board - a numpy array containing the state of the board using the
following encoding:
- the board maintains its same two dimensions
- row 0 is the top of the board and so is
the last row filled
- spaces that are unoccupied are marked as 0
- spaces that are occupied by player 1 have a 1 in them
- spaces that are occupied by player 2 have a 2 in them
RETURNS:
The utility value for the current board
"""
return 0
class RandomPlayer:
def __init__(self, player_number):
self.player_number = player_number
self.type = 'random'
self.player_string = 'Player {}:random'.format(player_number)
def get_move(self, board):
"""
Given the current board state select a random column from the available
valid moves.
INPUTS:
board - a numpy array containing the state of the board using the
following encoding:
- the board maintains its same two dimensions
- row 0 is the top of the board and so is
the last row filled
- spaces that are unoccupied are marked as 0
- spaces that are occupied by player 1 have a 1 in them
- spaces that are occupied by player 2 have a 2 in them
RETURNS:
The 0 based index of the column that represents the next move
"""
valid_cols = []
for col in range(board.shape[1]):
if 0 in board[:,col]:
valid_cols.append(col)
return np.random.choice(valid_cols)
class HumanPlayer:
def __init__(self, player_number):
self.player_number = player_number
self.type = 'human'
self.player_string = 'Player {}:human'.format(player_number)
def get_move(self, board):
"""
Given the current board state returns the human input for next move
INPUTS:
board - a numpy array containing the state of the board using the
following encoding:
- the board maintains its same two dimensions
- row 0 is the top of the board and so is
the last row filled
- spaces that are unoccupied are marked as 0
- spaces that are occupied by player 1 have a 1 in them
- spaces that are occupied by player 2 have a 2 in them
RETURNS:
The 0 based index of the column that represents the next move
"""
valid_cols = []
for i, col in enumerate(board.T):
if 0 in col:
valid_cols.append(i)
move = int(input('Enter your move: '))
while move not in valid_cols:
print('Column full, choose from:{}'.format(valid_cols))
move = int(input('Enter your move: '))
return move