forked from 4belito/Connect4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummies.py
98 lines (63 loc) · 2.17 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
import time
import random
import numpy as np
from player import Player
rows, cols = 7, 8
class Random(Player):
'''
Drop the disc randomly on any of the columns of the board
'''
def __init__(self, rows=rows, columns=cols):
self.rows=rows
self.cols=columns
def setup(self):
pass
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=rows, columns=cols):
self.rows=rows
self.cols=columns
def setup(self):
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=rows, columns=cols):
self.rows=rows
self.cols=columns
def setup(self):
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=rows, columns=cols):
self.rows=rows
self.cols=columns
def setup(self):
pass
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)