from board import Board from bot import Bot from restore_bot import RestoreBot import numpy as np from cup import Cup class Game: def __init__(self): self.board = Board.initial_state self.p1 = Bot(1) self.p2 = RestoreBot(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 = Board.flip(self.p2.make_move(Board.flip(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 prev_board = self.board while Board.outcome(self.board) is None: 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 self.next_round() # print("-"*30) # print(Board.pretty(self.board)) # print("/"*30) print("Outcome:", Board.outcome(self.board)[1]) outcomes.append(Board.outcome(self.board)[1]) 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)) 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) is 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)