-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-tests.js
More file actions
225 lines (210 loc) · 6.64 KB
/
ai-tests.js
File metadata and controls
225 lines (210 loc) · 6.64 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// AI Test Suite for Connect-4
const AI_TESTS = [
// Immediate Win Detection
{
name: "AI takes horizontal win",
setup: [
{ player: 2, col: 2 }, { player: 1, col: 0 },
{ player: 2, col: 3 }, { player: 1, col: 0 },
{ player: 2, col: 4 }, { player: 1, col: 0 },
],
expectedMoves: [1, 5],
mustWin: true
},
{
name: "AI takes vertical win",
setup: [
{ player: 2, col: 3 }, { player: 1, col: 0 },
{ player: 2, col: 3 }, { player: 1, col: 0 },
{ player: 2, col: 3 }, { player: 1, col: 0 },
],
expectedMove: 3,
mustWin: true
},
// Immediate Block Detection
{
name: "AI blocks horizontal threat",
setup: [
{ player: 1, col: 2 }, { player: 2, col: 3 },
{ player: 1, col: 4 }, { player: 2, col: 3 },
{ player: 1, col: 5 }, { player: 2, col: 3 },
],
expectedMoves: [1, 6],
mustBlock: true
},
{
name: "AI blocks vertical threat",
setup: [
{ player: 1, col: 0 }, { player: 2, col: 3 },
{ player: 1, col: 0 }, { player: 2, col: 3 },
{ player: 1, col: 0 }, { player: 2, col: 4 },
],
expectedMove: 0,
mustBlock: true
},
// Edge/Corner Threat Detection (THE BUG FIX)
{
name: "AI responds to column 0 stacking",
setup: [
{ player: 1, col: 0 }, { player: 2, col: 3 },
{ player: 1, col: 0 },
],
forbiddenMoves: [3],
shouldConsider: [0, 1],
description: "AI was ignoring corner builds"
},
{
name: "AI responds to column 6 stacking",
setup: [
{ player: 1, col: 6 }, { player: 2, col: 3 },
{ player: 1, col: 6 },
],
forbiddenMoves: [3],
shouldConsider: [5, 6],
},
{
name: "AI blocks corner 3-stack on column 0",
setup: [
{ player: 1, col: 0 }, { player: 2, col: 3 },
{ player: 1, col: 0 }, { player: 2, col: 3 },
{ player: 1, col: 0 },
],
expectedMove: 0,
mustBlock: true
},
{
name: "AI blocks corner 3-stack on column 6",
setup: [
{ player: 1, col: 6 }, { player: 2, col: 3 },
{ player: 1, col: 6 }, { player: 2, col: 3 },
{ player: 1, col: 6 },
],
expectedMove: 6,
mustBlock: true
},
// Double Threat Handling
{
name: "AI creates double threat",
setup: [
{ player: 2, col: 2 }, { player: 1, col: 0 },
{ player: 2, col: 4 }, { player: 1, col: 0 },
],
expectedMove: 3,
shouldCreateDoubleThreat: true
},
{
name: "AI blocks opponent double threat setup",
setup: [
{ player: 1, col: 2 }, { player: 2, col: 0 },
{ player: 1, col: 4 }, { player: 2, col: 0 },
],
expectedMove: 3,
mustBlock: true
},
// Win vs Block Priority
{
name: "AI takes win over block",
setup: [
{ player: 2, col: 2 }, { player: 1, col: 5 },
{ player: 2, col: 3 }, { player: 1, col: 5 },
{ player: 2, col: 4 }, { player: 1, col: 5 },
],
expectedMoves: [1, 5],
mustWin: true,
description: "AI should take the win, not block"
},
// Strategic Opening
{
name: "AI prefers center on empty board",
setup: [],
expectedMove: 3,
},
{
name: "AI contests center",
setup: [{ player: 1, col: 3 }],
shouldConsider: [2, 3, 4],
},
// Center Column Vertical Threat Detection (Bug from screenshot)
{
name: "AI blocks column 4 (center-adjacent) vertical stack",
setup: [
{ player: 1, col: 4 }, { player: 2, col: 3 },
{ player: 1, col: 4 }, { player: 2, col: 3 },
{ player: 1, col: 4 },
],
expectedMove: 4,
mustBlock: true,
description: "Tests vertical threat detection on center-adjacent column"
},
{
name: "AI blocks column 3 (center) vertical stack",
setup: [
{ player: 1, col: 3 }, { player: 2, col: 2 },
{ player: 1, col: 3 }, { player: 2, col: 2 },
{ player: 1, col: 3 },
],
expectedMove: 3,
mustBlock: true,
description: "AI must block vertical threat on center column"
},
{
name: "AI blocks column 2 vertical stack",
setup: [
{ player: 1, col: 2 }, { player: 2, col: 3 },
{ player: 1, col: 2 }, { player: 2, col: 3 },
{ player: 1, col: 2 },
],
expectedMove: 2,
mustBlock: true,
description: "AI must block vertical threat on column 2"
},
];
// Test runner
function runAITests() {
console.log("=== AI Test Suite ===\n");
let passed = 0, failed = 0;
for (const test of AI_TESTS) {
// Reset and setup
currentGameState = new GameState();
transpositionTable.clear();
clearHeuristics();
for (const move of test.setup || []) {
currentGameState.makeMove(move.player, move.col);
}
// Get AI move
const origin = new GameState(currentGameState);
const aiMove = think(origin, 2, 10, true, -Infinity, Infinity);
// Validate
let pass = true;
let reason = "";
if (test.expectedMove !== undefined && aiMove !== test.expectedMove) {
pass = false;
reason = `Expected ${test.expectedMove}, got ${aiMove}`;
}
if (test.expectedMoves && !test.expectedMoves.includes(aiMove)) {
pass = false;
reason = `Expected one of ${test.expectedMoves}, got ${aiMove}`;
}
if (test.forbiddenMoves && test.forbiddenMoves.includes(aiMove)) {
pass = false;
reason = `Move ${aiMove} was forbidden`;
}
if (test.shouldConsider && !test.shouldConsider.includes(aiMove)) {
pass = false;
reason = `Expected to consider ${test.shouldConsider}, got ${aiMove}`;
}
if (pass) {
console.log(`✅ ${test.name}`);
passed++;
} else {
console.log(`❌ ${test.name}: ${reason}`);
failed++;
}
}
console.log(`\n=== ${passed}/${passed + failed} passed ===`);
return { passed, failed, total: passed + failed };
}
// Export for use
if (typeof module !== 'undefined') {
module.exports = { AI_TESTS, runAITests };
}