Exemple #1
0
 def __str__(self):
     if self.status == 'x':
         return colored('X', 'red')
     elif self.status == 'o':
         return colored('O', 'green')
     elif self.status == 'p' and self.visible:
         return colored('?', 'cyan', attrs=['bold', 'blink'])
     elif self.status == 'h' and self.visible:
         return colored('.', 'cyan', attrs=['bold', 'blink'])
     return '.'
Exemple #2
0
    def add_command(self, name: str, description: str, disabled=False) -> None:
        """
        Ajoute une nouvelle commande avec son déclancheur et sa description

        :param str name: Trigger for the command
        :param str description: What the command do
        :param bool disabled: Is this command enabled ?
        """
        if disabled:
            description += colored(' (disabled)', 'red')
        self.commands[name] = description
Exemple #3
0
    def congratulation(self) -> None:
        """
        Affiche le message de victoire

        :return: None
        """
        if self.menu.pawns[0] > self.menu.pawns[1]:
            winner = colored('X', 'red')
            captured = self.menu.pawns[0]
        elif self.menu.pawns[1] > self.menu.pawns[0]:
            winner = colored('O', 'green')
            captured = self.menu.pawns[1]
        else:
            winner = False
            captured = self.menu.pawns[0]

        print("\n\n")
        if winner:
            print(f"Player {winner} won with " f"{captured} pawns !")
        else:
            print(f"Equality, {captured} pawns for 2 players")
Exemple #4
0
    def __str__(self):
        player = str(Pawn(self.player))
        menu = colored(f"Turns {self.turns}\n", "yellow")
        menu += f"It is the turn of {player}\n"
        menu += f"{player} can play\n"
        menu += "\n"
        menu += f"Pawns : {str(Pawn('x'))} {colored(self.pawns[0], 'red')}" \
                f" {int((self.pawns[0]*100)/(self.size**2))}%\n"
        menu += f"        {str(Pawn('o'))} {colored(self.pawns[1], 'green')}" \
                f" {int((self.pawns[1]*100)/(self.size**2))}%\n"
        menu += "\n"
        menu += "Commands :\n"
        for name, description in self.commands.items():
            regex = r"(\*[A-Za-z]\*)"
            match = re.findall(regex, description, re.MULTILINE)[0]
            description = description.replace(match, colored(match, 'yellow'))
            description = description.replace('*', '')
            menu += f"   {colored(name, 'yellow')}: {description}\n"
        menu += "\n"
        menu += "Hints : " + (colored("On", 'green')
                              if self.hints else colored("Off", 'red'))

        return menu
Exemple #5
0
    def process_action(self, action: str) -> None:
        """
        Execute l'action demandée

        :param str action: Action to do
        :return: None
        """
        if action == 'A':
            self.stop()
        elif action == 'P':
            already_parsed = False
            if self.menu.player == 'o' or (self.menu.player == 'x'
                                           and self.players == 2):
                del_top_line()  # efface la ligne supérieur
                print("Position: (ex: D3) " + colored(
                    "or type 'back' to return to the action menu",
                    'cyan',
                ))

                move = input('')
                while not self.board.is_valid_move(move) \
                        and move != 'back':
                    del_top_line(2)
                    print(
                        "Invalid position! please enter a new position: (ex: D3)"
                        +
                        colored("or type 'back' to return to the action menu",
                                'cyan'))
                    move = input('')

                if move == 'back':
                    del_top_line(2)
                    return self.get_action()
            else:
                can_play = self.board.set_valid_poses('x')
                if not can_play:
                    self.menu.player = 'o'
                    self.menu.turns += 1
                    self.board.set_valid_poses(self.menu.player)
                else:
                    move = random.choice(self.board.get_valid_poses())
                    already_parsed = True

            backup = {
                'board': self.board.export(),
                'is_playing': self.is_playing,
                'menu': self.menu.export(),
                'time': self.time
            }
            save(f"games/{self.time}", backup)

            self.menu.pawns = self.board.move(move, self.menu.player,
                                              already_parsed)
            self.menu.player = 'x' if self.menu.player == 'o' else 'o'
            self.menu.turns += 1
            self.render()

            save(
                f"games/{self.time}", {
                    'board': self.board.export(),
                    'is_playing': self.is_playing,
                    'menu': self.menu.export(),
                    'time': self.time
                })
        elif action == 'H':
            self.board.toggle_hints()
            self.menu.hints = self.board.hints
            self.render()

        elif action == 'U':
            to_undo = 1 if self.players == 2 else 2
            undo = load(f"games/{self.time}", self.menu.turns - to_undo)
            if undo:
                self.menu.load(undo.get('menu'))
                self.board.load(undo.get('board'))
                self.render()
            else:
                del_top_line(2)
                print(colored("No backup !", 'red'))

        elif action == 'R':
            to_redo = 1 if self.players == 2 else 2
            redo = load(f"games/{self.time}", self.menu.turns + to_redo)
            if redo:
                self.menu.load(redo.get('menu'))
                self.board.load(redo.get('board'))
                self.render()
            else:
                del_top_line(2)
                print(colored("No backup !", 'red'))