Skip to content

Commit

Permalink
Make summary display optional in play controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
donkirkby committed Nov 9, 2023
1 parent b38c432 commit b4cb893
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
20 changes: 20 additions & 0 deletions tests/test_play_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ def test_display(capsys):
assert line.startswith(expected_line)


def test_no_display(capsys):
game = SecondPlayerWinsGame()
heuristic = FirstChoiceHeuristic()
players = [MctsPlayer(game,
game.X_PLAYER,
iteration_count=10,
heuristic=heuristic),
MctsPlayer(game,
game.O_PLAYER,
iteration_count=20,
heuristic=heuristic)]
controller = PlayController(game, players)
expected_output = ""

controller.play(games=2, flip=True, display_summary=False)

out, err = capsys.readouterr()
assert out == expected_output


def test_player_results():
player = MctsPlayer(FirstPlayerWinsGame(), iteration_count=100)
player_results = PlayerResults(player)
Expand Down
10 changes: 9 additions & 1 deletion zero_play/play_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ def get_player_results(self, player: Player) -> PlayerResults:
return player_results
raise ValueError('Player not found.')

def play(self, games: int = 1, flip: bool = False, display: bool = False):
def play(self,
games: int = 1,
flip: bool = False,
display: bool = False,
display_summary: bool = True):
x_number = self.start_state.players[0]
o_number = self.start_state.players[1]
current_x = original_x = self.players[x_number]
Expand Down Expand Up @@ -98,6 +102,10 @@ def play(self, games: int = 1, flip: bool = False, display: bool = False):
original_o.player_number = o_number
self.players[x_number] = original_x
self.players[o_number] = original_o
if display_summary:
for player_results in self.results:
print(player_results.get_summary())
print(ties, 'ties')
x_results = self.get_player_results(original_x)
o_results = self.get_player_results(original_o)

Expand Down
6 changes: 4 additions & 2 deletions zero_play/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@ def train(search_milliseconds: int,
logger.info('Testing.')
wins_vs_base, base_ties, base_wins = base_controller.play(
comparison_size,
flip=True)
flip=True,
display_summary=False)
wins_vs_best, best_ties, best_wins = best_controller.play(
comparison_size,
flip=True)
flip=True,
display_summary=False)
writer.writerow(dict(wins_vs_base=wins_vs_base/comparison_size,
ties_vs_base=base_ties/comparison_size,
wins_vs_best=wins_vs_best/comparison_size,
Expand Down

0 comments on commit b4cb893

Please sign in to comment.