From fa6a80ea7107c3a1e7c1fa600817b0c5f7be5d36 Mon Sep 17 00:00:00 2001 From: Aineopintojen-harjoitustyo-Algoritmit-j Date: Tue, 30 Jan 2024 07:54:36 +0200 Subject: Refactoring options processing. --- app.py | 33 +++++++++++---------------------- tui/tui.py | 35 +++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/app.py b/app.py index f10a414..4af66dd 100644 --- a/app.py +++ b/app.py @@ -9,31 +9,20 @@ class App: """ App - Luokka pääohjelmalle""" def __init__(self, args=None): level = Level.BEGINNER - auto, uncertain, quiet = False, False, False + tui_opts = {'bot': DSSPBot} if args: - level = Level.INTERMEDIATE if args.intermediate else level - level = Level.EXPERT if args.expert else level - width, height, bombs = LevelSpecs[level] - auto = args.auto - auto, uncertain = (True, True) if args.uncertain else (auto, False) - auto, uncertain, quiet = (True, True, True) \ - if args.quiet else (auto, uncertain, False) - self.bot = SimpleBot(uncertain=uncertain) if args.simple \ - else DSSPBot(uncertain=uncertain) - self.ui = Tui ( - bot=self.bot, - autoplay=auto, - interact=not uncertain, - suppress=quiet, - width=width, - height=height, - bombs=bombs - ) + # pylint: disable = pointless-statement + (level:=Level.INTERMEDIATE) if args.intermediate else () + (level:=Level.EXPERT) if args.expert else () + + tui_opts['bot'] = SimpleBot if args.simple else DSSPBot + tui_opts['autoplay'] = args.auto + tui_opts['interactive'] = not args.uncertain + tui_opts['suppress'] = args.quiet + tui_opts['height'] = LevelSpecs[level][1] self.board = Board(level=level) - self.bot = DSSPBot(uncertain=uncertain) if self.bot is None \ - else self.bot - self.ui = Tui(bot=self.bot) if self.ui is None else self.ui + self.ui = Tui(**tui_opts) self.game = Game(self.board,self.ui) def run(self): diff --git a/tui/tui.py b/tui/tui.py index 7d8f565..f91db05 100644 --- a/tui/tui.py +++ b/tui/tui.py @@ -7,27 +7,34 @@ from .ansi_draw import AnsiDraw, SuppressDraw class Tui(): """ Tui - Luokka käyttäjän interaktiota varten """ - # pylint: disable = unused-argument - def __init__(self, **opts): - self.bot = opts['bot'] if 'bot' in opts else None - self.autoplay = opts['autoplay'] if 'autoplay' in opts else False - self.interact = opts['interact'] if 'interact' in opts else True - self.suppress = opts['suppress'] if 'suppress' in opts else False - self.height = opts['height'] if 'height' in opts else 15 + # pylint: disable = too-many-arguments + def __init__(self, + bot = None, + autoplay = False, + interactive = True, + suppress = False, + height = 9): + + self.autoplay = autoplay + self.interactive = interactive + self.suppress = suppress + self.height = height # jos ei oo bottia pitää olla interaktiivinen - if self.bot is None: + if bot is None: self.autoplay = False - self.interact = True + self.interactive = True self.suppress = False # jos ei mitään näytetä ei voi olla interaktiivinen - self.interact = False if self.suppress else self.interact + self.interactive = False if self.suppress else self.interactive # automaattipeli pitää olla päällä jos ei interaktiivinen - self.autoplay = self.autoplay if self.interact else True + self.autoplay = self.autoplay if self.interactive else True + + self.bot = bot(uncertain=not self.interactive) if bot else None - if self.interact: + if self.interactive: self.kbd = Kbd() else: self.kbd = NoKbd() @@ -49,7 +56,7 @@ class Tui(): # ilman näppiskäsittelijää voidaan lopettaa - if not self.interact: + if not self.interactive: return Action.QUIT, 0, 0 w, h = len(matrix), len(matrix[0]) @@ -84,6 +91,6 @@ class Tui(): def game_end(self, matrix): """ tehtävät ihan pelin lopuksi """ - if self.interact: + if self.interactive: self.draw.matrix(matrix, -1, -1) self.draw.status_line("Kiitos! ") -- cgit v1.2.3