-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartRandom.py
54 lines (37 loc) · 1.42 KB
/
SmartRandom.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
import random, time
class SmartRandom:#class name is the same as file name
def __init__(self, empty, me, opponent):
self.empty = empty
self.me = me
self.opponent = opponent
self.seed = time.time()
self.board = []
def play(self):
random.seed(self.seed)
self.seed += 1
slots, empties, blocks = self.readboard()
if slots:
return random.choice(slots)
if blocks:
return random.choice(blocks)
return random.choice(empties)
def surrounding(self, x, y):
coords = []
endx, endy = min(x+2,len(self.board)), min(y+2, len(self.board[0]))
for i in range(x-1, endx):
for j in range(y-1, endy):
if self.board[i][j] == self.empty:
coords.append((i, j))
return coords
def readboard(self):
slots, empties, blocks = [], [], []
rng = range(len(self.board))
for x in rng:
for y in rng:
if self.board[x][y] == self.me:
slots.extend(self.surrounding(x, y))
elif self.board[x][y] == self.opponent:
blocks.extend(self.surrounding(x, y))
else:
empties.append((x, y))
return slots, empties, blocks