diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-13 12:08:49 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-13 12:08:49 +0200 |
commit | 834150fe61eb79654d57067b4a96bf8fd1718b25 (patch) | |
tree | 3ab38620a8dd2d8fa119f5d430d2d3ddf65e17b2 /board | |
parent | 1ac23423308e9dcb662cdaf9709b8e28787b5a2b (diff) |
Refactoring neighbour finding code and making sanitychecks for board size.
Diffstat (limited to 'board')
-rw-r--r-- | board/board.py | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/board/board.py b/board/board.py index 23dfa79..741edf6 100644 --- a/board/board.py +++ b/board/board.py @@ -3,12 +3,22 @@ from sys import stderr from copy import deepcopy class Board(): - def __init__(self, size = 10): + def __init__(self, size = 10, bombs = 0): + # 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 + + # 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 = 1 if bombs == 0 else bombs + self.bombs = bombs + self.tiles = [] self.masked = [] self.initialize_tiles( size ) - self.randomize_bombs( size ) + self.randomize_bombs( bombs ) self.calculate_neighbours() def initialize_tiles(self, size): @@ -26,20 +36,28 @@ class Board(): def invalid_coordinates(self, x, y): return x < 0 or x >= self.size or y < 0 or y >= self.size + + def get_neighbours_coords(self, x, y, coordinates = None): + if not coordinates: + coordinates=[] + offsets = ( + (-1,-1), (0,-1), (1,-1), + (-1, 0), (1, 0), + (-1, 1), (0, 1), (1, 1) + ) + for dx,dy in offsets: + if not self.invalid_coordinates(x+dx, y+dy): + coordinates.append( (x+dx, y+dy) ) + return coordinates def calculate_neighbours(self): - neighbours = ( (-1,-1), (0,-1), (1,-1), - (-1, 0), (1, 0), - (-1, 1), (0, 1), (1, 1) ) for y in range(self.size): for x in range(self.size): if self.tiles[x][y] == 9: continue neighbouring_bombs = 0 - for dx,dy in neighbours: - if self.invalid_coordinates(x+dx, y+dy): - continue - if self.tiles[x+dx][y+dy] == 9: + for nx, ny in self.get_neighbours_coords(x,y): + if self.tiles[nx][ny] == 9: neighbouring_bombs += 1 self.tiles[x][y] = neighbouring_bombs @@ -92,9 +110,8 @@ class Board(): if self.tiles[x][y] == 0: for cx, cy in self.collect_area( x, y ): - for dx, dy in ( (0,-1), (-1,0), (0,0), (1,0), (0,1) ): - if not self.invalid_coordinates(cx+dx, cy+dy): - self.masked[cx+dx][cy+dy] = 0 + for nx, ny in self.get_neighbours_coords(cx, cy, [(0,0)]): + self.masked[nx][ny] = 0 return True |