-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathagent.py
74 lines (61 loc) · 2.34 KB
/
agent.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
#!/usr/bin/env python3
from game.minesweeper import *
from game.artificial_agent import ArtificialAgent
import sys
from os.path import dirname
from typing import List, Tuple
# hack for importing from parent package
sys.path.append(dirname(dirname(dirname(__file__))))
from csp_templates import *
from solver import Solver
class Agent(ArtificialAgent):
"""
Logic implementation for Minesweeper ArtificialAgent.
See ArtificialAgent for details.
"""
def __init__(self, verbose: int) -> None:
super().__init__(verbose)
# you can add your instance variables here
def new_game(self) -> None:
"""Agent got into a new level."""
super().new_game()
# you can reset your instance variables here
def think_impl(self, board: Board, previous_board: Board) -> Action:
"""
Code your custom agent here.
The existing dummy implementation always asks for a hint.
The Board object passed to think_impl gives you the current board state.
Check ArtificialAgent.think_impl docstring for more info.
"""
return ActionFactory.get_advice_action()
def reset_lists(self, board: Board) -> None:
"""Example."""
# reset
self.border_unknown = []
self.border_numbers = []
self.flagged = []
# query tiles
for (x, y), tile in board.generator():
if not tile.visible:
if tile.flag:
self.flagged.append((x, y))
# test border tile
if self.is_border_tile(x, y, board):
self.border_unknown.append((x, y))
elif tile.mines_around > 0:
# test border tile
if self.is_border_tile(x, y, board):
self.border_numbers.append((x, y))
def is_border_tile(self, x: int, y: int, board: Board) -> bool:
"""Example."""
width, height = board.width, board.height
is_border = False
for dy in range(-1 if y > 0 else 0, 2 if y < height - 1 else 1):
if is_border:
break
for dx in range(-1 if x > 0 else 0, 2 if x < width - 1 else 1):
next_tile: Tile = board.tile(x + dx, y + dy)
if next_tile.visible:
is_border = True
break
return is_border