diff options
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | __main__.py | 7 | ||||
-rw-r--r-- | app.py | 11 | ||||
-rw-r--r-- | board/board.py | 50 | ||||
-rw-r--r-- | tests/data/beginner_3win.txt | 31 |
5 files changed, 93 insertions, 12 deletions
@@ -22,5 +22,11 @@ Miinaharava ratkaisijalla ### Käyttöohjeet `python3 miinaharava -h` +### Pelaa automaattisesti keskivaikea lauta +`python3 miinaharava -i -aa` + ### Automaattipelaa 10 peliä `python3 miinaharava -c 10` + +### Pelaa kentät tiedostosta +`python3 miinaharava -f miinaharava/tests/data/beginner_3win.txt` diff --git a/__main__.py b/__main__.py index c2497ca..6dad3f8 100644 --- a/__main__.py +++ b/__main__.py @@ -4,6 +4,8 @@ from app import App from cmdline import args +vars(args)['board'] = None + if args.count is None and args.file is None: app = App(args) is_win = app.run() @@ -11,9 +13,9 @@ if args.count is None and args.file is None: sys.exit(not is_win) # Exit koodeissa 0 on onnistunut suoritus win_count = 0 -args.autoplay = 2 if args.file is None: + args.autoplay = 2 run_count = args.count for i in range(run_count): print(end=f" \rSuoritus {i+1:>6}/{run_count} ") @@ -32,8 +34,7 @@ else: line = bfile.readline() if not line or (line[0]!='.' and line[0]!='@'): if board: - args_dict = vars(args) - args_dict['board'] = board + args.board = board app = App(args) win_count += app.run() run_count += 1 @@ -14,10 +14,13 @@ class App: # pylint: disable = multiple-statements if args.intermediate: board_opts['level'] = Level.INTERMEDIATE if args.expert: board_opts['level'] = Level.EXPERT - if args.mines: board_opts['mines'] = args.mines - if args.size: - board_opts['width'] = args.size[0] - board_opts['height'] = args.size[1] + if args.board: + board_opts['board'] = args.board + else: + if args.mines: board_opts['mines'] = args.mines + if args.size: + board_opts['width'] = args.size[0] + board_opts['height'] = args.size[1] if args.bot==0: tui_opts['bot'] = None if args.bot==1: tui_opts['bot'] = SimpleBot 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 """ diff --git a/tests/data/beginner_3win.txt b/tests/data/beginner_3win.txt new file mode 100644 index 0000000..6eeb1a4 --- /dev/null +++ b/tests/data/beginner_3win.txt @@ -0,0 +1,31 @@ +# Kolme aloittelijan lautaa joissa varma voitto + +......... +......... +......... +...@@@... +...@@@... +...@@@... +....@.... +......... +......... + +......... +......... +......... +...@@@... +...@@@... +...@@@... +....@.... +......... +......... + +......... +......... +......... +...@@@... +...@@@... +...@@@... +....@.... +......... +......... |