def match(p1, p2, games, gname): # game on log.info('playing <%s> against <%s>...', p1.q.fname, 'itself' if p1==p2 else p2.q.fname) wins_left = wins_right = draws = played = 0 for game in range(games): left_starts = random.choice([True, False]) state,_,rounds = Episode(AlgoPlay(), p1, p2).run(State.create(gname, left_starts)) log.info('game %d: 1st=%s, rounds=%3d, winner=%s, score=%d/%d', game, 'left ' if left_starts else 'right', rounds, 'left ' if state.player_wins() else 'right' if state.opponent_wins() else 'draw', state.player_score(), state.opponent_score() ) played += 1 if state.player_wins(): wins_left += 1 elif state.opponent_wins(): wins_right += 1 else: draws += 1 if not running: break log.info('stats: left %d%% / right %d%% / draw %d%%', wins_left*100/played, wins_right*100/played, draws*100/played ) return 1 if wins_left > wins_right else 0 if wins_left == wins_right else -1
def test_opponent_starts_and_wins(self): self.policy.play.side_effect = [(2,0,0)] state, rounds, turns = Episode(self.algo, self.policy).run(StateTest(False)) self.assertEqual(state.opponent_wins(), True) self.assertEqual((3, 3), (rounds, turns)) self.assertEqual(1, self.algo.update.call_count)