def make_move(self, board, learning = False): # flatten the board from a grid to 1D for the neural network if learning: inputs = Game.flatten(board) else: inputs = Game.flatten(board, self.player) self.memories.observe(inputs) # get the neural networks movs self.net.set_input(inputs) self.net.forward_propagate() output = self.net.get_output() # if we are learning, we want to take the neural networks top choice, # but if we are in a game, it needs to pick a valid move while True: move = output.index(max(output)) # highest rated move by NN # Convert to x, y y = move/SIZE x = move%SIZE if learning: return move if board[x][y] == EMPTY: return move else: output[move] = -1
def main(): # Testing code """ game = Game('large_normal_186') game.load_events() game.process_events() print(game.generate_vote_count(278)) print('\nNow in RC style!\n') print(game.generate_vote_count(278, style='large_normal_186')) my_list = Component.create('players_list', game_state=game.game_state, post=game.game_state.post) print(my_list.generate()) print('\nLiving Players:\n') my_list = Component.create('players_list', game_state=game.game_state, post=game.game_state.post, filter='living') print(my_list.generate()) print('\nDead Players:\n') my_list = Component.create('players_list', game_state=game.game_state, post=game.game_state.post, filter='dead') print(my_list.generate()) print('\nModkilled Players:\n') my_list = Component.create('players_list', game_state=game.game_state, post=game.game_state.post, filter='modkilled') print(my_list.generate()) print('breakpoint') """ game = Game('mini_theme_1974') game.load_events() game.process_events() for election in game.game_state.elections: for vc in election.vote_counts: print(game.generate_vote_count(vc, style='Micc') + '\n\n') my_hacked_in_component = Component.create('players_list', game_state=game.game_state, post=game.game_state.post, style='Micc') print(my_hacked_in_component.generate()) print('\n\nbreakpoint')
def score(self, board, depth): winner = Game.winner(board) if winner == self.player: # I win return 10 if winner == (-1*self.player): # I lose return -10 return 0 # tie
def __init__(self): self.curr_player = X # whose turn is it? # the computer players self.learning_player = LearningPlayer(O) self.perfect_player = PerfectPlayer(O) self.computer_player = self.learning_player self.game = Game() # new game/board # run the gui self.gui = Display(self) self.gui.mainloop()
def learn_all_known_boards(self): self.passed_moves = self.failed_moves = 0 # solve from the reference frame of the X player perfect_player = PerfectPlayer(X) for board in self.memories.get_memories(): # build a grid out of a flattened board grid_board = Game.unflatten(board) # don't recompute move, if it's already be calculated if self.memories.remember_move(board) >= 0: correct_move = self.memories.remember_move(board) # get the move from the perfect player and store it in memory else: correct_move = perfect_player.make_move(grid_board) self.memories.learn_move(board, correct_move) self.learn_move(grid_board, correct_move) # learn the move
def minimax(self, player, board, depth, alpha, beta): if Game.game_over(board): return self.score(board, depth) # all empty positions where a move can be made moves = [(i, j) for i in range(SIZE) for j in range(SIZE) if board[i][j] == EMPTY] best_score = alpha if player == self.player else beta # try each move and see how well it does for move in moves: this_board = deepcopy(board) # make the move i, j = move this_board[i][j] = player # maximizing player if player == self.player: score = self.minimax(-1*player, this_board, depth+1, best_score, beta) if score > best_score: best_score = score best_move = j*SIZE + i if beta <= best_score: break # minimizing player else: score = self.minimax(-1*player, this_board, depth+1, alpha, best_score) if score < best_score: best_score = min(best_score, score) best_move = j*SIZE + i if best_score <= alpha: break return best_score if depth else best_move
class Controller: def __init__(self): self.curr_player = X # whose turn is it? # the computer players self.learning_player = LearningPlayer(O) self.perfect_player = PerfectPlayer(O) self.computer_player = self.learning_player self.game = Game() # new game/board # run the gui self.gui = Display(self) self.gui.mainloop() # reset the game def reset(self): self.curr_player = X self.game = Game() if self.computer_player.get_player() == X: self.get_computer_move() # X goes first # Called by the GUI so this means a human moved. Update the game and get the computer's move def make_move(self, x, y): # check that the move is valid if self.get_position(x,y) == EMPTY and self.game.set_position(self.curr_player, x, y): self.switch_player() # other players turn now winner = self.game_over() # was there a winner? # game isn't over so get the computer player's move if not winner: self.get_computer_move() # get the move from the computer def get_computer_move(self): move = self.computer_player.make_move(self.game.board) # the computer's move # convert from 1D to 2D y = move/SIZE x = move%SIZE self.game.set_position(self.curr_player, x, y) # update the game board self.gui.update_button(x, y) # update the GUI self.game_over() # check if the game is over self.switch_player() # other players turn now # this is the fun stuff! Have the machine learn from the observed behavior def learn(self): print '\n\nLearning\n\n' # Ideally we want it to learn until it gets every move right, but at somepoint we have to stop learning for i in range(20000): self.learning_player.learn_all_known_boards() # see how many moves the machine got right and how many it got wrong print 'Pass', i, 'correct_moves:', self.learning_player.passed_moves, 'incorrect moves:', self.learning_player.failed_moves if not self.learning_player.failed_moves: # got every move right! num_passes = i break print '\n\nIt took', i, 'iterations but I learned all of the moves!\n\n' def forget(self): self.learning_player.forget() print '\n\nI just forgot how to play\n\n' def get_position(self, x, y): return self.game.get_position(x, y) # has somebody won? if not, was there a tie? def game_over(self): winner = self.game.has_winner() if winner: # somebody won self.gui.game_over(winner) return True elif self.game.is_full(): # board is full -> tie self.gui.game_over(None) return True return False # game is not over # set what player the computer player is (X or O) def set_player(self, player): self.learning_player.set_player(-1*STRINGS[player]) self.perfect_player.set_player(-1*STRINGS[player]) # switch between whose turn it is def switch_player(self): if self.curr_player == X: self.curr_player = O else: self.curr_player = X # set the type of computer player (learning or perfect) def set_comp_type(self, comp_type): if comp_type == 'Perfect': self.computer_player = self.perfect_player else: self.computer_player = self.learning_player
def reset(self): self.curr_player = X self.game = Game() if self.computer_player.get_player() == X: self.get_computer_move() # X goes first