I think we can play against a random bot now

This commit is contained in:
Alexander Munch-Hansen 2018-03-08 13:32:40 +01:00
parent bae1e73692
commit a33826219d
2 changed files with 39 additions and 27 deletions

8
bot.py
View File

@ -30,7 +30,7 @@ class Bot:
def restore_model(self): def restore_model(self):
with self.graph.as_default(): with self.graph.as_default():
self.network.restore_model() self.network.restore_model()
def get_session(self): def get_session(self):
return self.session return self.session
@ -40,6 +40,10 @@ class Bot:
def get_network(self): def get_network(self):
return self.network return self.network
def make_random_move(self, board, sym, roll):
legal_moves = Board.calculate_legal_states(board, sym, roll)
return random.choice(list(legal_moves))
def make_move(self, board, sym, roll): def make_move(self, board, sym, roll):
# print(Board.pretty(board)) # print(Board.pretty(board))
legal_moves = Board.calculate_legal_states(board, sym, roll) legal_moves = Board.calculate_legal_states(board, sym, roll)
@ -49,4 +53,4 @@ class Bot:
#print("Found the best state, being:", np.array(move_scores).argmax()) #print("Found the best state, being:", np.array(move_scores).argmax())
return best_move_pair return best_move_pair
# return random.choice(list(legal_moves))

58
game.py
View File

@ -74,45 +74,53 @@ class Game:
print(self.board) print(self.board)
print("--------------------------------") print("--------------------------------")
def play(self): def play(self, amount_of_games):
count = 0 outcomes = []
while Board.outcome(self.board) is None: for i in range(amount_of_games):
count += 1 count = 0
print("Turn:",count) self.board = Board.initial_state
while Board.outcome(self.board) is None:
count += 1
print("Turn:",count)
roll = self.roll() roll = self.roll()
print("type of board: ", type(self.board)) print("type of board: ", type(self.board))
print("Board:",self.board) print("Board:",self.board)
print("{} rolled: {}".format(self.p1.get_sym(), roll)) print("{} rolled: {}".format(self.p1.get_sym(), roll))
self.board = (self.p1.make_move(self.board, self.p1.get_sym(), roll))[0] self.board = (self.p1.make_move(self.board, self.p1.get_sym(), roll))[0]
print(self.board)
print(self.board) print()
print()
count += 1 count += 1
roll = self.roll() roll = self.roll()
print("{} rolled: {}".format(self.p2.get_sym(), roll)) print("{} rolled: {}".format(self.p2.get_sym(), roll))
self.board = self.p2.make_move(self.board, self.p2.get_sym(), roll)[0] self.board = Board.flip(self.p2.make_random_move(Board.flip(self.board), self.p2.get_sym(), roll))
if Board.outcome(self.board)[1] > 0: if Board.outcome(self.board)[1] > 0:
print_winner = "1: White, " + str(Board.outcome(self.board)) print_winner = "1: White, " + str(Board.outcome(self.board))
else: else:
print_winner = "-1: Black " + str(Board.outcome(self.board)) print_winner = "-1: Black " + str(Board.outcome(self.board))
print("The winner is {}!".format(print_winner)) outcomes.append(Board.outcome(self.board)[1])
print("Final board:",Board.pretty(self.board)) print("The winner is {}!".format(print_winner))
return count print("Final board:",Board.pretty(self.board))
return outcomes
# return count
highest = 0 highest = 0
#for i in range(100000): #for i in range(100000):
# try: # try:
g = Game() g = Game()
g.train_model() #g.train_model()
outcomes = g.play(2000)
print(outcomes)
print(sum(outcomes))
#count = g.play() #count = g.play()
# highest = max(highest,count) # highest = max(highest,count)
# except KeyboardInterrupt: # except KeyboardInterrupt: