-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleagent.py
executable file
·63 lines (58 loc) · 2.8 KB
/
simpleagent.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
from game import Minesweeper
from random import Random
class SimpleAgent:
def __init__(self, game: Minesweeper, seed=None):
self.game = game
self.random = Random(seed)
def primitive(self, c: tuple[int, int]) -> tuple[set[tuple[int, int]], set[tuple[int, int]]]:
to_step = set()
to_flag = set()
not_stepped_neighbours = self.game.neighbours(c) - self.game.stepped
if len(not_stepped_neighbours) == self.game.cell_value(c):
to_flag = not_stepped_neighbours - self.game.flagged
if len(not_stepped_neighbours & self.game.flagged) == self.game.cell_value(c):
to_step = not_stepped_neighbours - self.game.flagged
return to_step, to_flag
def play(self, **kwargs):
show_mines = kwargs['show_mines'] if "show_mines" in kwargs.keys() else False
coloured = kwargs['coloured'] if 'coloured' in kwargs.keys() else False
verbosity = kwargs['verbosity'] if 'verbosity' in kwargs.keys() else 0
# the tiles we know we need to step onto next
to_step = {(self.random.randrange(self.game.row_count), self.random.randrange(self.game.col_count))}
# the tiles we know to flag next
to_flag = set()
# the tiles we have already stepped onto but have yet to use the information of
to_search = set()
while self.game.flagged | self.game.stepped != self.game.grid:
# flag a tile we know we should flag
if len(to_flag) > 0:
tile = to_flag.pop()
self.game.flag(tile)
for neighbour in self.game.neighbours(tile) & self.game.stepped:
to_search.add(neighbour)
# step onto a tile we know we should step onto
if len(to_step) > 0:
tile = to_step.pop()
try:
self.game.step(tile)
except ValueError:
break
to_search.add(tile)
for neighbour in self.game.neighbours(tile) & self.game.stepped:
to_search.add(neighbour)
# search a tile we know we should search
if len(to_search) > 0:
tile = to_search.pop()
new_steps, new_flags = self.primitive(tile)
to_step |= new_steps
to_flag |= new_flags
if len(to_step) + len(to_flag) + len(to_search) == 0:
try:
tile = self.random.choice(list(self.game.grid - self.game.flagged - self.game.stepped))
to_step.add(tile)
except IndexError:
break
if verbosity > 2:
self.game.draw(show_mines=show_mines, coloured=coloured)
if verbosity > 1:
self.game.draw(show_mines=show_mines, coloured=coloured)