Add objectives, stage config, and GameEngine session core

Sealed Objective types (clearGems/reachScore/clearLines) with JSON
round-trip; StageConfig with preset cells and star thresholds;
GameEngine orchestrating placement -> clear -> scoring -> objectives
with stuck detection, one-shot rescue (continue / +5 moves), and
deterministic per-attempt RNG. 100-game headless stress test and
pure-Dart architecture guard. 76 tests green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 13:11:31 +09:00
parent 0210c14858
commit 62cbb4b16a
10 changed files with 886 additions and 1 deletions
+66
View File
@@ -0,0 +1,66 @@
import 'package:block_seasons/game/engine/game_event.dart';
import 'package:block_seasons/game/models/objective.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('ClearGems', () {
test('accumulates gems from clear events', () {
var obj = Objective.clearGems(6);
obj = obj.onEvent(const LinesCleared(lines: 1, gems: 2));
obj = obj.onEvent(const LinesCleared(lines: 2, gems: 3));
expect(obj.current, 5);
expect(obj.isComplete, isFalse);
obj = obj.onEvent(const LinesCleared(lines: 1, gems: 1));
expect(obj.isComplete, isTrue);
});
test('ignores unrelated events', () {
var obj = Objective.clearGems(3);
obj = obj.onEvent(const ScoreChanged(total: 9999));
expect(obj.current, 0);
});
});
group('ReachScore', () {
test('tracks the running total score', () {
var obj = Objective.reachScore(500);
obj = obj.onEvent(const ScoreChanged(total: 320));
expect(obj.current, 320);
expect(obj.isComplete, isFalse);
obj = obj.onEvent(const ScoreChanged(total: 510));
expect(obj.isComplete, isTrue);
});
});
group('ClearLines', () {
test('accumulates cleared lines', () {
var obj = Objective.clearLines(4);
obj = obj.onEvent(const LinesCleared(lines: 3, gems: 0));
expect(obj.current, 3);
obj = obj.onEvent(const LinesCleared(lines: 1, gems: 0));
expect(obj.isComplete, isTrue);
});
});
group('JSON', () {
test('round-trips all objective types', () {
for (final json in [
{'type': 'clearGems', 'count': 6},
{'type': 'reachScore', 'target': 1200},
{'type': 'clearLines', 'count': 10},
]) {
final obj = Objective.fromJson(json);
expect(obj.toJson(), json);
expect(obj.current, 0);
expect(obj.target, greaterThan(0));
}
});
test('throws on unknown type', () {
expect(
() => Objective.fromJson({'type': 'collectStars', 'count': 1}),
throwsFormatException,
);
});
});
}
+62
View File
@@ -0,0 +1,62 @@
import 'package:block_seasons/game/models/cell.dart';
import 'package:block_seasons/game/models/objective.dart';
import 'package:block_seasons/game/models/stage.dart';
import 'package:flutter_test/flutter_test.dart';
const sampleJson = {
'id': 's001_007',
'seed': 8841273,
'moveLimit': 22,
'preset': [
{'x': 3, 'y': 4, 't': 'gem'},
{'x': 0, 'y': 7, 't': 'filled', 'c': 2},
],
'objectives': [
{'type': 'clearGems', 'count': 6},
],
'stars': {
'two': {'movesLeft': 4},
'three': {'movesLeft': 8},
},
'generatorProfile': 'mid',
};
void main() {
group('StageConfig', () {
test('parses the season pack stage schema', () {
final stage = StageConfig.fromJson(sampleJson);
expect(stage.id, 's001_007');
expect(stage.seed, 8841273);
expect(stage.moveLimit, 22);
expect(stage.preset, hasLength(2));
expect(stage.preset[0].type, CellType.gem);
expect(stage.preset[1].colorId, 2);
expect(stage.objectives.single, isA<Objective>());
expect(stage.generatorProfile, 'mid');
});
test('round-trips to JSON', () {
final stage = StageConfig.fromJson(sampleJson);
expect(stage.toJson(), sampleJson);
});
test('star thresholds map moves left to stars', () {
final stage = StageConfig.fromJson(sampleJson);
expect(stage.stars.starsFor(movesLeft: 0), 1);
expect(stage.stars.starsFor(movesLeft: 3), 1);
expect(stage.stars.starsFor(movesLeft: 4), 2);
expect(stage.stars.starsFor(movesLeft: 7), 2);
expect(stage.stars.starsFor(movesLeft: 8), 3);
expect(stage.stars.starsFor(movesLeft: 20), 3);
});
test('builds the initial grid from preset cells', () {
final stage = StageConfig.fromJson(sampleJson);
final grid = stage.initialGrid();
expect(grid.cellAt(3, 4).type, CellType.gem);
expect(grid.cellAt(0, 7).type, CellType.filled);
expect(grid.cellAt(0, 7).colorId, 2);
expect(grid.occupiedCount, 2);
});
});
}