-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_submission_tests.py
152 lines (129 loc) · 5.98 KB
/
player_submission_tests.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
import traceback
from isolation import Board, game_as_text
from test_players import RandomPlayer, HumanPlayer, Player
import platform
if platform.system() != 'Windows':
import resource
from time import time, sleep
def correctOpenEvalFn(yourOpenEvalFn):
print()
try:
sample_board = Board(RandomPlayer(), RandomPlayer())
# setting up the board as though we've been playing͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
board_state = [
[' ', ' ', ' ', 'X', ' ', ' ', ' ', ' ', ' '],
[' ', 'X', ' ', ' ', ' ', ' ', 'X', ' ', 'K2'],
[' ', ' ', 'X', 'X', ' ', ' ', 'X', ' ', 'X'],
['X', ' ', ' ', ' ', ' ', 'X', ' ', 'X', ' '],
[' ', ' ', 'K1', ' ', ' ', ' ', 'X', 'X', ' '],
[' ', ' ', ' ', ' ', 'X', ' ', 'X', 'X', 'X'],
[' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X', 'X'],
[' ', ' ', ' ', 'X', 'X', ' ', 'X', 'X', 'X'],
[' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X', 'X']
]
sample_board.set_state(board_state, True)
#test = sample_board.get_legal_moves()͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
h = yourOpenEvalFn()
print('OpenMoveEvalFn Test: This board has a score of %s.' % (h.score(sample_board, sample_board.get_active_player())))
except NotImplementedError:
print('OpenMoveEvalFn Test: Not implemented')
except:
print('OpenMoveEvalFn Test: ERROR OCCURRED')
print(traceback.format_exc())
print()
def beatRandom(yourAgent):
"""Example test you can run
to make sure your AI does better
than random."""
print("")
try:
r = RandomPlayer()
p = yourAgent()
game = Board(p, r, 9, 9)
output_b = game.copy()
winner, move_history, termination = game.play_isolation(time_limit=5000, print_moves=True)
print("\n", winner, " has won. Reason: ", termination)
# Uncomment to see game͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
# print game_as_text(winner, move_history, termination, output_b)͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
except NotImplementedError:
print('CustomPlayer Test: Not Implemented')
except:
print('CustomPlayer Test: ERROR OCCURRED')
print(traceback.format_exc())
print()
def minimaxTest(yourAgent, minimax_fn):
"""Example test to make sure
your minimax works, using the
OpenMoveEvalFunction evaluation function.
This can be used for debugging your code
with different model Board states.
Especially important to check alphabeta
pruning"""
# create dummy 9x9 board͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
print("Now running Minimax test 1.")
print()
try:
def time_left(): # For these testing purposes, let's ignore timeouts
return 10000
player = yourAgent() #using as a dummy player to create a board
sample_board = Board(player, RandomPlayer())
# setting up the board as though we've been playing͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
board_state = [
[' ', ' ', 'K1', 'X', 'X', 'X', ' ', ' ', ' '],
['X', ' ', ' ', 'X', ' ', 'X', ' ', 'X', ' '],
[' ', 'X', 'X', 'X', 'X', 'X', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', 'X', ' ', 'X', ' ', ' '],
['X', ' ', 'X', ' ', 'X', ' ', ' ', ' ', ' '],
[' ', ' ', 'X', ' ', ' ', 'X', 'K2', ' ', ' '],
[' ', 'X', ' ', 'X', ' ', ' ', ' ', ' ', ' '],
[' ', 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
]
sample_board.set_state(board_state, True)
test_pass = True
expected_depth_scores = [(1, -3), (2, -2), (3, -1), (4, -1), (5, 0)]
for depth, exp_score in expected_depth_scores:
move, score = minimax_fn(player, sample_board, time_left, depth=depth, my_turn=True)
if exp_score != score:
print("Minimax failed for depth: ", depth)
test_pass = False
else:
print("Minimax passed for depth: ", depth)
if test_pass:
print()
print("Now running Minimax test 2.")
print()
player = yourAgent()
sample_board = Board(RandomPlayer(),player)
# setting up the board as though we've been playing͏︅͏︀͏︋͏︋͏󠄌͏󠄎͏︀͏󠄑͏󠄂͏︍
board_state = [
[' ', ' ', 'X', 'X', ' ', ' ', ' ', ' ', ' '],
['X', ' ', ' ', ' ', ' ', 'X', ' ', 'X', ' '],
[' ', 'X', 'X', 'X', 'X', ' ', 'X', 'X', ' '],
[' ', ' ', 'X', ' ', ' ', 'X', 'X', ' ', 'X'],
[' ', ' ', 'X', 'X', 'X', 'X', 'K2', 'X', 'X'],
[' ', ' ', ' ', 'K1', 'X', 'X', 'X', 'X', ' '],
[' ', ' ', ' ', 'X', ' ', 'X', 'X', 'X', 'X'],
[' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X', 'X'],
[' ', ' ', ' ', ' ', 'X', ' ', 'X', 'X', ' ']
]
sample_board.set_state(board_state, p1_turn=True)
test_pass = True
expected_depth_scores = [(1, -1), (2, 1), (3, 0), (4, -2), (5, -3)]
for depth, exp_score in expected_depth_scores:
move, score = minimax_fn(player, sample_board, time_left, depth=depth, my_turn=False)
if exp_score != score:
print("Minimax failed for depth: ", depth)
test_pass = False
else:
print("Minimax passed for depth: ", depth)
if test_pass:
print("Minimax Test: Runs Successfully!")
else:
print("Minimax Test: Failed")
except NotImplementedError:
print('Minimax Test: Not implemented')
except:
print('Minimax Test: ERROR OCCURRED')
print(traceback.format_exc())