Add pure-Dart engine core: RNG, grid, placement, line clear, scoring, piece generator
PCG32 seeded RNG; immutable 8x8 GridState with occupancy bitmask; placement legality + anyPlacementExists; simultaneous row/col clears with single-count gem credit; combo scoring with one-move grace; weighted-bag generator with pity bias and depth-3 solvability nudge. All TDD, 51 tests green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import 'package:block_seasons/game/engine/line_clear.dart';
|
||||
import 'package:block_seasons/game/models/cell.dart';
|
||||
import 'package:block_seasons/game/models/grid.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
GridState _fillRow(GridState grid, int y, {Set<int>? skip}) {
|
||||
for (var x = 0; x < GridState.size; x++) {
|
||||
if (skip?.contains(x) ?? false) continue;
|
||||
grid = grid.withCell(x, y, const Cell(CellType.filled, colorId: 0));
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
GridState _fillCol(GridState grid, int x, {Set<int>? skip}) {
|
||||
for (var y = 0; y < GridState.size; y++) {
|
||||
if (skip?.contains(y) ?? false) continue;
|
||||
grid = grid.withCell(x, y, const Cell(CellType.filled, colorId: 0));
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('detectAndClear', () {
|
||||
test('no full lines leaves the grid untouched', () {
|
||||
final grid = _fillRow(GridState.empty(), 3, skip: {5});
|
||||
final result = detectAndClear(grid);
|
||||
expect(result.clearedRows, isEmpty);
|
||||
expect(result.clearedCols, isEmpty);
|
||||
expect(result.linesCleared, 0);
|
||||
expect(result.gemsCleared, 0);
|
||||
expect(result.grid.occupiedCount, grid.occupiedCount);
|
||||
});
|
||||
|
||||
test('clears a single full row', () {
|
||||
final grid = _fillRow(GridState.empty(), 2);
|
||||
final result = detectAndClear(grid);
|
||||
expect(result.clearedRows, [2]);
|
||||
expect(result.clearedCols, isEmpty);
|
||||
expect(result.linesCleared, 1);
|
||||
expect(result.grid.occupiedCount, 0);
|
||||
});
|
||||
|
||||
test('clears a single full column', () {
|
||||
final grid = _fillCol(GridState.empty(), 6);
|
||||
final result = detectAndClear(grid);
|
||||
expect(result.clearedRows, isEmpty);
|
||||
expect(result.clearedCols, [6]);
|
||||
expect(result.grid.occupiedCount, 0);
|
||||
});
|
||||
|
||||
test('clears two rows at once', () {
|
||||
var grid = _fillRow(GridState.empty(), 0);
|
||||
grid = _fillRow(grid, 7);
|
||||
final result = detectAndClear(grid);
|
||||
expect(result.clearedRows, [0, 7]);
|
||||
expect(result.linesCleared, 2);
|
||||
expect(result.grid.occupiedCount, 0);
|
||||
});
|
||||
|
||||
test('row and column sharing a corner clear together', () {
|
||||
var grid = _fillRow(GridState.empty(), 4);
|
||||
grid = _fillCol(grid, 4);
|
||||
final result = detectAndClear(grid);
|
||||
expect(result.clearedRows, [4]);
|
||||
expect(result.clearedCols, [4]);
|
||||
expect(result.linesCleared, 2);
|
||||
// 8 + 8 - 1 shared cell were occupied; all gone now.
|
||||
expect(result.grid.occupiedCount, 0);
|
||||
});
|
||||
|
||||
test('counts gems in cleared lines, intersection gem only once', () {
|
||||
var grid = GridState.empty();
|
||||
grid = grid.withCell(4, 4, const Cell(CellType.gem));
|
||||
grid = grid.withCell(0, 4, const Cell(CellType.gem));
|
||||
grid = grid.withCell(4, 0, const Cell(CellType.gem));
|
||||
grid = _fillRow(grid, 4, skip: {0, 4});
|
||||
grid = _fillCol(grid, 4, skip: {0, 4});
|
||||
final result = detectAndClear(grid);
|
||||
expect(result.clearedRows, [4]);
|
||||
expect(result.clearedCols, [4]);
|
||||
// Gems at (0,4), (4,0), and the shared (4,4) -> exactly 3.
|
||||
expect(result.gemsCleared, 3);
|
||||
});
|
||||
|
||||
test('cells outside cleared lines survive', () {
|
||||
var grid = _fillRow(GridState.empty(), 1);
|
||||
grid = grid.withCell(3, 5, const Cell(CellType.filled, colorId: 2));
|
||||
final result = detectAndClear(grid);
|
||||
expect(result.grid.occupiedCount, 1);
|
||||
expect(result.grid.cellAt(3, 5).colorId, 2);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import 'package:block_seasons/core/rng.dart';
|
||||
import 'package:block_seasons/game/engine/piece_generator.dart';
|
||||
import 'package:block_seasons/game/models/cell.dart';
|
||||
import 'package:block_seasons/game/models/grid.dart';
|
||||
import 'package:block_seasons/game/models/piece_library.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
/// Checkerboard: no line can ever be completed, big shapes never fit.
|
||||
GridState _checkerboard() {
|
||||
var grid = GridState.empty();
|
||||
for (var y = 0; y < GridState.size; y++) {
|
||||
for (var x = 0; x < GridState.size; x++) {
|
||||
if ((x + y).isEven) {
|
||||
grid = grid.withCell(x, y, const Cell(CellType.filled, colorId: 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
/// Rows 1..7 filled except column 0; row 0 filled except (0,0).
|
||||
/// Only column 0 is free -> tight but recoverable via line clears.
|
||||
GridState _tightBoard() {
|
||||
var grid = GridState.empty();
|
||||
for (var y = 0; y < GridState.size; y++) {
|
||||
for (var x = 1; x < GridState.size; x++) {
|
||||
grid = grid.withCell(x, y, const Cell(CellType.filled, colorId: 0));
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('isTrayPlayable', () {
|
||||
test('any tray plays out on an empty grid', () {
|
||||
final tray = [
|
||||
PieceLibrary.byId('square3'),
|
||||
PieceLibrary.byId('line5_h'),
|
||||
PieceLibrary.byId('rect3x2'),
|
||||
];
|
||||
expect(isTrayPlayable(GridState.empty(), tray), isTrue);
|
||||
});
|
||||
|
||||
test('recognizes trays that need intermediate line clears', () {
|
||||
// Column 0 free (8 cells); tray sums to 9 cells, so it only plays out
|
||||
// if placements trigger clears that open space.
|
||||
final tray = [
|
||||
PieceLibrary.byId('line4_v'),
|
||||
PieceLibrary.byId('line4_v'),
|
||||
PieceLibrary.byId('mono'),
|
||||
];
|
||||
expect(isTrayPlayable(_tightBoard(), tray), isTrue);
|
||||
});
|
||||
|
||||
test('returns false when no ordering can play all pieces', () {
|
||||
final tray = [
|
||||
PieceLibrary.byId('square3'),
|
||||
PieceLibrary.byId('square3'),
|
||||
PieceLibrary.byId('line5_h'),
|
||||
];
|
||||
expect(isTrayPlayable(_checkerboard(), tray), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('PieceGenerator', () {
|
||||
test('same seed produces identical tray sequences', () {
|
||||
final a = PieceGenerator(SeededRng(42));
|
||||
final b = PieceGenerator(SeededRng(42));
|
||||
final grid = GridState.empty();
|
||||
for (var i = 0; i < 100; i++) {
|
||||
final trayA = a.nextTray(grid).map((p) => p.id).toList();
|
||||
final trayB = b.nextTray(grid).map((p) => p.id).toList();
|
||||
expect(trayA, trayB);
|
||||
}
|
||||
});
|
||||
|
||||
test('trays always contain exactly 3 pieces with distinct ids', () {
|
||||
final gen = PieceGenerator(SeededRng(7));
|
||||
final grids = [GridState.empty(), _checkerboard(), _tightBoard()];
|
||||
for (final grid in grids) {
|
||||
for (var i = 0; i < 200; i++) {
|
||||
final tray = gen.nextTray(grid);
|
||||
expect(tray.length, 3);
|
||||
expect(tray.map((p) => p.id).toSet().length, 3);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('pity bias: tighter boards get smaller pieces on average', () {
|
||||
final emptyGen = PieceGenerator(SeededRng(123));
|
||||
final tightGen = PieceGenerator(SeededRng(123));
|
||||
final empty = GridState.empty();
|
||||
final tight = _checkerboard(); // 50% fill, no clears possible
|
||||
|
||||
double meanSize(PieceGenerator gen, GridState grid) {
|
||||
var total = 0;
|
||||
for (var i = 0; i < 400; i++) {
|
||||
for (final p in gen.nextTray(grid)) {
|
||||
total += p.size;
|
||||
}
|
||||
}
|
||||
return total / (400 * 3);
|
||||
}
|
||||
|
||||
final emptyMean = meanSize(emptyGen, empty);
|
||||
final tightMean = meanSize(tightGen, tight);
|
||||
expect(tightMean, lessThan(emptyMean));
|
||||
});
|
||||
|
||||
test('nudge: tight-but-recoverable boards always get playable trays',
|
||||
() {
|
||||
final gen = PieceGenerator(SeededRng(2026));
|
||||
final grid = _tightBoard();
|
||||
for (var i = 0; i < 100; i++) {
|
||||
final tray = gen.nextTray(grid);
|
||||
expect(isTrayPlayable(grid, tray), isTrue,
|
||||
reason: 'deal #$i: ${tray.map((p) => p.id).join(", ")}');
|
||||
}
|
||||
});
|
||||
|
||||
test('on a dead board the tray still contains the smallest pieces', () {
|
||||
// Fully occupied: nothing fits; engine handles game over, but the
|
||||
// generator must not loop forever and should fall back to small pieces.
|
||||
var grid = GridState.empty();
|
||||
for (var y = 0; y < GridState.size; y++) {
|
||||
for (var x = 0; x < GridState.size; x++) {
|
||||
grid = grid.withCell(x, y, const Cell(CellType.filled, colorId: 0));
|
||||
}
|
||||
}
|
||||
final tray = PieceGenerator(SeededRng(5)).nextTray(grid);
|
||||
expect(tray.length, 3);
|
||||
expect(tray.every((p) => p.size <= 2), isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'package:block_seasons/game/engine/placement.dart';
|
||||
import 'package:block_seasons/game/models/cell.dart';
|
||||
import 'package:block_seasons/game/models/grid.dart';
|
||||
import 'package:block_seasons/game/models/piece.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
const mono = Piece(id: 'mono', colorId: 0, offsets: [(0, 0)]);
|
||||
const dominoH = Piece(id: 'domino_h', colorId: 1, offsets: [(0, 0), (1, 0)]);
|
||||
const square2 = Piece(
|
||||
id: 'square2',
|
||||
colorId: 2,
|
||||
offsets: [(0, 0), (1, 0), (0, 1), (1, 1)],
|
||||
);
|
||||
|
||||
void main() {
|
||||
group('canPlace', () {
|
||||
test('any piece fits anywhere legal on an empty grid', () {
|
||||
final grid = GridState.empty();
|
||||
expect(canPlace(grid, mono, 0, 0), isTrue);
|
||||
expect(canPlace(grid, mono, 7, 7), isTrue);
|
||||
expect(canPlace(grid, square2, 6, 6), isTrue);
|
||||
});
|
||||
|
||||
test('rejects out-of-bounds placements', () {
|
||||
final grid = GridState.empty();
|
||||
expect(canPlace(grid, square2, 7, 7), isFalse);
|
||||
expect(canPlace(grid, dominoH, 7, 0), isFalse);
|
||||
expect(canPlace(grid, mono, -1, 0), isFalse);
|
||||
expect(canPlace(grid, mono, 0, 8), isFalse);
|
||||
});
|
||||
|
||||
test('rejects overlap with occupied cells', () {
|
||||
final grid = GridState.empty()
|
||||
.withCell(1, 0, const Cell(CellType.filled, colorId: 0));
|
||||
expect(canPlace(grid, dominoH, 0, 0), isFalse);
|
||||
expect(canPlace(grid, dominoH, 2, 0), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('place', () {
|
||||
test('fills cells with the piece color and keeps original grid', () {
|
||||
final grid = GridState.empty();
|
||||
final next = place(grid, square2, 3, 3);
|
||||
|
||||
expect(grid.occupiedCount, 0);
|
||||
expect(next.occupiedCount, 4);
|
||||
for (final (dx, dy) in square2.offsets) {
|
||||
expect(next.cellAt(3 + dx, 3 + dy).type, CellType.filled);
|
||||
expect(next.cellAt(3 + dx, 3 + dy).colorId, square2.colorId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('anyPlacementExists', () {
|
||||
test('true on an empty grid', () {
|
||||
expect(anyPlacementExists(GridState.empty(), [square2]), isTrue);
|
||||
});
|
||||
|
||||
test('detects when only a small piece fits', () {
|
||||
// Fill everything except (7, 7).
|
||||
var grid = GridState.empty();
|
||||
for (var y = 0; y < GridState.size; y++) {
|
||||
for (var x = 0; x < GridState.size; x++) {
|
||||
if (x == 7 && y == 7) continue;
|
||||
grid = grid.withCell(x, y, const Cell(CellType.filled, colorId: 0));
|
||||
}
|
||||
}
|
||||
expect(anyPlacementExists(grid, [mono]), isTrue);
|
||||
expect(anyPlacementExists(grid, [dominoH]), isFalse);
|
||||
expect(anyPlacementExists(grid, [dominoH, mono]), isTrue);
|
||||
});
|
||||
|
||||
test('false when the grid is completely full', () {
|
||||
var grid = GridState.empty();
|
||||
for (var y = 0; y < GridState.size; y++) {
|
||||
for (var x = 0; x < GridState.size; x++) {
|
||||
grid = grid.withCell(x, y, const Cell(CellType.filled, colorId: 0));
|
||||
}
|
||||
}
|
||||
expect(anyPlacementExists(grid, [mono]), isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import 'package:block_seasons/game/engine/scoring.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('lineClearBase', () {
|
||||
test('escalates with simultaneous lines', () {
|
||||
expect(lineClearBase(1), 100);
|
||||
expect(lineClearBase(2), 300);
|
||||
expect(lineClearBase(3), 600);
|
||||
expect(lineClearBase(4), 1000);
|
||||
});
|
||||
});
|
||||
|
||||
group('comboMultiplier', () {
|
||||
test('grows by 0.5 per streak step and caps at 8', () {
|
||||
expect(comboMultiplier(1), 1.5);
|
||||
expect(comboMultiplier(2), 2.0);
|
||||
expect(comboMultiplier(8), 5.0);
|
||||
expect(comboMultiplier(12), 5.0);
|
||||
});
|
||||
});
|
||||
|
||||
group('ComboState', () {
|
||||
test('clearing placements grow the streak', () {
|
||||
var combo = ComboState.initial;
|
||||
combo = combo.advance(cleared: true);
|
||||
expect(combo.streak, 1);
|
||||
combo = combo.advance(cleared: true);
|
||||
expect(combo.streak, 2);
|
||||
});
|
||||
|
||||
test('one dry move is grace, streak survives', () {
|
||||
var combo = ComboState.initial;
|
||||
combo = combo.advance(cleared: true);
|
||||
combo = combo.advance(cleared: false);
|
||||
expect(combo.streak, 1);
|
||||
combo = combo.advance(cleared: true);
|
||||
expect(combo.streak, 2);
|
||||
});
|
||||
|
||||
test('two consecutive dry moves reset the streak', () {
|
||||
var combo = ComboState.initial;
|
||||
combo = combo.advance(cleared: true);
|
||||
combo = combo.advance(cleared: false);
|
||||
combo = combo.advance(cleared: false);
|
||||
expect(combo.streak, 0);
|
||||
combo = combo.advance(cleared: true);
|
||||
expect(combo.streak, 1);
|
||||
});
|
||||
});
|
||||
|
||||
group('scorePlacement', () {
|
||||
test('placement without clear scores cell count only', () {
|
||||
final delta = scorePlacement(
|
||||
cellsPlaced: 4,
|
||||
linesCleared: 0,
|
||||
combo: ComboState.initial,
|
||||
);
|
||||
expect(delta.points, 4);
|
||||
expect(delta.combo.streak, 0);
|
||||
});
|
||||
|
||||
test('first clear applies x1.5 multiplier', () {
|
||||
final delta = scorePlacement(
|
||||
cellsPlaced: 5,
|
||||
linesCleared: 1,
|
||||
combo: ComboState.initial,
|
||||
);
|
||||
// 5 + round(100 * 1.5) = 155
|
||||
expect(delta.points, 155);
|
||||
expect(delta.combo.streak, 1);
|
||||
});
|
||||
|
||||
test('streak compounds across placements', () {
|
||||
var combo = ComboState.initial;
|
||||
final first = scorePlacement(
|
||||
cellsPlaced: 3,
|
||||
linesCleared: 1,
|
||||
combo: combo,
|
||||
);
|
||||
combo = first.combo;
|
||||
final second = scorePlacement(
|
||||
cellsPlaced: 3,
|
||||
linesCleared: 2,
|
||||
combo: combo,
|
||||
);
|
||||
// 3 + round(300 * 2.0) = 603
|
||||
expect(second.points, 603);
|
||||
expect(second.combo.streak, 2);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user