Make special mechanics essential in postcard and story puzzles
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:lumen/domain/puzzle.dart';
|
||||
import 'package:lumen/domain/thinking.dart';
|
||||
|
||||
void main() {
|
||||
for (final path in [
|
||||
'assets/campaign.json',
|
||||
'assets/stories/midnight-invitation/campaign.json',
|
||||
]) {
|
||||
final data =
|
||||
jsonDecode(File(path).readAsStringSync()) as Map<String, dynamic>;
|
||||
final levels = data['levels'] as List;
|
||||
for (final index in [4, 6, 8, 10]) {
|
||||
final level = levels[index] as Map<String, dynamic>;
|
||||
final solution = (level['picture'] as String)
|
||||
.split('')
|
||||
.map(int.parse)
|
||||
.toList();
|
||||
final rng = Random(8721 + index);
|
||||
final trails = <List<int>>[];
|
||||
for (var a = 0; a < 49; a++) {
|
||||
for (final step in [1, 7]) {
|
||||
final cells = [a, a + step, a + 2 * step];
|
||||
if (cells.last >= 49 || (step == 1 && a ~/ 7 != cells.last ~/ 7)) {
|
||||
continue;
|
||||
}
|
||||
if (solution[cells[1]] == 1 &&
|
||||
solution[cells[0]] + solution[cells[2]] == 1) {
|
||||
trails.add(cells);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, dynamic>? chosen;
|
||||
List<int>? givens;
|
||||
for (var attempt = 0; attempt < 600; attempt++) {
|
||||
final p = Puzzle(7, solution);
|
||||
for (final area
|
||||
in (level['areas'] as String)
|
||||
.split(';')
|
||||
.where((a) => a.isNotEmpty)) {
|
||||
final v = area.split(',').map(int.parse).toList();
|
||||
p.regions.add(
|
||||
Region(
|
||||
String.fromCharCode(65 + p.regions.length),
|
||||
v[0],
|
||||
v[1],
|
||||
v[2],
|
||||
v[3],
|
||||
),
|
||||
);
|
||||
}
|
||||
p.omittedRow = rng.nextInt(7);
|
||||
p.omittedColumn = rng.nextInt(7);
|
||||
final mechanics = <String, dynamic>{
|
||||
'omittedRow': p.omittedRow,
|
||||
'omittedColumn': p.omittedColumn,
|
||||
};
|
||||
final marked = <int>{};
|
||||
if (index == 6 || index == 10) {
|
||||
final trail = trails[rng.nextInt(trails.length)];
|
||||
p.footprintTrails.add(trail);
|
||||
marked.addAll(trail);
|
||||
mechanics['trail'] = trail;
|
||||
} else {
|
||||
final a = rng.nextInt(49);
|
||||
final options = [
|
||||
for (var b = 0; b < 49; b++)
|
||||
if (solution[a] != solution[b] &&
|
||||
a ~/ 7 != b ~/ 7 &&
|
||||
a % 7 != b % 7)
|
||||
b,
|
||||
];
|
||||
if (options.isEmpty) continue;
|
||||
final b = options[rng.nextInt(options.length)];
|
||||
p.lanternPairs.add(LanternPair(a, b));
|
||||
marked.addAll([a, b]);
|
||||
mechanics['lantern'] = [a, b];
|
||||
}
|
||||
for (var j = 0; j < 49; j++) {
|
||||
p.given[j] = !marked.contains(j);
|
||||
}
|
||||
final full = p.constraints;
|
||||
final order = [
|
||||
for (var j = 0; j < 49; j++)
|
||||
if (p.given[j]) j,
|
||||
]..shuffle(rng);
|
||||
for (final cell in order) {
|
||||
p.given[cell] = false;
|
||||
if (!LogicSolver(49, full).analyze(p.initial).solved ||
|
||||
p.countSolutions() != 1) {
|
||||
p.given[cell] = true;
|
||||
}
|
||||
}
|
||||
final without = full
|
||||
.where(
|
||||
(c) =>
|
||||
c.name != 'Lantern pair' &&
|
||||
!c.name.startsWith('Footprint trail'),
|
||||
)
|
||||
.toList();
|
||||
final logic = LogicSolver(49, full).analyze(p.initial);
|
||||
final thinking = ThinkingAnalyzer(
|
||||
49,
|
||||
full,
|
||||
).analyze(p.initial, maxLookahead: 0);
|
||||
final start = LogicSolver(49, full).analyze(p.initial, useAreas: false);
|
||||
final paced = path == 'assets/campaign.json'
|
||||
? logic.areaDeductions <= (index < 8 ? 5 : 6) &&
|
||||
start.cells.where((v) => v >= 0).length -
|
||||
p.given.where((v) => v).length >=
|
||||
7
|
||||
: thinking.rounds.length >= 4 &&
|
||||
thinking.rounds.length <= 8 &&
|
||||
thinking.comparisonSteps >= 2;
|
||||
if (paced &&
|
||||
p.countSolutions() == 1 &&
|
||||
LogicSolver(49, full).analyze(p.initial).solved &&
|
||||
!LogicSolver(49, full).analyze(p.initial, useAreas: false).solved &&
|
||||
LogicSolver(49, without).countSolutions(p.initial) > 1) {
|
||||
chosen = mechanics;
|
||||
givens = [
|
||||
for (var j = 0; j < 49; j++)
|
||||
if (p.given[j]) j,
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (chosen == null) throw StateError('No authored board: $path $index');
|
||||
level['mechanics'] = chosen;
|
||||
level['clues'] = givens!.join(',');
|
||||
stderr.writeln('$path ${level['id']}: ${givens.length} givens, $chosen');
|
||||
}
|
||||
File(path).writeAsStringSync(
|
||||
'${const JsonEncoder.withIndent(' ').convert(data)}\n',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user