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-06 10:06:38 +00:00
|
|
|
from restore_bot import RestoreBot
|
2018-03-04 16:35:36 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2018-02-07 15:27:03 +00:00
|
|
|
from cup import Cup
|
2018-02-05 21:31:34 +00:00
|
|
|
|
2018-03-06 10:06:38 +00:00
|
|
|
|
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-06 12:04:47 +00:00
|
|
|
|
2018-02-22 13:01:28 +00:00
|
|
|
self.p1 = Bot(1)
|
2018-03-07 13:44:17 +00:00
|
|
|
self.p2 = 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
|
2018-03-06 12:04:47 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
def next_round(self):
|
|
|
|
roll = self.roll()
|
2018-03-06 15:23:08 +00:00
|
|
|
#print(roll)
|
2018-03-07 13:44:17 +00:00
|
|
|
self.board = Board.flip(self.p2.make_move(Board.flip(self.board), self.p2.get_sym(), roll)[0])
|
2018-03-04 16:35:36 +00:00
|
|
|
return self.board
|
|
|
|
|
|
|
|
def board_state(self):
|
|
|
|
return self.board
|
|
|
|
|
|
|
|
def train_model(self):
|
2018-03-07 13:44:17 +00:00
|
|
|
episodes = 100
|
2018-03-04 16:35:36 +00:00
|
|
|
outcomes = []
|
|
|
|
for episode in range(episodes):
|
|
|
|
self.board = Board.initial_state
|
2018-03-06 15:23:08 +00:00
|
|
|
# prev_board = self.board
|
|
|
|
prev_board, prev_board_value = self.roll_and_find_best_for_bot()
|
|
|
|
# find the best move here, make this move, then change turn as the
|
|
|
|
# first thing inside of the while loop and then call
|
|
|
|
# roll_and_find_best_for_bot to get V_t+1
|
|
|
|
# self.p1.make_move(prev_board, self.p1.get_sym(), self.roll())
|
2018-03-06 10:06:38 +00:00
|
|
|
while Board.outcome(self.board) is None:
|
2018-03-06 15:23:08 +00:00
|
|
|
self.next_round()
|
2018-03-06 10:06:38 +00:00
|
|
|
cur_board, cur_board_value = self.roll_and_find_best_for_bot()
|
|
|
|
self.p1.get_network().train(prev_board, cur_board_value)
|
|
|
|
prev_board = cur_board
|
2018-03-06 15:23:08 +00:00
|
|
|
# self.next_round()
|
2018-03-06 12:04:47 +00:00
|
|
|
# print("-"*30)
|
|
|
|
# print(Board.pretty(self.board))
|
|
|
|
# print("/"*30)
|
2018-03-06 10:06:38 +00:00
|
|
|
print("Outcome:", Board.outcome(self.board)[1])
|
2018-03-04 16:35:36 +00:00
|
|
|
outcomes.append(Board.outcome(self.board)[1])
|
2018-03-06 10:06:38 +00:00
|
|
|
final_score = np.array([ Board.outcome(self.board)[1] ]).reshape((1, 1))
|
|
|
|
self.p1.get_network().train(prev_board, final_score)
|
|
|
|
print("trained episode {}".format(episode))
|
2018-03-07 13:44:17 +00:00
|
|
|
if episode % 10 == 0:
|
2018-03-06 10:06:38 +00:00
|
|
|
print("Saving...")
|
2018-03-04 16:35:36 +00:00
|
|
|
self.p1.get_network().save_model()
|
2018-03-06 15:23:08 +00:00
|
|
|
self.p2.restore_model()
|
2018-03-07 13:44:17 +00:00
|
|
|
print(sum(outcomes))
|
2018-03-06 12:04:47 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
print(outcomes)
|
2018-03-06 15:23:08 +00:00
|
|
|
print(sum(outcomes))
|
2018-03-06 10:06:38 +00:00
|
|
|
|
2018-03-04 16:35:36 +00:00
|
|
|
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
|
2018-03-06 10:06:38 +00:00
|
|
|
while Board.outcome(self.board) is None:
|
2018-02-22 13:01:28 +00:00
|
|
|
count += 1
|
|
|
|
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))
|
2018-03-07 13:44:17 +00:00
|
|
|
self.board = self.p2.make_move(self.board, self.p2.get_sym(), roll)[0]
|
2018-02-22 13:01:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|