backgammon/game.py

108 lines
3.1 KiB
Python
Raw Normal View History

2018-02-05 21:31:34 +00:00
from board import Board
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-04 16:35:36 +00:00
self.p1 = Bot(1)
2018-03-06 10:06:38 +00:00
self.p2 = RestoreBot(-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
2018-03-06 10:06:38 +00:00
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
2018-03-04 16:35:36 +00:00
self.next_round()
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-04 16:35:36 +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()
print(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):
count = 0
2018-03-06 10:06:38 +00:00
while Board.outcome(self.board) is None:
count += 1
print("Turn:",count)
2018-02-07 15:27:03 +00:00
roll = self.roll()
2018-03-04 16:35:36 +00:00
print("type of board: ", type(self.board))
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]
print(self.board)
print()
count += 1
2018-02-07 15:27:03 +00:00
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))
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)