Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions tests/ai.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,64 @@ describe('DotDot AI Logic', () => {
expect(isDangerousH).toBe(false);
expect(isDangerousV).toBe(false);
});

test('AI takes multiple squares if possible (Chain Reaction)', () => {
// Setup: Two squares side-by-side that can be chained.
// Sq(0,0) needs H(1,0).
// Sq(0,1) needs H(1,1).

game.horizontalLines[0][0] = true;
game.verticalLines[0][0] = true;
game.verticalLines[0][1] = true;

game.horizontalLines[0][1] = true;
game.verticalLines[0][2] = true;

// If we place H(1,0), we complete Sq(0,0).
// Does AI see both? Yes, `findScoringMoves` returns all.

const move = ai.getMove(game);
expect(ai.doesMoveCompleteSquare(game, move)).toBe(true);
});

test('Full Game Simulation (AI vs Random)', () => {
// Ensure AI doesn't crash or freeze throughout a whole game.
const simGame = new GameState(5, 5); // 5x5 grid
let moves = 0;
const maxMoves = 1000; // Safety break

while (!simGame.gameOver && moves < maxMoves) {
moves++;
const player = simGame.getCurrentPlayer();
let move;

if (player === 'P1') {
// Random player logic for test
const available = ai.getAllAvailableMoves(simGame);
// Simple random move if available
if (available.length > 0) {
move = available[Math.floor(Math.random() * available.length)];
} else {
break; // Game over or error
}
} else {
// AI
move = ai.getMove(simGame);
}

if (!move) break;

const result = simGame.placeLine(move.type, move.r, move.c);
if (!result.success) {
// This might happen if AI returns a move that is already taken?
// Should not happen with valid AI.
throw new Error(`Invalid move generated by ${player}: ${JSON.stringify(move)}`);
}
}

expect(simGame.gameOver).toBe(true);
// Scores check?
const total = simGame.scores['P1'] + simGame.scores['P2'];
expect(total).toBe(16); // 4x4 squares = 16
});
});