コード例 #1
0
ファイル: game.py プロジェクト: laegsgaardTroels/chess
 def moves_str(self, color):
     moves_strs = []
     for piece, move in self.moves(color, with_piece=True):
         move_str = Board.chess_move_notation(move)
         moves_strs.append(
             f"{str(piece)} : {move_str}"
         )
     return '\n'.join(moves_strs)
コード例 #2
0
ファイル: game.py プロジェクト: laegsgaardTroels/chess
    def play(self, verbose=False):
        """The game loop.
        """
        if verbose:
            print(r"""
            (  ____ \|\     /|(  ____ \(  ____ \(  ____ \
            | (    \/| )   ( || (    \/| (    \/| (    \/
            | |      | (___) || (__    | (_____ | (
            | |      |  ___  ||  __)   (_____  )(_____  )
            | |      | (   ) || (            ) |      ) |
            | (____/\| )   ( || (____/\/\____) |/\____) |
            (_______/|/     \|(_______/\_______)\_______)

            """)
            print(f"White player: {self.white_player}")
            print(f"Black player: {self.black_player}")
            print()
            print()
        try:
            while not (
                self.is_checkmate(self.current_color)
                or self.is_draw()
            ):
                if verbose:
                    print(self)
                    print()

                move = self.get_move()
                if move is None:
                    if verbose:
                        print()
                        print("Invalid move...\n")
                    continue
                if verbose:
                    print()
                    print('moving', Board.chess_move_notation(move))
                    print()
                from_, to = move
                new_game = self.simulate_move(from_, to)
                if new_game.is_check(self.current_color):
                    if verbose:
                        print()
                        print("Cannot move into check...\n")
                    continue
                self._game_history.append(str(self))
                self.move(from_, to)

                # TODO: Below looks like shit
                check = self.is_check(self.current_color)
                if check:
                    if verbose:
                        check_str = '\n'.join(map(str, check))
                        print()
                        print(f'{self.current_color} is check by {check_str}')
                        print()

            if verbose:
                if self.is_draw():
                    self.winner = None
                    print()
                    print()
                    print("It is a draw!")
                    logger.info("it is a draw.")
                else:
                    self.winner = self.opponent_color()
                    print()
                    print()
                    print(f"Winner is {self.winner}")
                    print(f"White player: {self.white_player}")
                    print(f"Black player: {self.black_player}")
                    logger.info(f"winner is {self.winner}.")

            if verbose:
                print()
                print()
                print("White Moves:")
                print(self.moves_str('white'))
                print("Black Moves:")
                print(self.moves_str('black'))
                print()

        except KeyboardInterrupt:
            if verbose:
                print()
                print()
                print("Game stopped.")
            logger.info("game stopped due to keyboard interrupt.")