def test_init(): """Test CFE engine init method.""" # Try to initialize CoinFlipEngine with negative probability try: CoinFlipEngine(prob_win=-1) assert False except AttributeError: pass # Try to initialize CoinFlipEngine with probability > 1 try: CoinFlipEngine(prob_win=50) assert False except AttributeError: pass
def test_run_game(): """Test run_game functions properly.""" # Set up variables ba1 = BaseAgent() ba2 = BaseAgent() cfe = CoinFlipEngine() lad = WeightedLadder(game=cfe) # Add players to the ladder lad.add_player(ba1) lad.add_player(ba2) # Run the game lad.run_game() # Check that the ladder updated properly players = lad.get_players() player1 = players[0] player2 = players[1] # Only one elo value changes assert ((player1.elo > 1000 and player2.elo == 1000) or (player1.elo == 1000 and player2.elo > 1000)) # Someone won the game assert ((player1.num_wins == 0 and player2.num_wins == 1) or (player1.num_wins == 1 and player2.num_wins == 0)) # Someone lost the game assert ((player1.num_losses == 0 and player2.num_losses == 1) or (player1.num_losses == 1 and player2.num_losses == 0))
def test_get_players_sorted(): """Run get_players with sorted flag to true.""" # Set up variables ba1 = BaseAgent() ba2 = BaseAgent() cfe = CoinFlipEngine() lad = BaseLadder(game=cfe) lad.match_func = mock_match_func # Add players to the ladder lad.add_player(ba1) lad.add_player(ba2) # Run the game lad.run_game() # Check that the results are sorted in ascending elo players = lad.get_players(sort=True) player1 = players[0] player2 = players[1] assert player1.elo > player2.elo assert (player1.num_wins == 1 and player2.num_wins == 0) assert (player1.num_losses == 0 and player2.num_losses == 1)
def __init__(self, **kwargs): """Initialize CF Simulation.""" cf_kwargs = kwargs cf_kwargs["game"] = CoinFlipEngine() cf_kwargs["prefix"] = "CF" super().__init__(kwargs) self.add_agents()
def test_run(): """Run single iteration of game.""" player1 = BaseAgent() player2 = BaseAgent() cfe_win = CoinFlipEngine(prob_win=1) outcome = cfe_win.run(player1, player2) assert outcome == 1 cfe_loss = CoinFlipEngine(prob_win=0) outcome = cfe_loss.run(player1, player2) assert outcome == 0