-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdummies.py
129 lines (92 loc) · 3.53 KB
/
dummies.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
import time
import random
import numpy as np
from player import Player
class Random(Player):
'''
Drop the disc randomly on any of the columns of the board
'''
def __init__(self, rows, cols, connect_number,
timeout_setup, timeout_move, max_invalid_moves,
cylinder):
self.rows = rows
self.cols = cols
self.connect_number = connect_number
self.timeout_setup = timeout_setup
self.timeout_move = timeout_move
self.max_invalid_moves = max_invalid_moves
self.cylinder = cylinder
def setup(self,piece_color):
self.piece_color=piece_color
def play(self, board: np.ndarray) -> int:
return np.random.randint(self.cols)
class SmartRandom(Player):
'''
Randomly drop the disc into any column on the board that is not full.
'''
def __init__(self, rows, cols, connect_number,
timeout_setup, timeout_move, max_invalid_moves,
cylinder):
self.rows = rows
self.cols = cols
self.connect_number = connect_number
self.timeout_setup = timeout_setup
self.timeout_move = timeout_move
self.max_invalid_moves = max_invalid_moves
self.cylinder = cylinder
def setup(self,piece_color):
self.piece_color=piece_color
self.moves=np.arange(self.cols)
def play(self, board: np.ndarray) -> int:
valid_moves = self.valid_moves(board)
i = np.random.randint(0, len(valid_moves))
return valid_moves[i]
def valid_moves(self, board: np.ndarray) -> np.ndarray:
return self.moves[board[0,:]==0]
class LazySmartRandom(Player):
'''
Similar to SmartRandom, but wait for some time before each move and during the setup.
'''
def __init__(self, rows, cols, connect_number,
timeout_setup, timeout_move, max_invalid_moves,
cylinder):
self.rows = rows
self.cols = cols
self.connect_number = connect_number
self.timeout_setup = timeout_setup
self.timeout_move = timeout_move
self.max_invalid_moves = max_invalid_moves
self.cylinder = cylinder
def setup(self,piece_color):
self.piece_color=piece_color
self.moves=np.arange(self.cols)
time.sleep(0.99)
print(self.__class__.__name__ + ': ...Hmm?')
def play(self, board: np.ndarray) -> int:
time.sleep(random.random() + 0.05)
valid_moves = self.valid_moves(board)
i = np.random.randint(0, len(valid_moves))
return valid_moves[i]
def valid_moves(self, board: np.ndarray) -> np.ndarray:
return self.moves[board[0,:]==0]
class DropLowest(Player):
'''
Drop the disc in the column with fewer discs, breaking ties by choosing the leftmost column
'''
def __init__(self, rows, cols, connect_number,
timeout_setup, timeout_move, max_invalid_moves,
cylinder):
self.rows = rows
self.cols = cols
self.connect_number = connect_number
self.timeout_setup = timeout_setup
self.timeout_move = timeout_move
self.max_invalid_moves = max_invalid_moves
self.cylinder = cylinder
def setup(self,piece_color):
self.piece_color=piece_color
def play(self, board: np.ndarray) -> int:
col_filled=self.cols_filled(board)
return np.argmin(col_filled)
def cols_filled(self, board: np.ndarray) -> np.ndarray:
return np.sum(board!=0,axis=0)