diff options
Diffstat (limited to 'board/board.py')
-rw-r--r-- | board/board.py | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/board/board.py b/board/board.py index e457a1a..3b5cd77 100644 --- a/board/board.py +++ b/board/board.py @@ -23,6 +23,9 @@ class Board(): continue self.tiles[x][y]=9 break + + def invalid_coordinates(self, x, y): + return x < 0 or x >= self.size or y < 0 or y >= self.size def calculate_neighbours(self): neighbours = ( (-1,-1), (0,-1), (1,-1), @@ -34,17 +37,12 @@ class Board(): continue neighbouring_bombs = 0 for dx,dy in neighbours: - if x+dx < 0 or x+dx >= self.size: - continue - if y+dy < 0 or y+dy >= self.size: + if self.invalid_coordinates(x+dx, y+dy): continue if self.tiles[x+dx][y+dy] == 9: neighbouring_bombs += 1 self.tiles[x][y] = neighbouring_bombs - def reset_guesses(self): - pass - def get_view(self): view = deepcopy(self.masked) for y in range(self.size): @@ -53,16 +51,22 @@ class Board(): view[x][y]=self.tiles[x][y] return view + def is_winning(self): + for y in range(self.size): + for x in range(self.size): + if self.tiles[x][y] != 9 and self.masked[x][y]: + return False + return True + + def collect_area(self, x, y, area=set()): to_test = [] for dx, dy in ( (0,-1), (-1,0), (1,0), (0,1) ): - if x+dx < 0 or x+dx >= self.size: - continue - if y+dy < 0 or y+dy >= self.size: + if self.invalid_coordinates(x+dx, y+dy): continue if self.tiles[x+dx][y+dy] == 0 and (x+dx,y+dy) not in area: - area.add((x+dx, y+dy)) to_test.append((x+dx, y+dy)) + area.add((x+dx, y+dy)) for tx, ty in to_test: area.add((x,y)) area=area.union(self.collect_area(tx, ty, area)) @@ -70,7 +74,7 @@ class Board(): def make_guess(self, x, y): - if x not in range(self.size) or y not in range(self.size): + if self.invalid_coordinates(x, y): print("Koordinaatit on pelilaudan ulkopuolella", file=stderr) return False @@ -85,11 +89,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 cx+dx < 0 or cx+dx >= self.size: - continue - if cy+dy < 0 or cy+dy >= self.size: - continue - self.masked[cx+dx][cy+dy] = 0 + if not self.invalid_coordinates(cx+dx, cy+dy): + self.masked[cx+dx][cy+dy] = 0 else: self.masked[x][y] = 0 @@ -108,5 +109,4 @@ if __name__ == "__main__": b.make_guess(5,5) print_matrix(b.get_view()) -
\ No newline at end of file |