2018-02-22 13:01:28 +00:00
|
|
|
import time
|
2018-02-07 15:08:59 +00:00
|
|
|
from human import Human
|
2018-02-05 21:31:34 +00:00
|
|
|
from board import Board
|
2018-02-05 22:50:31 +00:00
|
|
|
from bot import Bot
|
2018-03-04 16:35:36 +00:00
|
|
|
from restore_bot import Restore_bot
|
|
|
|
import tensorflow as tf
|
|
|
|
import numpy as np
|
|
|
|
import random
|
|
|
|
|
2018-02-07 15:27:03 +00:00
|
|
|
from cup import Cup
|
2018-02-05 21:31:34 +00:00
|
|
|
|
|
|
|
class Game:
|
|
|
|
def __init__(self):
|
2018-02-13 13:38:49 +00:00
|
|
|
self.board = Board.initial_state
|
2018-03-04 16:35:36 +00:00
|
|
|
# self.session = tf.Session()
|
|
|
|
# self.restored_network = Network(self.session)
|
|
|
|
# self.network = Network(self.session)
|
|
|
|
# self.restored_network.restore_model()
|
|
|
|
|
2018-02-22 13:01:28 +00:00
|
|
|
self.p1 = Bot(1)
|
2018-03-04 16:35:36 +00:00
|
|
|
self.p2 = Restore_bot(-1)
|
2018-02-07 15:27:03 +00:00
|
|
|
self.cup = Cup()
|
2018-02-07 14:31:05 +00:00
|
|
|
|
2018-02-07 15:27:03 +00:00
|
|
|
def roll(self):
|
|
|
|
return self.cup.roll()
|
2018-02-05 21:31:34 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
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("--------------------------------")
|
|
|
|
|
|
|
|
|
2018-02-05 21:31:34 +00:00
|
|
|
def play(self):
|
2018-02-22 13:01:28 +00:00
|
|
|
count = 0
|
|
|
|
while Board.outcome(self.board) == None:
|
|
|
|
count += 1
|
2018-03-04 16:35:36 +00:00
|
|
|
|
2018-02-22 13:01:28 +00:00
|
|
|
print("Turn:",count)
|
|
|
|
|
2018-02-07 15:27:03 +00:00
|
|
|
roll = self.roll()
|
2018-02-22 13:01:28 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
print("type of board: ", type(self.board))
|
2018-02-22 13:01:28 +00:00
|
|
|
print("Board:",self.board)
|
2018-02-07 15:27:03 +00:00
|
|
|
print("{} rolled: {}".format(self.p1.get_sym(), roll))
|
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
self.board = (self.p1.make_move(self.board, self.p1.get_sym(), roll))[0]
|
2018-02-22 13:01:28 +00:00
|
|
|
|
|
|
|
print(self.board)
|
|
|
|
|
|
|
|
print()
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
2018-02-07 15:27:03 +00:00
|
|
|
roll = self.roll()
|
2018-02-22 13:01:28 +00:00
|
|
|
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))
|
2018-03-04 16:35:36 +00:00
|
|
|
return count
|
|
|
|
|
|
|
|
highest = 0
|
2018-02-05 21:31:34 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
#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)
|