Created
August 9, 2018 18:22
-
-
Save llSourcell/ac6bff9f3f6c828e5a6c8f6b2110ed62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import numpy as np | |
| from games.games import AbstractGame | |
| def playout_value(game): | |
| if game.over(): | |
| return -game.score() | |
| move = random.choice(game.valid_moves()) | |
| game.make_move(move) | |
| value = -playout_value(game)) | |
| game.undo_move() | |
| return value | |
| #Finds the expected value of a game by running random simulations | |
| def monte_carlo_value(game, N=100): | |
| scores = [playout_value(game) for i in range(0, N)] | |
| return np.mean(scores) | |
| # Chooses best valued move to play using Monte Carlo Tree search. | |
| def ai_best_move(game): | |
| action_dict = {} | |
| for move in game.valid_moves(): | |
| game.make_move(move) | |
| action_dict[move] = -monte_carlo_value(game) | |
| game.undo_move() | |
| return max(action_dict, key=action_dict.get) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment