forked from Zulko/easyAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_unittests.py
181 lines (159 loc) · 7.54 KB
/
ai_unittests.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#######################################
#
# TESTING FOR AIs IN EASYAI
#
# To run tests, simply run this script.
#
#######################################
import unittest
import easyAI
import easyAI.games as examples
class Test_Negamax(unittest.TestCase):
def test_play_knights_against_self(self):
ai_algo_K1 = easyAI.Negamax(8)
ai_algo_K2 = easyAI.Negamax(10)
game = examples.Knights([easyAI.AI_Player(ai_algo_K1), easyAI.AI_Player(ai_algo_K2)])
move_list_K1 = []
move_list_K2 = []
while not game.is_over():
move = game.get_move()
if game.nplayer == 1:
move_list_K1.append(move)
else:
move_list_K2.append(move)
game.play_move(move)
K1_correct = ['B3', 'C5', 'D7', 'E5', 'F7', 'G5', 'H7', 'F6', 'G8', 'H6', 'G4', 'H2', 'F3', 'G1', 'H3', 'F2', 'E4', 'D6', 'C8', 'B6', 'C4', 'A3', 'B1', 'D2', 'F1', 'G3', 'H5']
K2_correct = ['G6', 'F8', 'E6', 'D8', 'C6', 'B8', 'A6', 'B4', 'C2', 'D4', 'E2', 'F4', 'G2', 'H4', 'F5', 'G7', 'E8', 'C7', 'D5', 'E3', 'D1', 'C3', 'A4', 'B2', 'D3', 'C1', 'A2']
self.assertEqual(move_list_K1, K1_correct)
self.assertEqual(move_list_K2, K2_correct)
def test_play_awele_against_self(self):
ai_algo_P1 = easyAI.Negamax(3)
ai_algo_P2 = easyAI.Negamax(4)
game = examples.AweleTactical([easyAI.AI_Player(ai_algo_P1), easyAI.AI_Player(ai_algo_P2)])
move_list_P1 = []
move_list_P2 = []
while not game.is_over():
move = game.get_move()
if game.nplayer == 1:
move_list_P1.append(move)
else:
move_list_P2.append(move)
game.play_move(move)
P1_correct = ['c', 'e', 'f', 'f', 'a', 'c', 'e', 'f', 'd', 'b', 'c', 'a', 'f', 'd', 'b', 'e']
P2_correct = ['i', 'j', 'l', 'h', 'g', 'k', 'j', 'i', 'l', 'h', 'j', 'l', 'k', 'l', 'i', 'g']
self.assertEqual(move_list_P1, P1_correct)
self.assertEqual(move_list_P2, P2_correct)
class Test_NonRecursiveNegamax(unittest.TestCase):
def test_play_knights_against_self(self):
ai_algo_K1 = easyAI.NonRecursiveNegamax(8)
ai_algo_K2 = easyAI.NonRecursiveNegamax(10)
game = examples.Knights([easyAI.AI_Player(ai_algo_K1), easyAI.AI_Player(ai_algo_K2)])
move_list_K1 = []
move_list_K2 = []
while not game.is_over():
move = game.get_move()
if game.nplayer == 1:
move_list_K1.append(move)
else:
move_list_K2.append(move)
game.play_move(move)
K1_correct = ['B3', 'C5', 'D7', 'E5', 'F7', 'G5', 'H7', 'F6', 'G8', 'H6', 'G4', 'H2', 'F3', 'G1', 'H3', 'F2', 'E4', 'D6', 'C8', 'B6', 'C4', 'A3', 'B1', 'D2', 'F1', 'G3', 'H5']
K2_correct = ['G6', 'F8', 'E6', 'D8', 'C6', 'B8', 'A6', 'B4', 'C2', 'D4', 'E2', 'F4', 'G2', 'H4', 'F5', 'G7', 'E8', 'C7', 'D5', 'E3', 'D1', 'C3', 'A4', 'B2', 'D3', 'C1', 'A2']
self.assertEqual(move_list_K1, K1_correct)
self.assertEqual(move_list_K2, K2_correct)
def test_play_awele_against_self(self):
ai_algo_P1 = easyAI.NonRecursiveNegamax(3)
ai_algo_P2 = easyAI.NonRecursiveNegamax(4)
game = examples.AweleTactical([easyAI.AI_Player(ai_algo_P1), easyAI.AI_Player(ai_algo_P2)])
move_list_P1 = []
move_list_P2 = []
while not game.is_over():
move = game.get_move()
if game.nplayer == 1:
move_list_P1.append(move)
else:
move_list_P2.append(move)
game.play_move(move)
P1_correct = ['c', 'e', 'f', 'f', 'a', 'c', 'e', 'f', 'd', 'b', 'c', 'a', 'f', 'd', 'b', 'e']
P2_correct = ['i', 'j', 'l', 'h', 'g', 'k', 'j', 'i', 'l', 'h', 'j', 'l', 'k', 'l', 'i', 'g']
self.assertEqual(move_list_P1, P1_correct)
self.assertEqual(move_list_P2, P2_correct)
class Test_SSS(unittest.TestCase):
def test_play_knights_against_self(self):
ai_algo_K1 = easyAI.SSS(8)
ai_algo_K2 = easyAI.SSS(10)
game = examples.Knights([easyAI.AI_Player(ai_algo_K1), easyAI.AI_Player(ai_algo_K2)])
move_list_K1 = []
move_list_K2 = []
while not game.is_over():
move = game.get_move()
if game.nplayer == 1:
move_list_K1.append(move)
else:
move_list_K2.append(move)
game.play_move(move)
K1_correct = ['B3', 'C5', 'D7', 'E5', 'F7', 'G5', 'H7', 'F6', 'G8', 'H6', 'G4', 'H2', 'F3', 'G1', 'H3', 'F2', 'E4', 'D6', 'C8', 'B6', 'C4', 'A3', 'B1', 'D2', 'F1', 'G3', 'H5']
K2_correct = ['G6', 'F8', 'E6', 'D8', 'C6', 'B8', 'A6', 'B4', 'C2', 'D4', 'E2', 'F4', 'G2', 'H4', 'F5', 'G7', 'E8', 'C7', 'D5', 'E3', 'D1', 'C3', 'A4', 'B2', 'D3', 'C1', 'A2']
self.assertEqual(move_list_K1, K1_correct)
self.assertEqual(move_list_K2, K2_correct)
class Test_DUAL(unittest.TestCase):
def test_play_knights_against_self(self):
ai_algo_K1 = easyAI.DUAL(8)
ai_algo_K2 = easyAI.DUAL(10)
game = examples.Knights([easyAI.AI_Player(ai_algo_K1), easyAI.AI_Player(ai_algo_K2)])
move_list_K1 = []
move_list_K2 = []
while not game.is_over():
move = game.get_move()
if game.nplayer == 1:
move_list_K1.append(move)
else:
move_list_K2.append(move)
game.play_move(move)
K1_correct = ['B3', 'C5', 'D7', 'E5', 'F7', 'G5', 'H7', 'F6', 'G8', 'H6', 'G4', 'H2', 'F3', 'G1', 'H3', 'F2', 'E4', 'D6', 'C8', 'B6', 'C4', 'A3', 'B1', 'D2', 'F1', 'G3', 'H5']
K2_correct = ['G6', 'F8', 'E6', 'D8', 'C6', 'B8', 'A6', 'B4', 'C2', 'D4', 'E2', 'F4', 'G2', 'H4', 'F5', 'G7', 'E8', 'C7', 'D5', 'E3', 'D1', 'C3', 'A4', 'B2', 'D3', 'C1', 'A2']
self.assertEqual(move_list_K1, K1_correct)
self.assertEqual(move_list_K2, K2_correct)
class Test_TT(unittest.TestCase):
def test_pickle_save_and_restore(self):
# 1. solve game/save TT
tt = easyAI.TT()
winner, depth, best_player_move = easyAI.id_solve(
examples.Nim,
range(13, 16),
tt=tt,
win_score=80,
verbose=False
)
tt.tofile("tt-data.pickle.temp")
# 2. restore TT from file
restored_tt = easyAI.TT()
restored_tt.fromfile("tt-data.pickle.temp")
# 3. get first AI move using the TT
game = examples.Nim([easyAI.Human_Player(), easyAI.AI_Player(restored_tt)])
game.play_move(best_player_move) # let the human play
ai_move = game.get_move() # get the AI's move based on tt
self.assertEqual(ai_move, "2,5")
self.assertEqual(best_player_move, "1,5")
def test_json_save_and_restore(self):
# 1. solve game/save TT
tt = easyAI.TT()
winner, depth, best_player_move = easyAI.id_solve(
examples.Nim,
range(13, 16),
tt=tt,
win_score=80,
verbose=False
)
tt.to_json_file("tt-data.json.temp", use_tuples=True)
# 2. restore TT from file
restored_tt = easyAI.TT()
restored_tt.from_json_file("tt-data.json.temp", use_tuples=True)
# 3. get first AI move using the TT
game = examples.Nim([easyAI.Human_Player(), easyAI.AI_Player(restored_tt)])
game.play_move(best_player_move) # let the human play
ai_move = game.get_move() # get the AI's move based on tt
self.assertEqual(ai_move, "2,5")
self.assertEqual(best_player_move, "1,5")
if __name__ == "__main__":
unittest.main(exit=False)