diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-18 13:10:21 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-18 13:10:21 +0200 |
commit | 024f743255a5335e0ff3448ab2ca799a74e538b4 (patch) | |
tree | 333fa12393fa568f1ccf4ad4b12a25a97b5bd826 | |
parent | 88b9ac285490a0d9da0342a8c9c61ed8d9e93919 (diff) |
Refactor more bots/bot.py
-rw-r--r-- | src/miinaharava/bots/bot.py | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/src/miinaharava/bots/bot.py b/src/miinaharava/bots/bot.py index 20dfe5b..81f24d4 100644 --- a/src/miinaharava/bots/bot.py +++ b/src/miinaharava/bots/bot.py @@ -34,12 +34,10 @@ class Bot(): def saved_hints(self): """ Kertoo onko miinojen tai vapaiden joukossa jäljellä vihjeitä. Siivoaa samalla joukoista jo avatut laatat.""" - for tile in list(self.safe_tiles): - if self.known_tile(tile): - self.safe_tiles.remove(tile) - for tile in list(self.mine_tiles): - if self.known_tile(tile): - self.mine_tiles.remove(tile) + for tiles in (self.safe_tiles, self.mine_tiles): + for tile in list(tiles): + if self.known_tile(tile): + tiles.remove(tile) return self.safe_tiles or self.mine_tiles def hint(self, matrix, cursor_x, cursor_y): @@ -47,13 +45,11 @@ class Bot(): vain nykyisen paikan ilman toimintoa. """ self.matrix = matrix self.w, self.h = self.get_dimensions() - - if self.saved_hints(): - return self.get_hint_from_list() - if self.search(): - return self.get_hint_from_list() - if self.uncertain and self.lucky_guess(): - return self.get_hint_from_list() + def ok_to_guess(): + return self.lucky_guess() if self.uncertain else False + for step in (self.saved_hints, self.search, ok_to_guess ): + if step(): + return self.get_hint_from_list() return Action.NOOP, cursor_x, cursor_y def get_dimensions(self): @@ -114,9 +110,8 @@ class Bot(): for x in range(self.w): for y in range(self.h): if self.number_tile((x,y)): - neighbours = self.get_neighbours((x,y)) - unknown_count = self.count_unknowns(neighbours) - if unknown_count in range(1,len(neighbours)-1): + nbrs = self.get_neighbours((x,y)) + if self.count_unknowns(nbrs) in range(1,len(nbrs)-1): tiles.add((x,y)) return tiles @@ -127,8 +122,7 @@ class Bot(): for x in range(self.w): for y in range(self.h): if self.number_tile((x,y)): - n = self.get_neighbours((x,y)) - if self.count_unknowns(n): + if self.count_unknowns(self.get_neighbours((x,y))): tiles.add((x,y)) return tiles |