diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-17 10:51:08 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-17 10:51:08 +0200 |
commit | 823877510422f6ce2952de5cc40edd0d62d813e4 (patch) | |
tree | 1e68d682a3f59dea0a87aa83c9bb1c73aeb2badb | |
parent | 899d0997a6badae6535e9f69e0f6d70f3a272578 (diff) |
Adding BadBot for hinting.
-rw-r--r-- | app.py | 5 | ||||
-rw-r--r-- | board/board.py | 4 | ||||
-rw-r--r-- | bots/bad.py | 58 | ||||
-rw-r--r-- | bots/bot.py | 23 | ||||
-rw-r--r-- | bots/idiot.py | 8 | ||||
-rw-r--r-- | doc/muistilista.txt | 3 | ||||
-rw-r--r-- | doc/määrittelydokumentti.pdf | bin | 25993 -> 26693 bytes | |||
-rw-r--r-- | game/game.py | 4 | ||||
-rw-r--r-- | tests/test_board.py | 22 | ||||
-rw-r--r-- | tui/static.py | 6 |
10 files changed, 106 insertions, 27 deletions
@@ -2,14 +2,15 @@ from board.board import Board from tui.tui import Tui from game.game import Game -from bots.idiot import IdiotBot +#from bots.idiot import IdiotBot +from bots.bad import BadBot # pylint: disable = too-few-public-methods class App: """ App - Luokka pääohjelmalle""" def __init__(self): self.board = Board() - self.bot = IdiotBot() + self.bot = BadBot() self.ui = Tui(self.bot) self.game = Game(self.board,self.ui) diff --git a/board/board.py b/board/board.py index 0b4c3a2..b6bd25b 100644 --- a/board/board.py +++ b/board/board.py @@ -7,7 +7,7 @@ class Board(): """ Board - Luokka joka pitää huolen pelilaudasta ja siihen kohdistuvista siirroista. """ - def __init__(self, size = 10, bombs = 0): + def __init__(self, size = 9, bombs = 10): # Lauta pitää olla vähintään 2x2, jotta on jotain pelattavaa size = 2 if size < 2 else size size = 50 if size > 50 else size @@ -29,7 +29,7 @@ class Board(): def initialize_tiles(self, size): """ alustaa pelilaudan matriisit """ self.tiles = [[0 for _ in range(size)] for _ in range(size)] - self.masked = [[10 for _ in range(size)] for _ in range(size)] + self.masked = [[12 for _ in range(size)] for _ in range(size)] def randomize_bombs(self, bomb_count): diff --git a/bots/bad.py b/bots/bad.py new file mode 100644 index 0000000..1419fc0 --- /dev/null +++ b/bots/bad.py @@ -0,0 +1,58 @@ +""" bots/bad.py - botti joka ehkä osaa merkata jonkun asian """ +from bots.bot import Bot +from tui.static import Action +class BadBot(Bot): + """ IdiotBot - merkistsee kaikki turvallisiksi avata """ + # pylint: disable = too-few-public-methods + def missing_bombs(self, matrix, x, y): + """ test how many boms are not found at the coordinate """ + dx = len(matrix) + dy = len(matrix[0]) + bcount = 0 + for nx, ny in self.neighbours(dx, dy, x, y): + if matrix[nx][ny] in (9,11): + bcount+=1 + return matrix[x][y]-bcount + + def get_tiles_at_border(self, matrix): + """ get interesting tiles on the border of cleared and masked area """ + tiles = [] + w = len(matrix) + h = len(matrix[0]) + for y in range(h): + for x in range(w): + if matrix[x][y] < 12: + open_tiles=1 + masked_tiles=0 + else: + open_tiles=0 + masked_tiles=1 + for nx, ny in self.neighbours(w, h, x, y): + if matrix[nx][ny] < 12: + open_tiles+=1 + else: + masked_tiles+=1 + if open_tiles and masked_tiles: + tiles.append( (x,y) ) + return tiles + + + def hint(self, matrix, cursor_x, cursor_y): + """ merkitsee jonkin ruudun """ + super().hint(matrix, cursor_x, cursor_y) + w = len(matrix) + h = len(matrix[0]) + # pylint: disable = consider-using-enumerate + for x, y in self.get_tiles_at_border(matrix): + ncoords=self.neighbours(w,h,x,y) + ntiles=self.coordinates_to_tiles(matrix,ncoords) + unopened=ntiles.count(12) + bombs=ntiles.count(10) + if unopened: + if matrix[x][y]<9 and matrix[x][y]==bombs: + safe = ncoords[ntiles.index(12)] + return(Action.SAFE, safe[0], safe[1]) + if matrix[x][y]-bombs==unopened: + bomb = ncoords[ntiles.index(12)] + return(Action.BOMB, bomb[0], bomb[1]) + return (Action.NOOP, cursor_x, cursor_y) diff --git a/bots/bot.py b/bots/bot.py index 1b7fedd..8466c51 100644 --- a/bots/bot.py +++ b/bots/bot.py @@ -3,12 +3,29 @@ from tui.static import Action class Bot(): """ Bot - perusluokka perittäväksi """ - # pylint: disable = too-few-public-methods def __init__(self): self.hints = 0 - def hint(self, matrix, x, y): + def neighbours(self,dx,dy,x,y): + """ palauttaa listana viereiset koordinaatit """ + offsets = ( + (-1, -1), ( 0, -1), ( 1, -1), + (-1, 0), ( 1, 0), + (-1, 1), ( 0, 1), ( 1, 1), + ) + coords=[] + for ox, oy in offsets: + if ox+x in range(dx): + if oy+y in range(dy): + coords.append((ox+x, oy+y)) + return coords + + def coordinates_to_tiles(self, matrix, coords): + """ lukee koordinaateissa olevien ruutujen arvot listaksi """ + return [matrix[x][y] for x,y in coords] + + def hint(self, matrix, cursor_x, cursor_y): """ antaa vinkin. tässä tapauksessa ei mitään """ # pylint: disable = unused-argument self.hints += 1 - return Action.NOOP, x, y + return Action.NOOP, cursor_x, cursor_y diff --git a/bots/idiot.py b/bots/idiot.py index 4f1f9f4..200d690 100644 --- a/bots/idiot.py +++ b/bots/idiot.py @@ -5,13 +5,13 @@ class IdiotBot(Bot): """ IdiotBot - merkistsee kaikki turvallisiksi avata """ # pylint: disable = too-few-public-methods - def hint(self, matrix, x, y): + def hint(self, matrix, cursor_x, cursor_y): """ merkitsee jonkin ruudun """ - super().hint(matrix, x, y) + super().hint(matrix, cursor_x, cursor_y) # pylint: disable = consider-using-enumerate for ty in range(len(matrix[0])): for tx in range(len(matrix)): - if matrix[tx][ty]==10: + if matrix[tx][ty]==12: return(Action.SAFE, tx, ty) - return (Action.NOOP, x, y) + return (Action.NOOP, cursor_x, cursor_y)
\ No newline at end of file diff --git a/doc/muistilista.txt b/doc/muistilista.txt index 6ee2281..d163048 100644 --- a/doc/muistilista.txt +++ b/doc/muistilista.txt @@ -5,3 +5,6 @@ poetry run python3 -m coverage run --branch -m pytest -v && poetry run python3 - Linttaus poetry run python3 -m pylint -v . + +riippuvuuksien asennus: +PYTHON_KEYRING_BACKEND=keyring.backends.fail.Keyring poetry install --no-root diff --git a/doc/määrittelydokumentti.pdf b/doc/määrittelydokumentti.pdf Binary files differindex 5268088..414bcb6 100644 --- a/doc/määrittelydokumentti.pdf +++ b/doc/määrittelydokumentti.pdf diff --git a/game/game.py b/game/game.py index 0a36b0a..5ccbab9 100644 --- a/game/game.py +++ b/game/game.py @@ -35,7 +35,7 @@ class Game: case Action.FLAG: self.board.flag(self.x, self.y) case Action.BOMB: - self.board.flag(self.x, self.y, 11) + self.board.flag(self.x, self.y, 10) case Action.SAFE: - self.board.flag(self.x, self.y, 12) + self.board.flag(self.x, self.y, 11) return True diff --git a/tests/test_board.py b/tests/test_board.py index 82a2015..1bbd079 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -21,13 +21,13 @@ class TestBoardClass(unittest.TestCase): b.tiles=[[0,0,0],[0,1,1],[0,1,9]] v = b.get_view() - t = [[10,10,10],[10,10,10],[10,10,10]] + t = [[12,12,12],[12,12,12],[12,12,12]] for i in range(3): self.assertEqual(v[i],t[i]) self.assertTrue(b.guess(0,0)) v = b.get_view() - t = [[0,0,0],[0,1,1],[0,1,10]] + t = [[0,0,0],[0,1,1],[0,1,12]] for i in range(3): self.assertEqual(v[i],t[i]) @@ -37,11 +37,11 @@ class TestBoardClass(unittest.TestCase): """ toimiiko voittotilanteen tunnistus """ b = Board(2) b.tiles=[[1,9],[9,9]] - b.masked=[[10,10],[10,10]] + b.masked=[[12,12],[12,12]] self.assertFalse(b.is_winning()) - b.masked=[[0,10],[10,10]] + b.masked=[[0,12],[12,12]] self.assertTrue(b.is_winning()) - b.masked=[[0,0],[10,10]] + b.masked=[[0,0],[12,12]] self.assertFalse(b.is_winning()) def test_error_conditions_in_guess(self): @@ -56,23 +56,23 @@ class TestBoardClass(unittest.TestCase): """ maski annetaan oikein """ b = Board(2) b.tiles=[[1,9],[9,9]] - self.assertEqual(b.get_mask(0,0), 10) + self.assertEqual(b.get_mask(0,0), 12) def test_flag(self): """ ruudun liputus toimii """ b = Board(2) b.tiles=[[1,9],[9,9]] + self.assertEqual(b.get_mask(0,0), 12) + self.assertTrue(b.flag(0,0)) + self.assertEqual(b.get_mask(0,0), 13) + self.assertTrue(b.flag(0,0)) self.assertEqual(b.get_mask(0,0), 10) self.assertTrue(b.flag(0,0)) self.assertEqual(b.get_mask(0,0), 11) self.assertTrue(b.flag(0,0)) self.assertEqual(b.get_mask(0,0), 12) - self.assertTrue(b.flag(0,0)) - self.assertEqual(b.get_mask(0,0), 13) - self.assertTrue(b.flag(0,0)) + self.assertTrue(b.flag(0,0,10)) self.assertEqual(b.get_mask(0,0), 10) - self.assertTrue(b.flag(0,0,13)) - self.assertEqual(b.get_mask(0,0), 13) def test_flag_error_conditions(self): """ liputus ei onnistu jos avattu, alueen ulkopuolella, outo arvo """ diff --git a/tui/static.py b/tui/static.py index 98b6985..391e3f1 100644 --- a/tui/static.py +++ b/tui/static.py @@ -67,8 +67,8 @@ TileTypes = { 7: TileType( "[7]", [(0x9,0), (0x9,0), (0x9,0)] ), 8: TileType( "[8]", [(0x9,0), (0x9,0), (0x9,0)] ), 9: TileType( "[@]", [(0xF,1), (0xF,1), (0xF,1)] ), - 10: TileType( "[#]", [(0x8,7), (0x8,7), (0x8,7)] ), - 11: TileType( "[×]", [(0x8,7), (0x1,7), (0x8,7)] ), - 12: TileType( "[•]", [(0x8,7), (0x2,7), (0x8,7)] ), + 10: TileType( "[×]", [(0x8,7), (0x1,7), (0x8,7)] ), + 11: TileType( "[•]", [(0x8,7), (0x2,7), (0x8,7)] ), + 12: TileType( "[#]", [(0x8,7), (0x8,7), (0x8,7)] ), 13: TileType( "[?]", [(0x8,7), (0x0,7), (0x8,7)] ) } |