Exemple #1
0
 def setUp(self) -> None:
     self.player_turn = 1
     self.chess_game = ChessGame()
     self.chess_board = self.chess_game.board
     self.chess_game.player_turn = self.player_turn
     self.start_space = Vec2(5, 5)
     self.king = King(team=self.player_turn)
     self.knight = Knight(team=self.player_turn)
     self.pawn = Pawn(team=self.player_turn)
     self.enemy_pawn = Pawn(team=2)
     self.p1_pawn = Pawn(team=1)
Exemple #2
0
    async def accept(self, ctx, *args):
        #should be log
        print("accept read from", ctx.author)
        now = datetime.now()  #used to clear out old self.challenges

        #go through the challenges
        for idx in range(len(self.challenges)):
            challenge = self.challenges[idx]

            if (challenge[3] - now) > timedelta(minutes=15):
                print("challenge timed out")
                self.challenges.pop(idk)
                continue

            #first see if the challenge is the same channel as the accept
            if challenge[2] == ctx.channel:

                #if there is a game already in the channel, do not start another
                for game in self.ongoing_games:
                    if game.channel == ctx.channel:
                        return

                #see if the accepter is one of the people challenged
                for mention in challenge[0]:
                    if mention.id == ctx.author.id:
                        #see if accepter @'d someone, if not just accept first challenge
                        if (len(ctx.message.mentions)) == 0:
                            print("game start between ", ctx.author,
                                  challenge[1], "in channel", ctx.channel)
                            self.ongoing_games.append(
                                ChessGame(challenge[1], ctx.author,
                                          ctx.channel, challenge[4]))
                            await self.ongoing_games[-1].start_game()
                            self.challenges.pop(idx)
                            return
                        else:
                            #find the challenger who the accepter @'d
                            for mention2 in ctx.message.mentions:
                                if mention2.id == challenge[1]:
                                    print("@ game start between ", ctx.author,
                                          challenge[1], "in channel",
                                          ctx.channel)
                                    self.ongoing_games.append(
                                        ChessGame(challenge[1], ctx.author,
                                                  ctx.channel, challenge[4]))
                                    await self.ongoing_games[-1].start_game()
                                    self.challenges.pop(idx)
                                    return
Exemple #3
0
    async def challenge(self, ctx, *args):
        #should be log
        print("challenge read from", ctx.author)
        #must challenge another user
        if len(args) == 0:
            return

        #cannot challenge everyone
        if not ctx.message.mention_everyone:
            #if the bot is being challenged start the game
            if ctx.bot.user in ctx.message.mentions:
                #check if game already in channel
                for g in self.ongoing_games:
                    if g.channel == ctx.channel:
                        return
                self.ongoing_games.append(
                    ChessGame(ctx.author, 0, ctx.channel, args[-1]))
                await self.ongoing_games[-1].start_game()
            else:
                now = datetime.now()
                try:
                    #Cannot challenge self, try to remove
                    self.challenges.append(
                        (ctx.message.mentions.remove(ctx.author), ctx.author,
                         ctx.channel, now, args[-1]))
                except:
                    self.challenges.append((ctx.message.mentions, ctx.author,
                                            ctx.channel, now, args[-1]))
Exemple #4
0
def main():
    print("Welcome to our chess AI! Please select your difficulty:")
    print("Easy: 1")
    print("Medium: 2")
    print("Hard: 3")
    diff = input("")

    depth = 0
    if diff == "1":
        depth = 1
    elif diff == "2":
        depth = 2
    elif diff == "3":
        depth = 3

    demo_human_agent = HumanAgent(True)
    demo_opponent_agent = OrderedAgentTrans(False,
                                            combined,
                                            depth,
                                            load_hh=True)

    demo_game = ChessGame(demo_human_agent, demo_opponent_agent)
    demo_game.play_game(True)
Exemple #5
0
 def precmd(self, line):
     # Maybe change later for better way to take things out of queue
     # Think about using try except
     while not self.data_streamer.challenges.empty():
         challenge = self.data_streamer.challenges.get()
         self.challenges[challenge['id']] = challenge
     while not self.data_streamer.games.empty():
         new_game, date = self.data_streamer.games.get()
         self.games[new_game['id']] = ChessGame(self.username, new_game,
                                                date)
     while not self.data_streamer.updates.empty():
         # update is tuple of (game_id, message)
         game_id, message, date = self.data_streamer.updates.get()
         if message['type'] == 'gameState':
             self.games[game_id].update_game(message, date)
     return line
Exemple #6
0
 def transform(self, algebraic_moves):
     game = ChessGame()
     return ''.join(
         game.make_move_from_algebraic_and_return_uci(move)
         for move in algebraic_moves)
Exemple #7
0
def main():

    game = ChessGame(setup_pieces=True)
    game.run()
Exemple #8
0
def main():
    my_game = ChessGame(GreedyAgent, RandAgent)
    my_game.play_game()
Exemple #9
0
def main():
    selected_game_mode = -1
    while (selected_game_mode < 0 or selected_game_mode > 3):
        print('Select game mode:')
        print('1. Normal chess')
        print('2. Reverse chess (suicide mode)')
        selected_game_mode = int(input('Selection: '))
        print('')

    selected_scenario = -1
    while (selected_scenario < 0 or selected_scenario > 7):
        print('Select game scenario:')
        print('1. Train AI')
        print('2. Watch trained AI vs trained AI')
        print('3. Watch trained AI vs normal AI')
        print('4. Watch normal AI vs normal AI')
        print('5. Play against trained AI')
        print('6. Play against normal AI')
        selected_scenario = int(input('Selection: '))

    # Select playing color if human vs ai
    selected_color = -1
    if (selected_scenario > 4 and selected_scenario <= 6):
        while (selected_color < 0 or selected_color > 3):
            print('Select Color:')
            print('1. White')
            print('2. Black')
            selected_color = int(input('Selection: '))

    game_variant = None
    if (selected_game_mode == 1):
        game_variant = 'chess'
    elif (selected_game_mode == 2):
        game_variant = 'suicide'
    else:
        raise Exception('Invalid game mode!')

    # Load train data if a scenario including 'trained AI' is selected
    # train_data = np.load('train_file.npy')

    # [1. Train AI]
    if (selected_scenario == 1):
        # todoooooooooooooooooooooooooo------------------------------------------
        pass
    else:
        white_player = None
        black_player = None
        # [2. Watch trained AI vs trained AI]
        if (selected_scenario == 2):
            white_player = AIPlayer(is_white=True,
                                    depth=3,
                                    piece_square_table=None)
            black_player = AIPlayer(is_white=False,
                                    depth=3,
                                    piece_square_table=None)
        # [3. Watch trained AI vs normal AI]
        elif (selected_scenario == 3):
            white_player = AIPlayer(is_white=True,
                                    depth=3,
                                    piece_square_table=None)
            black_player = AIPlayer(is_white=False,
                                    depth=3,
                                    piece_square_table=None)
        # [4. Watch normal AI vs normal AI]
        elif (selected_scenario == 4):
            white_player = AIPlayer(is_white=True,
                                    depth=3,
                                    piece_square_table=None)
            black_player = AIPlayer(is_white=False,
                                    depth=3,
                                    piece_square_table=None)
        # [5. Play against trained AI]
        elif (selected_scenario == 5):
            # [1. White]
            if (selected_color == 1):
                white_player = HumanPlayer(is_white=True)
                black_player = AIPlayer(is_white=False,
                                        depth=3,
                                        piece_square_table=None)
            # [2. Black]
            elif (selected_color == 2):
                white_player = AIPlayer(is_white=True,
                                        depth=3,
                                        piece_square_table=None)
                black_player = HumanPlayer(is_white=False)
            else:
                raise Exception('Invalid color!')
        # [6. Play against normal AI]
        elif (selected_scenario == 6):
            # [1. White]
            if (selected_color == 1):
                white_player = HumanPlayer(is_white=True)
                black_player = AIPlayer(is_white=False,
                                        depth=3,
                                        piece_square_table=None)
            # [2. Black]
            elif (selected_color == 2):
                white_player = AIPlayer(is_white=True,
                                        depth=3,
                                        piece_square_table=None)
                black_player = HumanPlayer(is_white=False)
            else:
                raise Exception('Invalid color!')
        else:
            raise Exception('Invalid scenario!')

        chess_game = ChessGame(white_player=white_player,
                               black_player=black_player,
                               variant=game_variant,
                               max_turn=None)

        gameover = False
        result = None
        while (not gameover):
            gameover, result = chess_game.play_half_turn()
        print(gameover)
        print(result)
Exemple #10
0
class ChessGameTests(unittest.TestCase):
    def setUp(self) -> None:
        self.player_turn = 1
        self.chess_game = ChessGame()
        self.chess_board = self.chess_game.board
        self.chess_game.player_turn = self.player_turn
        self.start_space = Vec2(5, 5)
        self.king = King(team=self.player_turn)
        self.knight = Knight(team=self.player_turn)
        self.pawn = Pawn(team=self.player_turn)
        self.enemy_pawn = Pawn(team=2)
        self.p1_pawn = Pawn(team=1)

    def test_creating_a_chess_game_works_fuck(self):
        self.assertIsInstance(self.chess_game, ChessGame)

    def test_pawn_can_move_one_space_forward(self):
        # Arrange
        end_space = self.start_space + Vec2(0, -1)
        move = Move(self.start_space, end_space)

        self.chess_game.board.set_piece(self.start_space, self.pawn)

        # Act
        self.chess_game.try_player_move(move, self.player_turn)

        # Assert
        self.assertEqual(self.chess_game.board.get_piece(end_space), self.pawn)

    def test_pawn_cannot_move_backwards(self):
        # Arrange
        end_space = self.start_space + Vec2(0, 1)
        move = Move(self.start_space, end_space)

        self.chess_game.board.set_piece(self.start_space, self.pawn)

        # Act & Assert
        self.assertRaises(IllegalMove, self.chess_game.try_player_move, move, self.player_turn)

    def test_king_is_on_board(self):
        # Arrange
        self.chess_game.board.set_piece(self.start_space, self.king)

        # Act & Assert
        self.assertTrue(self.chess_game.board.get_king(self.player_turn))

    def test_knight_border_portal(self):
        # Arrange
        knight = Knight(team=1)

        knight_starting_space = Vec2(1, 0)
        knight_destination_space = Vec2(0, 2)

        knight_move = Move(knight_starting_space, knight_destination_space)

        self.chess_game.board.set_piece(knight_starting_space, knight)

        # Act & Assert
        self.assertTrue(knight.can_move(knight_move, self.chess_board))

    def test_get_king(self):
        # Arrange
        king_start_pos = Vec2(4, 0)
        self.chess_game.board.set_piece(king_start_pos, self.king)

        # Act
        the_king = self.chess_board.get_king(self.player_turn)

        # Assert
        self.assertEqual(self.king, the_king)

    def test_get_king_returns_none_if_no_king(self):
        # Arrange
        # No arrangement

        # Act
        the_king = self.chess_board.get_king(self.player_turn)

        # Assert
        self.assertIsNone(the_king)

    def test_can_king_free_himself_if_checked(self):
        # Arrange
        king_start_pos = Vec2(4,7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(Vec2(5, 6), self.enemy_pawn)

        # Act
        king_move = self.king.can_move(Move(king_start_pos, Vec2(3, 7)), self.chess_board)

        # Assert
        self.assertTrue(king_move)

    def test_king_cannot_endanger_himself(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(Vec2(5, 5), self.enemy_pawn)

        # Act
        king_move = self.king.can_move(Move(king_start_pos, Vec2(4, 2)), self.chess_board)

        # Assert
        self.assertFalse(king_move)

    def test_pawn_may_double_jump(self):
        # Arrange
        pawn_start_pos = Vec2(5, 5)

        # Act
        pawn_move = self.pawn.can_move(Move(pawn_start_pos, Vec2(5, 3)), self.chess_board)

        # Assert
        self.assertTrue(pawn_move)

    def test_pawn_may_not_double_jump_after_moving(self):
        # Arrange
        pawn_start_pos = Vec2(0, 6)

        # Act
        self.chess_board.move(Move(pawn_start_pos, Vec2(0, 3)))
        pawn_move_to_test = self.p1_pawn.can_move(Move(pawn_start_pos, Vec2(5, 3)), self.chess_board)

        # Assert
        self.assertFalse(pawn_move_to_test)

    def test_horse_may_jump(self):
        # Arrange
        knight_start_pos = Vec2(5, 5)
        self.chess_game.board.set_piece(knight_start_pos, self.knight)
        self.chess_game.board.set_piece(Vec2(5, 4), self.pawn)
        self.chess_game.board.set_piece(Vec2(4, 4), self.king)

        # Act
        knight_jump = self.knight.can_move(Move(knight_start_pos, Vec2(3, 4)), self.chess_board)

        # Assert
        self.assertTrue(knight_jump)

    def test_king_may_castle_left(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        rook_start_pos = Vec2(0, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(rook_start_pos, Rook(team=self.player_turn))

        # Act
        castle_move = self.king.can_move(Move(king_start_pos, Vec2(2, 7)), self.chess_board)

        # Assert
        self.assertTrue(castle_move)

    def test_king_may_castle_right(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        rook_start_pos = Vec2(7, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(rook_start_pos, Rook(team=self.player_turn))

        # Act
        castle_move = self.king.can_move(Move(king_start_pos, Vec2(6, 7)), self.chess_board)

        # Assert
        self.assertTrue(castle_move)

    def test_king_cannot_over_move_For_castle_left(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        rook_start_pos = Vec2(0, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(rook_start_pos, Rook(team=self.player_turn))

        # Act
        castle_move = self.king.can_move(Move(king_start_pos, Vec2(1, 7)), self.chess_board)

        # Assert
        self.assertFalse(castle_move)

    def test_king_cannot_over_move_For_castle_right(self):
        # Arrange
        king_start_pos = Vec2(4, 7)
        rook_start_pos = Vec2(7, 7)
        self.chess_game.board.set_piece(king_start_pos, self.king)
        self.chess_game.board.set_piece(rook_start_pos, Rook(team=self.player_turn))

        # Act
        castle_move = self.king.can_move(Move(king_start_pos, Vec2(7, 7)), self.chess_board)

        # Assert
        self.assertFalse(castle_move)

    def test_piece_may_not_move_out_of_bounds(self):
        # Arrange
        knight_end_pos = Vec2(-2, -1)

        # Act
        is_knight_moving_inbound = self.chess_board.in_board(knight_end_pos)

        # Assert
        self.assertFalse(is_knight_moving_inbound)

    def test_new_coordinates_via_pawn_double_jump(self):
        # Arrange
        self.chess_board.set_piece(Vec2(0, 2), self.enemy_pawn)

        # Act
        self.chess_board.move(Move(Vec2(0, 2), Vec2(0, 4)))

        # Assert
        self.assertTrue(self.chess_board.get_piece(Vec2(0, 4)).name == "Pawn")
 def transform(self, algebraic_moves):
     game = ChessGame()
     return "".join(game.make_move_from_algebraic_and_return_uci(move) for move in algebraic_moves)
Exemple #12
0
from chess_game import ChessGame

_DEBUG = False

if __name__ == '__main__':
    my_game = ChessGame()
    my_game.run()
Exemple #13
0
 def process_result_value(self, uci_moves_string, dialect):
     if isinstance(uci_moves_string, basestring):
         playable_game = ChessGame()
         return playable_game.make_moves_from_long_uci_string(
             uci_moves_string)
     return uci_moves_string
Exemple #14
0
 def moves_list(self):
     return ChessGame().make_moves_from_long_uci_string(self.moves)
Exemple #15
0
 def process_result_value(self, uci_moves_string, dialect):
     if isinstance(uci_moves_string, basestring):
         playable_game = ChessGame()
         return playable_game.make_moves_from_long_uci_string(uci_moves_string)
     return uci_moves_string