diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-03 12:09:45 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-03 12:09:45 +0200 |
commit | 5e257c3cd894e7fbe350f4e58b8c75824e755a42 (patch) | |
tree | d4b91de28d33453244b8f15e6006089ca5ecbe1c /board/board.py | |
parent | 63f27f918c8e0e6a0ae24e0b9315c2f2f7b49b6f (diff) |
Implementing use of readymade board at board.py.
Diffstat (limited to 'board/board.py')
-rw-r--r-- | board/board.py | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/board/board.py b/board/board.py index 1d31d13..c1ec798 100644 --- a/board/board.py +++ b/board/board.py @@ -14,7 +14,9 @@ class Board(): level = Level.BEGINNER, width = None, height = None, - mines = None): + mines = None, + board = None): + # pylint: disable = too-many-arguments self.__level = level self.__width, self.__height, self.__mines =LevelSpecs[self.__level][:3] @@ -29,9 +31,17 @@ class Board(): if self.__mines not in range(1,self.__width*self.__height): self.__mines = self.__width - if ( (self.__width, self.__height, self.__mines) - == LevelSpecs[self.__level][:3] ): - self.__level_name = LevelSpecs[self.__level][3] + + if board and self.__validate_board(board): + self.__width, self.__height = self.__get_board_dimensions(board) + self.__mines = self.__get_board_mines(board) + else: + board = None + + for _, specs in LevelSpecs.items(): + if (self.__width, self.__height, self.__mines) == specs[:3]: + self.__level_name = specs[3] + break else: self.__level_name = "Mukautettu" @@ -40,10 +50,40 @@ class Board(): self.__tiles = None self.__masked = None + self.__initialize_tiles() - self.__randomize_mines() + if board: + self.__populate_with_board(board) + else: + self.__randomize_mines() self.__calculate_neighbours() + def __validate_board(self, board): + w = len(board[0]) + h = len(board) + if w not in range(2,51) or h not in range(2,51): + return False + for line in board: + if len(line)!=w: + return False + if self.__get_board_mines(board) not in range (1, w*h - 1): + return False + return True + + + def __get_board_dimensions(self, board): + return len(board[0]), len(board) + + + def __get_board_mines(self, board): + return sum((sum(x) for x in board)) + + + def __populate_with_board(self, board): + for y in range(self.__height): + for x in range(self.__width): + if board[y][x]: + self.__tiles[x][y] = Tile.MINE def __initialize_tiles(self): """ alustaa pelilaudan matriisit """ |