init
This commit is contained in:
commit
a986cfea41
1716 files changed
+1004440
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Board, ROWS, COLS } from '../../src/core/board.js';
|
||||
import { SeededRNG } from '../../src/utils/random.js';
|
||||
|
||||
describe('Board', () => {
|
||||
it('should generate a board with correct dimensions', () => {
|
||||
const board = Board.generate(new SeededRNG(42));
|
||||
expect(board.grid.length).toBe(ROWS);
|
||||
expect(board.grid[0].length).toBe(COLS);
|
||||
});
|
||||
|
||||
it('should have all values between 1 and 9', () => {
|
||||
const board = Board.generate(new SeededRNG(42));
|
||||
for (let r = 0; r < ROWS; r++) {
|
||||
for (let c = 0; c < COLS; c++) {
|
||||
expect(board.get(r, c)).toBeGreaterThanOrEqual(1);
|
||||
expect(board.get(r, c)).toBeLessThanOrEqual(9);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should clone independently', () => {
|
||||
const board = Board.generate(new SeededRNG(42));
|
||||
const clone = board.clone();
|
||||
clone.set(0, 0, 0);
|
||||
expect(board.get(0, 0)).not.toBe(0);
|
||||
});
|
||||
|
||||
it('should count non-empty cells correctly', () => {
|
||||
const board = Board.generate(new SeededRNG(42));
|
||||
expect(board.countNonEmpty()).toBe(ROWS * COLS);
|
||||
board.set(0, 0, 0);
|
||||
expect(board.countNonEmpty()).toBe(ROWS * COLS - 1);
|
||||
});
|
||||
|
||||
it('should produce deterministic boards from same seed', () => {
|
||||
const a = Board.generate(new SeededRNG(123));
|
||||
const b = Board.generate(new SeededRNG(123));
|
||||
expect(a.grid).toEqual(b.grid);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Board } from '../../src/core/board.js';
|
||||
import { playGame } from '../../src/core/game.js';
|
||||
import { SeededRNG } from '../../src/utils/random.js';
|
||||
import { RandomStrategy } from '../../src/strategies/random.js';
|
||||
import { GreedyLargestStrategy } from '../../src/strategies/greedy-largest.js';
|
||||
|
||||
describe('playGame', () => {
|
||||
it('should complete without errors using random strategy', () => {
|
||||
const board = Board.generate(new SeededRNG(42));
|
||||
const strategy = new RandomStrategy(new SeededRNG(42));
|
||||
const result = playGame(board, strategy);
|
||||
|
||||
expect(result.score).toBeGreaterThan(0);
|
||||
expect(result.score + result.remaining).toBe(result.totalApples);
|
||||
expect(result.moveCount).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should complete without errors using greedy strategy', () => {
|
||||
const board = Board.generate(new SeededRNG(42));
|
||||
const result = playGame(board, new GreedyLargestStrategy());
|
||||
|
||||
expect(result.score).toBeGreaterThan(0);
|
||||
expect(result.score + result.remaining).toBe(result.totalApples);
|
||||
});
|
||||
|
||||
it('should produce deterministic results with same seed', () => {
|
||||
const r1 = playGame(Board.generate(new SeededRNG(99)), new RandomStrategy(new SeededRNG(99)));
|
||||
const r2 = playGame(Board.generate(new SeededRNG(99)), new RandomStrategy(new SeededRNG(99)));
|
||||
expect(r1.score).toBe(r2.score);
|
||||
expect(r1.moveCount).toBe(r2.moveCount);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Board } from '../../src/core/board.js';
|
||||
import { applyGravity } from '../../src/core/gravity.js';
|
||||
|
||||
describe('applyGravity', () => {
|
||||
it('should move values down to fill empty cells', () => {
|
||||
const grid = Array.from({ length: 10 }, () => new Array(17).fill(0));
|
||||
grid[0][0] = 5; // top cell has value
|
||||
// middle and bottom are empty
|
||||
const board = new Board(grid);
|
||||
|
||||
applyGravity(board);
|
||||
|
||||
// value should fall to bottom
|
||||
expect(board.get(9, 0)).toBe(5);
|
||||
expect(board.get(0, 0)).toBe(0);
|
||||
});
|
||||
|
||||
it('should preserve order when multiple values fall', () => {
|
||||
const grid = Array.from({ length: 10 }, () => new Array(17).fill(0));
|
||||
grid[0][0] = 3;
|
||||
grid[2][0] = 7;
|
||||
grid[5][0] = 1;
|
||||
const board = new Board(grid);
|
||||
|
||||
applyGravity(board);
|
||||
|
||||
expect(board.get(9, 0)).toBe(1);
|
||||
expect(board.get(8, 0)).toBe(7);
|
||||
expect(board.get(7, 0)).toBe(3);
|
||||
});
|
||||
|
||||
it('should not change a fully packed board', () => {
|
||||
const grid = Array.from({ length: 10 }, () => new Array(17).fill(5));
|
||||
const board = new Board(grid);
|
||||
const before = board.grid.map(r => [...r]);
|
||||
|
||||
applyGravity(board);
|
||||
|
||||
expect(board.grid).toEqual(before);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Board } from '../../src/core/board.js';
|
||||
import { findAllValidMoves } from '../../src/core/moves.js';
|
||||
|
||||
describe('findAllValidMoves', () => {
|
||||
it('should find single-cell moves where value is 10 (none, since max is 9)', () => {
|
||||
const board = new Board([[1, 2, 3], [4, 5, 6], [7, 8, 9]].map(r => [...r, ...new Array(14).fill(0)]).concat(new Array(7).map(() => new Array(17).fill(0))));
|
||||
// This board has values only in top-left 3x3
|
||||
// No single cell equals 10
|
||||
});
|
||||
|
||||
it('should find valid rectangles summing to 10', () => {
|
||||
// Create a small test: 1 2 3 4 in first row, rest 0
|
||||
const grid = Array.from({ length: 10 }, () => new Array(17).fill(0));
|
||||
grid[0][0] = 1;
|
||||
grid[0][1] = 2;
|
||||
grid[0][2] = 3;
|
||||
grid[0][3] = 4;
|
||||
const board = new Board(grid);
|
||||
|
||||
const moves = findAllValidMoves(board);
|
||||
// 1+2+3+4 = 10, so the rectangle (0,0)-(0,3) should be found
|
||||
const hasFullRow = moves.some(m => m.r1 === 0 && m.c1 === 0 && m.r2 === 0 && m.c2 === 3);
|
||||
expect(hasFullRow).toBe(true);
|
||||
|
||||
// 2+3+4 = 9, not valid; 3+4 = 7, not valid; etc.
|
||||
// But 1+2+3+4 = 10 ✓
|
||||
// Also check that no invalid moves are included
|
||||
for (const m of moves) {
|
||||
let sum = 0;
|
||||
for (let r = m.r1; r <= m.r2; r++) {
|
||||
for (let c = m.c1; c <= m.c2; c++) {
|
||||
sum += board.get(r, c);
|
||||
}
|
||||
}
|
||||
expect(sum).toBe(10);
|
||||
}
|
||||
});
|
||||
|
||||
it('should find no moves on empty board', () => {
|
||||
const grid = Array.from({ length: 10 }, () => new Array(17).fill(0));
|
||||
const board = new Board(grid);
|
||||
expect(findAllValidMoves(board)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user