Files
tic_tac_toe/tic_tac_toe.py
T

104 lines
2.6 KiB
Python

import numpy as np
from state import create_empty_state, to_state_string
from machine_player import MachinePlayer
from human_player import HumanPlayer
from player_provider import PlayerProvider
import json
float_formatter = "{:.3f}".format
np.set_printoptions(formatter={'float_kind': float_formatter})
REPORT_INTERVAL = 1000
report_counter = REPORT_INTERVAL
def play(player_provider: PlayerProvider, k_max=10000, with_debug=False):
player_provider.set_debug(with_debug)
for k in range(0, k_max):
state = create_empty_state()
players = player_provider.choose(state)
move = 1
run = True
other_player = players[-1]
while run:
do_stop = False
this_reward = 0
other_reward = 0
for this_player in players:
if with_debug:
print(f"---------------------------------------------------")
print(f"- Game {k:06d}, Move {move} -----------------------------")
print(f"---------------------------------------------------")
last_state = state
state, has_moved = this_player.move(state)
if with_debug:
print(to_state_string(last_state, state))
if not has_moved:
this_reward = 0
other_reward = 0
do_stop = True
if with_debug:
print(f"{this_player.mark}: No more moves")
if this_player.has_won(state):
this_reward = 1
other_reward = 0
do_stop = True
if with_debug:
print(f"{this_player.mark}: Has won the game")
if do_stop:
this_player.end_game(state, this_reward)
other_player.end_game(state, other_reward)
run = False
break
other_player = this_player
move += 1
do_training = 0
do_human_player = 0
do_load_values = 1
if do_load_values:
try:
with open("values_x.json", "r") as fp:
x_values = json.load(fp)
with open("values_o.json", "r") as fp:
o_values = json.load(fp)
except Exception:
x_values = {}
o_values = {}
else:
x_values = {}
o_values = {}
p_hx = HumanPlayer(mark='X', name="Jens")
p_mx = MachinePlayer(mark='X', params=MachinePlayer.Params(p_explore=0.1, alpha=0.1), values=x_values)
p_mo = MachinePlayer(mark='O', params=MachinePlayer.Params(p_explore=0.1, alpha=0.1), values=o_values)
K = 5
N = 1000
if do_training:
for k in range(0, K):
play(PlayerProvider(p_mx, p_mo), N, False)
# Values after training
# Convert and write JSON object to file
with open("values_x.json", "w") as fp:
json.dump(p_mx.values, fp, indent=0)
with open("values_o.json", "w") as fp:
json.dump(p_mo.values, fp, indent=0)
print(f"Iteration: {k*N:8d}")
if do_human_player:
pp = PlayerProvider(p_hx, p_mo)
else:
pp = PlayerProvider(p_mx, p_mo)
play(pp, 10, True)