def test_selected_piece(self): """ Tests selected_piece variable in Play class instance """ test = Play() self.assertEqual(test.selected_piece, None) test.selected_piece = test.game.pieces['0'] self.assertEqual(test.selected_piece, '0000') self.assertEqual(test.selected_piece, test.game.pieces['0'])
def test_players(self): """ Tests players array variable in Play class instance """ test = Play() self.assertEqual(test.players, []) test.init_players(3, 2) self.assertEqual(len(test.players), 2) for i in range(2): self.assertTrue(isinstance(test.players[i], PlayerMediumAI))
def test_current_and_change_player(self): """ Tests current_player variable and change_player() method in Play class instance """ test = Play() self.assertEqual(test.current_player, None) test.init_players(3, 2) player_one = test.current_player self.assertTrue(isinstance(player_one, PlayerMediumAI)) player_two = test.change_player() self.assertTrue(isinstance(player_two, PlayerMediumAI)) self.assertNotEqual(player_one, player_two)
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_hard_play(self): """ Test HardAI for average wins against itself being <= 10% """ average = 0 for i in range(100): test = Play() test.players = [ PlayerHardAI(test.game, "Player One"), PlayerHardAI(test.game, "Player Two") ] test.current_player = choice(test.players) if test.play_auto() and test.current_player == "Player One": average += 1 self.assertTrue(average <= 10)
def test_play_auto(self): """ Tests that play_auto() returns only game result and winner if any play_auto() returns winner's name if game won, or None if game drawn """ p_one, p_two = "Player One", "Player Two" test = Play() test.players = [ PlayerMediumAI(test.game, p_one), PlayerMediumAI(test.game, p_two) ] test.current_player = test.players[0] result = test.play_auto() if result: self.assertTrue(result in [i.name for i in test.players]) else: self.assertFalse(result)
def test_player_ai_medium_play(self): """ Tests average PlayerMediumAI player wins 50% +/- 15% in non-drawn games Usually between 40-60% win rate, but can by chance be just below/over """ p_one, p_two, count, wins = "Player One", "Player Two", 0, 0 samples = 100 # the number of samples of won games in each test while count < samples: test = Play() test.players = [ PlayerMediumAI(test.game, p_one), PlayerMediumAI(test.game, p_two) ] test.current_player = choice(test.players) if test.play_auto(): count += 1 if test.current_player.name == p_one: wins += 1 self.assertTrue((35 <= (wins / samples * 100) <= 65))
def test_easy_difficulty_ai_vs_ai(self): """ Tests initialisation of two AI players in AI vs AI game play at easy difficulty """ test, count = Play(), 0 test.init_players(3, 1) for i in range(2): if test.current_player.name == "Player One": self.assertTrue(isinstance(test.current_player, PlayerEasyAI)) count += 1 elif test.current_player.name == "Player Two": self.assertTrue(isinstance(test.current_player, PlayerEasyAI)) count += 1 test.change_player() self.assertEqual(len(test.players), 2) self.assertEqual(count, 2)
def test_easy_difficulty_human_vs_ai(self): """ Tests initialisation of human and AI players in human vs AI game play at easy difficulty """ test, count = Play(), 0 test.init_players(2, 1, "Peter") for i in range(2): if test.current_player.name == "Peter": self.assertTrue(isinstance(test.current_player, PlayerHuman)) count += 1 elif test.current_player.name == "Player Two": self.assertTrue(isinstance(test.current_player, PlayerEasyAI)) count += 1 test.change_player() self.assertEqual(len(test.players), 2) self.assertEqual(count, 2)
def test_human_vs_human(self): """ Tests initialisation of two human players in human vs human game play """ test, count = Play(), 0 test.init_players(1, None, "Andy", "Joe") for i in range(2): if test.current_player.name == "Andy": self.assertTrue(isinstance(test.current_player, PlayerHuman)) count += 1 elif test.current_player.name == "Joe": self.assertTrue(isinstance(test.current_player, PlayerHuman)) count += 1 test.change_player() self.assertEqual(len(test.players), 2) self.assertEqual(count, 2)
class GameEngine: """ This is temporary class for the CLI testing and presentation Author(s): Adam Ross; Gustav From Last-edit-date: 14/02/2019 """ def __init__(self): """ The GameEngine CLI test class constructor """ self.play = Play() # initiates a Play class instance self.introduction() # Prints to terminal an introduction to the game def introduction(self): print("\n*** Welcome to GameEngine ***") def declare_available_pieces(self): """ Declares to the players the pieces available for selection Temporary for the CLI testing """ print("\nGame pieces status:") print( list(self.play.game.pieces.items())[:int( (len(self.play.game.pieces) + 1) / 2)]) if len(self.play.game.pieces) > 1: print( list(self.play.game.pieces.items())[int( (len(self.play.game.pieces) + 1) / 2):]) def declare_board_status(self): """ Declares to the players the current status of the game board Temporary for the CLI testing """ print("\nGame board status:") print(*(row for row in self.play.game.board), sep="\n") def declare_current_player(self): """ Temporary printing of current player for CLI testing and presenting """ print("\nCurrent player: '" + self.play.current_player.name + "'") def declare_selected_piece(self): """ Temporary printing of selected piece for CLI testing and presenting """ print("\nCurrent piece: " + self.play.selected_piece) def game_mode_selection(self): while True: print("\nSelect a following game mode (enter number 1 - 3):") n = input("1: Player vs Player\n2: Player vs AI\n3: AI vs AI\n") if n == "2" or n == "3": print("\nSelect a following difficulty (enter number 1 - 3):") d = input("1: easy\n2: medium\n3: hard\n") if d == "1" or d == "2" or d == "3": self.play.init_players(int(n), int(d)) # initializes play break elif n == "1": self.play.init_players(int(n), None) # initializes players break self.declare_current_player() # prints the starting player turn def play_game(self): self.declare_available_pieces() # prints game board status self.declare_board_status() # prints available pieces status if isinstance(self.play.current_player, PlayerHuman): while True: pce = input("\nEnter number 0-15 of piece selection: ") if self.play.play_selection(pce): break else: self.play.play_selection() self.declare_selected_piece() # prints the selected piece self.declare_current_player() # prints the current player turn if isinstance(self.play.current_player, PlayerHuman): while True: try: y, x = input("\nEnter 2 ints 0-3 separated by a space: ").\ split() if self.play.play_placement(y, x): break except: continue else: self.play.play_placement() if self.play.game.has_won_game(self.play.selected_piece): self.declare_board_status() # prints final status of board print("game won by " + self.play.current_player.name) elif not self.play.game.has_next_play(): # checks if turns remaining self.declare_board_status() # prints final status of board else: self.play_game() # plays the next turn
def __init__(self): """ The GameEngine CLI test class constructor """ self.play = Play() # initiates a Play class instance self.introduction() # Prints to terminal an introduction to the game
def test_game_instance(self): """ Tests Game instance in Play class instance """ test = Play() self.assertTrue(isinstance(test.game, Game))
def test_play(self): """ Tests Play class instance """ test = Play() self.assertTrue(isinstance(test, Play))