diff options
| author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-17 09:41:48 +0200 | 
|---|---|---|
| committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-02-17 09:41:48 +0200 | 
| commit | e785dbd4f726c5716f21071ed25dc35ac87c0c74 (patch) | |
| tree | 781373b78380a1ffd1ea8c5dc8ceb2bd313631e9 /src/miinaharava/tui/tui.py | |
| parent | 4eff4a32cfa594cc2a3df3885de92d407edc6675 (diff) | |
Dev tools and directory structure rework.
Diffstat (limited to 'src/miinaharava/tui/tui.py')
| -rw-r--r-- | src/miinaharava/tui/tui.py | 109 | 
1 files changed, 109 insertions, 0 deletions
diff --git a/src/miinaharava/tui/tui.py b/src/miinaharava/tui/tui.py new file mode 100644 index 0000000..d7f7fb3 --- /dev/null +++ b/src/miinaharava/tui/tui.py @@ -0,0 +1,109 @@ +""" tui/tui.py - runko käyttöliittymälle """ +import time +from .static import Action +from .kbd import Kbd, NoKbd +from .ansi_draw import AnsiDraw, SuppressDraw + + +class Tui(): +    """ Tui - Luokka käyttäjän interaktiota varten """ +    # pylint: disable = too-many-arguments, too-many-instance-attributes +    def __init__(self, +                bot = None, +                autoplay = False, +                interactive = True, +                suppress = False, +                height = 9, +                level_name = "outo lauta", +                delay = 0): + +        # jos ei ole bottia pitää olla interaktiivinen +        if bot is None: +            autoplay = False +            interactive = True +            suppress = False + +        # jos ei mitään näytetä ei voi olla interaktiivinen +        if suppress: +            interactive = False + +        # automaattipeli pitää olla päällä jos ei interaktiivinen +        if not interactive: +            autoplay = True + +        if delay and delay not in range(0,500): +            delay = 50 + +        self.autoplay = autoplay +        self.interactive = interactive +        self.suppress = suppress +        self.height = height +        self.level_name = level_name +        self.delay = delay + +        self.bot = bot(uncertain=not self.interactive) if bot else None + +        self.kbd = Kbd() if self.interactive else NoKbd() + +        if self.suppress: +            self.draw = SuppressDraw() +        else: +            self.draw = AnsiDraw(height=self.height, name=self.level_name) + +    def matrix_selector(self, matrix, x, y): +        """ valinta matriisita """ + +        # automaattipeli avaa botin vinkit heti +        if self.autoplay: +            action, x, y = self.bot.hint(matrix, x, y) +            if action != Action.NOOP: +                if self.delay: +                    self.draw.matrix(matrix, x, y) +                    time.sleep(self.delay/100) +                return Action.OPEN if action==Action.SAFE else action, x, y + + +        # ilman näppiskäsittelijää voidaan lopettaa +        if not self.interactive: +            return Action.QUIT, 0, 0 + +        w, h = len(matrix), len(matrix[0]) +        while True: +            self.draw.matrix(matrix, x, y) +            action, x, y = self.kbd.read_matrix_action(w, h, x, y) +            match action: +                case Action.QUIT: +                    return (action, x, y) +                case Action.OPEN | Action.FLAG | Action.MINE | Action.SAFE: +                    if matrix[x][y] >= 10: +                        return (action, x, y) +                case Action.HINT: +                    if self.bot is not None: +                        return self.bot.hint(matrix, x, y) + +    def game_over(self, matrix, x, y): +        """ tehtävät kun kuolee """ +        self.draw.matrix(matrix, x, y) +        self.draw.status_line( +            f"{self.level_name}: " + +            ("K  " if self.suppress else f"{'Kuolit!':<30}") +        ) +        self.kbd.read_action() + +    def game_win(self, matrix, x, y): +        """ tehtävät kun voittaa """ +        self.draw.matrix(matrix, x, y) +        self.draw.status_line( +            f"{self.level_name}: " + +            ("V  " if self.suppress else f"{'Voitit!':<30}") +        ) +        self.kbd.read_action() + +    def game_end(self, matrix): +        """ tehtävät ihan pelin lopuksi """ +        if self.interactive: +            self.draw.matrix(matrix, -1, -1) +            self.draw.status_line( +                f"{self.level_name}: " + +                f"{'Kiitos!':<30}" +            )  |