From f97c3b6f5644f63503708d766e92aa01cbfe4202 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 8 Jun 2024 20:47:30 +0200 Subject: [PATCH] - refactored --- tic_tac_toe.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/tic_tac_toe.py b/tic_tac_toe.py index 98a19e3..232d6e4 100644 --- a/tic_tac_toe.py +++ b/tic_tac_toe.py @@ -178,8 +178,27 @@ class MachinePlayer(APlayer): return state[:field] + self.mark + state[field + 1:] -def play(players: list[APlayer], k_max=10000, with_print=False): +class PlayerProvider: + def __init__(self, p1: APlayer, p2: APlayer): + self.p1 = p1 + self.p2 = p2 + + +def choose_player(player_provider: PlayerProvider) -> list[APlayer]: + # Wer fängt an? + p1 = player_provider.p1 + p2 = player_provider.p2 + if uniform() < 0.5: + result = [p1, p2] + else: + result = [p2, p1] + + return result + + +def play(player_provider: PlayerProvider, k_max=10000, with_print=False): for k in range(0, k_max): + players = choose_player(player_provider) state = "---------" move = 1 run = True @@ -211,16 +230,8 @@ def play(players: list[APlayer], k_max=10000, with_print=False): move += 1 -def choose_player(p1: APlayer, p2: APlayer) -> list[APlayer]: - # Wer fängt an? - if uniform() < 0.5: - result = [p1, p2] - else: - result = [p2, p1] - - return result - - -opponents = choose_player(MachinePlayer(mark='X', with_debug=True), MachinePlayer(mark='O', with_debug=True)) -play(opponents, 1000, True) +with_debug = True +players = PlayerProvider(MachinePlayer(mark='X', with_debug=with_debug), MachinePlayer(mark='O', with_debug=with_debug)) +play(players, 10000, with_debug) +play(players, 100, True)