-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandomBot.py
46 lines (36 loc) · 1.45 KB
/
RandomBot.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
from Bot import Bot
from GameAction import GameAction
from GameState import GameState
import random
import numpy as np
class RandomBot(Bot):
def get_action(self, state: GameState) -> GameAction:
all_row_marked = np.all(state.row_status == 1)
all_col_marked = np.all(state.col_status == 1)
if not (all_row_marked or all_col_marked):
return self.get_random_action(state)
elif all_row_marked:
return self.get_random_col_action(state)
else:
return self.get_random_row_action(state)
def get_random_action(self, state: GameState) -> GameAction:
if random.random() < 0.5:
return self.get_random_row_action(state)
else:
return self.get_random_col_action(state)
def get_random_row_action(self, state: GameState) -> GameAction:
position = self.get_random_position_with_zero_value(state.row_status)
return GameAction("row", position)
def get_random_position_with_zero_value(self, matrix: np.ndarray):
[ny, nx] = matrix.shape
x = -1
y = -1
valid = False
while not valid:
x = random.randrange(0, nx)
y = random.randrange(0, ny)
valid = matrix[y, x] == 0
return (x, y)
def get_random_col_action(self, state: GameState) -> GameAction:
position = self.get_random_position_with_zero_value(state.col_status)
return GameAction("col", position)