backgammon/bot.py

49 lines
1.2 KiB
Python
Raw Normal View History

from cup import Cup
2018-03-04 16:35:36 +00:00
import tensorflow as tf
from network import Network
import numpy as np
2018-02-13 13:38:49 +00:00
from board import Board
import random
class Bot:
2018-02-13 13:38:49 +00:00
def __init__(self, sym):
self.cup = Cup()
self.sym = sym
2018-03-04 16:35:36 +00:00
self.graph = tf.Graph()
with self.graph.as_default():
self.session = tf.Session()
self.network = Network(self.session)
def roll(self):
print("{} rolled: ".format(self.sym))
roll = self.cup.roll()
print(roll)
return roll
def switch(self,cur):
return -1 if cur == 1 else 1
2018-03-04 16:35:36 +00:00
def get_session(self):
return self.session
def get_sym(self):
return self.sym
2018-03-04 16:35:36 +00:00
def get_network(self):
return self.network
def make_move(self, board, sym, roll):
# print(Board.pretty(board))
legal_moves = Board.calculate_legal_states(board, sym, roll)
2018-03-04 16:35:36 +00:00
legal_list = list(legal_moves)
lol = []
for move in legal_list:
lol.append(self.network.eval_state(np.array(move).reshape(1,26)))
print("Found the best state, being:", np.array(lol).argmax())
return [legal_list[np.array(lol).argmax()], max(lol)]
2018-03-04 16:35:36 +00:00
# return random.choice(list(legal_moves))