-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex_player.py
More file actions
50 lines (42 loc) · 2.31 KB
/
index_player.py
File metadata and controls
50 lines (42 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'''
This code is not completely documented because it is not used in our website project.
I have included it as an example of a player you can create with the classes that are defined in our project.
For some time, this was used as our tic-tac-toe player, but was abandoned when position evaluation was coded.
'''
from players import Player
class IndexPlayer(Player):
'''A basic class for any player whose moves can be made out of a dictionary'''
def __init__(self, index_func):
# a function that returns a dict of moves to remember
self.index_func = index_func
# see the `memory` property of the SolvePlayer class to understand how predetermined works
self.predetermined = {}
def make_move(self, game):
self.add_predetermined(self.index_func(game, player_number=game.active_player))
game.make_move(self.predetermined[game.get_hash()][0])
def add_predetermined(self, more):
# a template that subclasses can override
# completely overwrites matching values in self.predetermined with new values from more
self.predetermined.update(more)
if __name__ == "__main__":
'''
This is an example of how to create and use an IndexPlayer
'''
from performance_testers import test_against
from monte_carlo_evaluation import unsure_monte_carlo_eval
from evaluation import Evaluation, simple_eval
from connect_four import ConnectFour
from tic_tac_toe import TicTacToe
from minimax import complete_minimax
from players import RandomPlayer, HumanPlayer
from basic_monte_carlo_player import BasicMonteCarloPlayer
def my_eval(game, player_number):
return unsure_monte_carlo_eval(game, player_number, simulation_amount=3)
def my_index(game, player_number):
ans = complete_minimax(game, 2, my_eval)
print([(index, ans[index][0], ans[index][1].value) for index in ans])
return ans
# With my_index = complete_minimax(game, 4, my_eval) and my_eval = unsure_monte_carlo_eval(game, player_number, simulation_amount=3)
# Score 0 - 2 - 0. It's bad and slow. Not a good combo.
# test_against((IndexPlayer(my_index), BasicMonteCarloPlayer(5, 2)), ConnectFour, comment=6)
test_against((IndexPlayer(my_index), BasicMonteCarloPlayer(5, 2)), ConnectFour, comment=6)