63 lines
2.3 KiB
Python
63 lines
2.3 KiB
Python
import numpy as np
|
|
from helper import create_empty_state, to_state_string
|
|
from machine_player import MachinePlayer
|
|
from player_provider import PlayerProvider
|
|
|
|
float_formatter = "{:.3f}".format
|
|
np.set_printoptions(formatter={'float_kind': float_formatter})
|
|
|
|
|
|
def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
|
|
for k in range(0, k_max):
|
|
players = player_provider.choose()
|
|
state = create_empty_state()
|
|
move = 1
|
|
run = True
|
|
other_player = players[-1]
|
|
while run:
|
|
for player in players:
|
|
if with_print:
|
|
print(f"---------------------------------------------------")
|
|
print(f"- Game {k:06d}, Move {move} -----------------------------")
|
|
print(f"---------------------------------------------------")
|
|
last_state = state
|
|
state, has_moved = player.move(state)
|
|
if with_print:
|
|
print(to_state_string(last_state, state))
|
|
if not has_moved:
|
|
if with_print:
|
|
print(f"{player.mark}: No more moves")
|
|
if isinstance(player, MachinePlayer):
|
|
player.reward(0.0)
|
|
if isinstance(other_player, MachinePlayer):
|
|
other_player.reward(0.0)
|
|
run = False
|
|
if player.has_won(state):
|
|
if with_print:
|
|
print(f"{player.mark}: Has won the game")
|
|
if isinstance(player, MachinePlayer):
|
|
player.reward(1.0)
|
|
if isinstance(other_player, MachinePlayer):
|
|
other_player.reward(0.0)
|
|
run = False
|
|
|
|
other_player = player
|
|
if not run:
|
|
break
|
|
|
|
move += 1
|
|
|
|
|
|
px = MachinePlayer(mark='X', with_debug=False)
|
|
po = MachinePlayer(mark='O', with_debug=False)
|
|
|
|
do_training = 1
|
|
if do_training:
|
|
players = PlayerProvider(px, po)
|
|
play(players, 10000, False)
|
|
|
|
players = PlayerProvider(MachinePlayer(mark='X', with_debug=True, values=px.values), MachinePlayer(mark='O', with_debug=True, values=po.values))
|
|
play(players, 1000, True)
|
|
|
|
px.print_state_table()
|