diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-17 13:16:56 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-17 13:16:56 +0200 |
commit | 9acab08eeede3a3c065057d4466ccaef2035b55d (patch) | |
tree | 9145cf28a8dfe2e4dc5eb0aea587bdd35c7ac1ca | |
parent | 823877510422f6ce2952de5cc40edd0d62d813e4 (diff) |
Making board compatible with non square playing areas like expert 30x16.
-rw-r--r-- | app.py | 2 | ||||
-rw-r--r-- | board/board.py | 55 | ||||
-rw-r--r-- | game/game.py | 5 | ||||
-rw-r--r-- | tests/test_board.py | 21 | ||||
-rw-r--r-- | tui/tui.py | 6 |
5 files changed, 55 insertions, 34 deletions
@@ -9,7 +9,7 @@ from bots.bad import BadBot class App: """ App - Luokka pääohjelmalle""" def __init__(self): - self.board = Board() + self.board = Board(30,16,99) 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 b6bd25b..33d2ab3 100644 --- a/board/board.py +++ b/board/board.py @@ -7,36 +7,44 @@ class Board(): """ Board - Luokka joka pitää huolen pelilaudasta ja siihen kohdistuvista siirroista. """ - def __init__(self, size = 9, bombs = 10): + def __init__(self, width = 9, height = 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 - self.size = size + #size = 2 if size < 2 else size + #size = 50 if size > 50 else size + #self.size = size + width = 2 if width < 2 else width + width = 50 if width > 50 else width + self.width = width + + height = 2 if height < 2 else height + height = 50 if height > 50 else height + self.height = height # Pommeja pitää olla vähintään yksi, kuten tyhjiäkin - bombs = size*size*size//100 if bombs < 1 else bombs - bombs = size*size-1 if bombs>=size*size else bombs + #bombs = size*size*size//100 if bombs < 1 else bombs + bombs = width*height-1 if bombs>=width*height else bombs bombs = 1 if bombs == 0 else bombs self.bombs = bombs self.tiles = [] self.masked = [] - self.initialize_tiles( size ) + self.initialize_tiles() self.randomize_bombs( bombs ) self.calculate_neighbours() - def initialize_tiles(self, size): + def initialize_tiles(self): """ alustaa pelilaudan matriisit """ - self.tiles = [[0 for _ in range(size)] for _ in range(size)] - self.masked = [[12 for _ in range(size)] for _ in range(size)] + w, h = self.width, self.height + self.tiles = [[0 for _ in range(h)] for _ in range(w)] + self.masked = [[12 for _ in range(h)] for _ in range(w)] def randomize_bombs(self, bomb_count): """ arpoo pelilaudalle pommit """ for _ in range(bomb_count): while True: - x, y = randrange(0,self.size), randrange(0,self.size) + x, y = randrange(0,self.width), randrange(0,self.height) if self.tiles[x][y] != 0: continue self.tiles[x][y]=9 @@ -45,8 +53,8 @@ class Board(): def calculate_neighbours(self): """ laskee naapurissa olevien pommien määrät valmiiksi laudalle """ - for y in range(self.size): - for x in range(self.size): + for y in range(self.height): + for x in range(self.width): if self.tiles[x][y] == 9: continue neighbouring_bombs = 0 @@ -58,7 +66,7 @@ class Board(): def invalid_coordinates(self, x, y): """ onko koordinaatit pelilaudan ulkopuolella """ - return x < 0 or x >= self.size or y < 0 or y >= self.size + return x < 0 or x >= self.width or y < 0 or y >= self.height def get_neighbours_coords(self, x, y, include_home = False): @@ -82,8 +90,8 @@ class Board(): def get_view(self): """ antaa matriisin nykyisestä pelinäkymästä """ view = deepcopy(self.masked) - for y in range(self.size): - for x in range(self.size): + for y in range(self.height): + for x in range(self.width): if not view[x][y]: view[x][y]=self.tiles[x][y] return view @@ -91,8 +99,8 @@ class Board(): def is_winning(self): """ tarkistaa onko peli voitettu """ - for y in range(self.size): - for x in range(self.size): + for y in range(self.height): + for x in range(self.width): if self.tiles[x][y] == 9: if not self.masked[x][y]: return False @@ -167,4 +175,13 @@ class Board(): def reveal(self): """ näytä koko lauta """ - self.masked = [[0 for _ in range(self.size)] for _ in range(self.size)] + w, h = self.width, self.height + self.masked = [[0 for _ in range(h)] for _ in range(w)] + + def get_width(self): + """ palauttaa laudan leveyden """ + return self.width + + def get_height(self): + """ palauttaa laudan korkeuden """ + return self.height diff --git a/game/game.py b/game/game.py index 5ccbab9..4420aed 100644 --- a/game/game.py +++ b/game/game.py @@ -6,7 +6,10 @@ class Game: def __init__(self, board, ui): self.board = board self.ui = ui - self.x, self.y = self.ui.game_begin(self.board.size) + self.x, self.y = self.ui.game_begin( + self.board.get_width(), + self.board.get_height() + ) def __del__(self): diff --git a/tests/test_board.py b/tests/test_board.py index 1bbd079..a74be9a 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -8,16 +8,17 @@ class TestBoardClass(unittest.TestCase): def test_init(self): """ olion luominen onnistuu """ b = Board() - self.assertTrue(b.size>0) + self.assertTrue(b.get_width()>0) def test_init_with_size(self): """ olion luominen onnistuu tietyllä koolla""" - b = Board(15) - self.assertEqual(b.size, 15) + b = Board(30, 15) + self.assertEqual(b.get_width(), 30) + self.assertEqual(b.get_height(), 15) def test_get_view_and_guess(self): """ laudan näkymä on oikein senkin jälkeen kun on arvattu""" - b = Board(3) + b = Board(3,3) b.tiles=[[0,0,0],[0,1,1],[0,1,9]] v = b.get_view() @@ -35,7 +36,7 @@ class TestBoardClass(unittest.TestCase): def test_is_winning(self): """ toimiiko voittotilanteen tunnistus """ - b = Board(2) + b = Board(2,2) b.tiles=[[1,9],[9,9]] b.masked=[[12,12],[12,12]] self.assertFalse(b.is_winning()) @@ -46,7 +47,7 @@ class TestBoardClass(unittest.TestCase): def test_error_conditions_in_guess(self): """ ruudun avaus alueen ulkopuolelta tai avatussa ruudussa ei onnistu""" - b = Board(2) + b = Board(2,2) b.tiles=[[1,9],[9,9]] self.assertFalse(b.guess(2,2)) self.assertTrue(b.guess(0,0)) @@ -54,13 +55,13 @@ class TestBoardClass(unittest.TestCase): def test_get_mask(self): """ maski annetaan oikein """ - b = Board(2) + b = Board(2,2) b.tiles=[[1,9],[9,9]] self.assertEqual(b.get_mask(0,0), 12) def test_flag(self): """ ruudun liputus toimii """ - b = Board(2) + b = Board(2,2) b.tiles=[[1,9],[9,9]] self.assertEqual(b.get_mask(0,0), 12) self.assertTrue(b.flag(0,0)) @@ -76,7 +77,7 @@ class TestBoardClass(unittest.TestCase): def test_flag_error_conditions(self): """ liputus ei onnistu jos avattu, alueen ulkopuolella, outo arvo """ - b = Board(2) + b = Board(2,2) b.tiles=[[1,9],[9,9]] b.masked[0][0]=6 self.assertFalse(b.flag(0,0)) @@ -88,7 +89,7 @@ class TestBoardClass(unittest.TestCase): def test_reveal(self): """ paljastuksen jälkeen näkyy laatat sellaisenaan """ - b = Board(2) + b = Board(2,2) b.reveal() t = b.tiles v = b.get_view() @@ -110,11 +110,11 @@ class Tui(): self.read_action() - def game_begin(self, size): + def game_begin(self, width, height): """ ruudun alustus ja lähtökoordinaatien määritys """ - print(end="\n"*(size+1)) + print(end="\n"*(height+1)) Ansi.cup(1) - return size//2, size//2 + return width//2, height//2 def game_over(self, matrix, x, y): |