def test_choose_piece(self): """ Tests choose_piece() method in the PlayerEasyAI class """ test = PlayerEasyAI(Game(), "Test") test.game.board = [['0000', '1000', '0001', '0101'], [None, '0010', '0111', None], ['1001', None, '0100', '1111'], ['1011', '0110', None, '1110']] test.game.pieces = {'3': '0011', '10': '1010', '12': '1100', '13': '1101'} self.assertEqual('3', test.choose_piece())
def init_players(self, mod, dif=0, p_one="Player One", p_two="Player Two"): """ Initializes the two players for the game dependent on game mode :param mod: the game mode being played; human vs human, AI vs AI, ... :param dif: the difficulty being played when playing AI :param p_one: the name for player one; default: Player One :param p_two: the name for player two; default: Player Two """ if mod == 1: # if human player vs human player self.players = [ PlayerHuman(self.game, p_one), PlayerHuman(self.game, p_two) ] elif mod == 2: # if human player vs AI player if dif == 1: # if easy difficulty AI self.players = [ PlayerHuman(self.game, p_one), PlayerEasyAI(self.game, p_two) ] elif dif == 2: # if medium difficulty AI self.players = [ PlayerHuman(self.game, p_one), PlayerMediumAI(self.game, p_two) ] else: # if hard difficulty AI self.players = [ PlayerHuman(self.game, p_one), PlayerHardAI(self.game, p_two) ] else: # if AI player vs AI player if dif == 1: # if easy difficulty AI self.players = [ PlayerEasyAI(self.game, p_one), PlayerEasyAI(self.game, p_two) ] elif dif == 2: # if medium difficulty AI self.players = [ PlayerMediumAI(self.game, p_one), PlayerMediumAI(self.game, p_two) ] else: # if hard difficulty AI self.players = [ PlayerHardAI(self.game, p_one), PlayerHardAI(self.game, p_two) ] self.current_player = choice(self.players) # random starting player
def test_place_piece(self): """ Tests choose_piece() method in the PlayerEasyAI class """ test = PlayerEasyAI(Game(), "Test") test.game.board = [[None, '1000', '0001', '0101'], [None, '0000', '0010', '0111'], ['1001', None, '1111', '1011'], ['0100', '0110', '0011', '1110']] test.game.pieces = {'10': '1010', '12': '1100', '13': '1101'} test.place_piece('1010') self.assertTrue((test.game.board == [[None, '1000', '0001', '0101'], ['1010', '0000', '0010', '0111'], ['1001', None, '1111', '1011'], ['0100', '0110', '0011', '1110']] or test.game.board == [['1010', '1000', '0001', '0101'], [None, '0000', '0010', '0111'], ['1001', None, '1111', '1011'], ['0100', '0110', '0011', '1110']]) and not test.game.has_won_game('1010'))
def test_player_ai_easy_vs_ai_hard_play(self): """ Tests average EasyAI player wins against HardAI player <= 5% Theoretically, should never win against the hard (minimax) AI """ p_one, p_two, wins = "Player One", "Player Two", 0 samples = 100 # the number of samples of won games in each test for i in range(samples): test = Play() test.players = [PlayerEasyAI(test.game, p_one), PlayerHardAI(test.game, p_two)] test.current_player = choice(test.players) if test.play_auto() and test.current_player.name == p_two: wins += 1 self.assertTrue(((samples - wins) / samples * 100) <= 5)
def test_player_ai_easy_vs_ai_medium_play(self): """ Tests average EasyAI player wins against MediumAI player <= 15% Is usually below 10% win rate, but can by chance be just above 10% """ p_one, p_two, wins = "Player One", "Player Two", 0 samples = 100 # the number of samples of won games in each test for i in range(samples): test = Play() test.players = [PlayerEasyAI(test.game, p_one), PlayerMediumAI(test.game, p_two)] test.current_player = choice(test.players) if test.play_auto() and test.current_player.name == p_one: wins += 1 self.assertTrue((wins / samples * 100) <= 10)
def test_player_ai_hard_vs_ai_easy_play(self): """ Test HardAI for average wins against EasyAI being >= 95% """ average, p_one, p_two = 0, "Player One", "Player Two" for i in range(100): test = Play() test.players = [ PlayerHardAI(test.game, p_one), PlayerEasyAI(test.game, p_two) ] test.current_player = choice(test.players) if test.play_auto() and test.current_player == p_one: average += 1 self.assertTrue(average >= 95)
def test_player_ai_medium(self): """ Tests PlayerEasyAI class instance """ test = PlayerEasyAI(Game(), "Test") self.assertTrue(isinstance(test, PlayerEasyAI))