示例#1
0
 def setUp(self):
     players = [
         AIPlayer(TicTacToeBrain(GameLevel.EASY), GameToken("X")),
         AIPlayer(TicTacToeBrain(GameLevel.EASY), GameToken("Y"))
     ]
     self.client = TicTacToeClientGame(players, "127.0.0.1", 8081)
     self.client.server_socket = SampleServer()
    def test_hard_mode(self):
        test_board = [
            [self.x_token, None, None],
            [self.o_token, None, None],
            [None, None, None],
        ]
        board = TicTacToeBoard()
        board.current_state = test_board

        under_test = TicTacToeBrain(GameLevel.HARD)
        result = under_test.calculate_next_move(board, self.o_token)

        self.assertIsNotNone(result)
示例#3
0
    def build_game(args: Namespace) -> Game:
        players = []
        ui = ConsoleUI()
        tokens = []
        new_game = None
        ai_brain = None

        # 1. Get the appropriate tokens and AI Brain
        if args.game == GameName.TIC_TAC_TOE:
            tokens = TIC_TAC_TOE_TOKENS.copy()
            ai_brain = TicTacToeBrain(args.level)
        elif args.game == GameName.CHECKERS:
            tokens = CHECKERS_TOKENS.copy()
            ai_brain = CheckersBrain(args.level)

        # 2. Build the player instances
        if args.game_mode == GameMode.LOCAL:
            for i in range(args.human_players):
                token_to_assign = tokens.pop(0)
                players.append(HumanPlayer(ui, token_to_assign))

        elif args.game_mode == GameMode.SERVER:
            for i in range(args.human_players):
                token_to_assign = tokens.pop(0)
                players.append(RemotePlayer(token_to_assign))

        elif args.game_mode == GameMode.CLIENT:
            # Client games contain only a local HumanPlayer everything else is provided by the server
            players.append(HumanPlayer(ui, tokens.pop(0)))

        # 3. Build the AI Players, if needed and as many as needed to complete 2 players
        if args.game_mode != GameMode.CLIENT:
            for i in range(len(players), 2):
                token_to_assign = tokens.pop(0)
                players.append(AIPlayer(ai_brain, token_to_assign))

        # Build the game
        if args.game == GameName.TIC_TAC_TOE:
            if args.game_mode == GameMode.LOCAL:
                new_game = TicTacToeLocalGame(players)
            elif args.game_mode == GameMode.SERVER:
                new_game = TicTacToeServerGame(players, args.port)
            elif args.game_mode == GameMode.CLIENT:
                new_game = TicTacToeClientGame(players, args.ip_address, args.port)

        elif args.game == GameName.CHECKERS:
            if args.game_mode == GameMode.LOCAL:
                new_game = CheckersLocalGame(players)
            elif args.game_mode == GameMode.SERVER:
                new_game = CheckersServerGame(players, args.port)
            elif args.game_mode == GameMode.CLIENT:
                new_game = CheckersClientGame(players, args.ip_address, args.port)

        return new_game
    def test_generate_name(self):
        expected = "AI_1"
        self.assertEqual(expected, self.under_test.name)

        expected = "AI_2"
        self.brain = TicTacToeBrain(GameLevel.HARD)
        self.under_test = AIPlayer(self.brain, self.game_token)
        self.assertEqual(expected, self.under_test.name)

        expected = "AI_3"
        self.under_test = AIPlayer(self.brain, self.game_token)
        self.assertEqual(expected, self.under_test.name)
 def setUp(self):
     AIPlayer.PLAYER_NUM = 1
     self.game_token = GameToken("X")
     self.brain = TicTacToeBrain(GameLevel.EASY)
     self.under_test = AIPlayer(self.brain, self.game_token)