- refactored

This commit is contained in:
2024-06-08 20:47:30 +02:00
parent 5b7430f208
commit f97c3b6f56
+24 -13
View File
@@ -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)