117 lines
3.3 KiB
Python
117 lines
3.3 KiB
Python
import time
|
|
from human import Human
|
|
from board import Board
|
|
from bot import Bot
|
|
from restore_bot import Restore_bot
|
|
import tensorflow as tf
|
|
import numpy as np
|
|
import random
|
|
|
|
from cup import Cup
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
self.board = Board.initial_state
|
|
# self.session = tf.Session()
|
|
# self.restored_network = Network(self.session)
|
|
# self.network = Network(self.session)
|
|
# self.restored_network.restore_model()
|
|
|
|
self.p1 = Bot(1)
|
|
self.p2 = Restore_bot(-1)
|
|
self.cup = Cup()
|
|
|
|
def roll(self):
|
|
return self.cup.roll()
|
|
|
|
def roll_and_find_best_for_bot(self):
|
|
roll = self.roll()
|
|
move_and_val = self.p1.make_move(self.board, self.p1.get_sym(), roll)
|
|
self.board = move_and_val[0]
|
|
return move_and_val
|
|
|
|
def next_round(self):
|
|
roll = self.roll()
|
|
print(roll)
|
|
self.board = self.p2.make_move(self.board, self.p2.get_sym(),roll)
|
|
return self.board
|
|
|
|
def board_state(self):
|
|
return self.board
|
|
|
|
def train_model(self):
|
|
episodes = 100
|
|
outcomes = []
|
|
for episode in range(episodes):
|
|
self.board = Board.initial_state
|
|
x = self.board
|
|
while Board.outcome(self.board) == None:
|
|
x_next, v_next = self.roll_and_find_best_for_bot()
|
|
self.p1.get_network().train(x, v_next)
|
|
x = x_next
|
|
self.next_round()
|
|
print("Outcome:",Board.outcome(self.board)[1])
|
|
outcomes.append(Board.outcome(self.board)[1])
|
|
self.p1.get_network().train(x, np.array([Board.outcome(self.board)[1]]).reshape((1,1)))
|
|
print("trained an episode")
|
|
if episode % 10 == 0:
|
|
print("Saving ....")
|
|
self.p1.get_network().save_model()
|
|
print(outcomes)
|
|
|
|
|
|
def next_round_test(self):
|
|
print(self.board)
|
|
print()
|
|
self.next_round()
|
|
print("--------------------------------")
|
|
print(self.board)
|
|
print("--------------------------------")
|
|
|
|
|
|
def play(self):
|
|
count = 0
|
|
while Board.outcome(self.board) == None:
|
|
count += 1
|
|
|
|
print("Turn:",count)
|
|
|
|
roll = self.roll()
|
|
|
|
print("type of board: ", type(self.board))
|
|
print("Board:",self.board)
|
|
print("{} rolled: {}".format(self.p1.get_sym(), roll))
|
|
|
|
self.board = (self.p1.make_move(self.board, self.p1.get_sym(), roll))[0]
|
|
|
|
print(self.board)
|
|
|
|
print()
|
|
|
|
count += 1
|
|
|
|
roll = self.roll()
|
|
print("{} rolled: {}".format(self.p2.get_sym(), roll))
|
|
self.board = self.p2.make_move(self.board, self.p2.get_sym(), roll)
|
|
|
|
|
|
if Board.outcome(self.board)[1] > 0:
|
|
print_winner = "1: White, " + str(Board.outcome(self.board))
|
|
else:
|
|
print_winner = "-1: Black " + str(Board.outcome(self.board))
|
|
print("The winner is {}!".format(print_winner))
|
|
print("Final board:",Board.pretty(self.board))
|
|
return count
|
|
|
|
highest = 0
|
|
|
|
#for i in range(100000):
|
|
# try:
|
|
g = Game()
|
|
g.train_model()
|
|
#count = g.play()
|
|
# highest = max(highest,count)
|
|
# except KeyboardInterrupt:
|
|
# break
|
|
#print("\nHighest amount of turns is:",highest)
|