Newer
Older
import 'dart:math';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:suguru/config/application_config.dart';
import 'package:suguru/cubit/activity/activity_cubit.dart';
import 'package:suguru/data/game_data.dart';
import 'package:suguru/models/activity/board.dart';
import 'package:suguru/models/activity/cell.dart';
import 'package:suguru/models/activity/cell_location.dart';
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Activity {
Activity({
// Settings
required this.activitySettings,
// State
this.isRunning = false,
this.isStarted = false,
this.isFinished = false,
this.animationInProgress = false,
this.boardAnimated = const [],
// Base data
required this.board,
// Game data
this.boardConflicts = const [],
this.selectedCell,
this.showConflicts = false,
this.givenTipsCount = 0,
this.buttonTipsCountdown = 0,
});
// Settings
final ActivitySettings activitySettings;
// State
bool isRunning;
bool isStarted;
bool isFinished;
bool animationInProgress;
AnimatedBoard boardAnimated;
// Base data
final Board board;
// Game data
ConflictsCount boardConflicts;
Cell? selectedCell;
bool showConflicts;
int givenTipsCount;
int buttonTipsCountdown;
factory Activity.createEmpty() {
return Activity(
// Settings
activitySettings: ActivitySettings.createDefault(appConfig: ApplicationConfig.config),
// Base data
board: Board.createEmpty(),
// Game data
givenTipsCount: 0,
);
}
factory Activity.createNew({
ActivitySettings? activitySettings,
}) {
final ActivitySettings newActivitySettings = activitySettings ??
ActivitySettings.createDefault(appConfig: ApplicationConfig.config);
final List<String> templates =
GameData.templates[newActivitySettings.get(ApplicationConfig.parameterCodeBoardSize)]
?[newActivitySettings.get(ApplicationConfig.parameterCodeDifficultyLevel)] ??
[];
final String template = templates.elementAt(Random().nextInt(templates.length)).toString();
final List<String> templateParts = template.split(';');
if (templateParts.length != 3) {
printlog('Failed to get grid template (wrong format)...');
return Activity.createEmpty();
}
// Build main game board
final Board board = Board.createEmpty();
board.createFromTemplate(template: template);
// Build empty conflicts board
ConflictsCount nonConflictedBoard = [];
for (int row = 0; row < board.boardSizeVertical; row++) {
for (int col = 0; col < board.boardSizeHorizontal; col++) {
line.add(0);
}
nonConflictedBoard.add(line);
}
// Build empty animated background
AnimatedBoard notAnimatedBoard = [];
for (int row = 0; row < board.boardSizeVertical; row++) {
for (int col = 0; col < board.boardSizeHorizontal; col++) {
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
line.add(false);
}
notAnimatedBoard.add(line);
}
return Activity(
// Settings
activitySettings: newActivitySettings,
// State
isRunning: true,
boardAnimated: notAnimatedBoard,
// Base data
board: board,
// Game data
boardConflicts: nonConflictedBoard,
selectedCell: null,
);
}
bool get canBeResumed => isStarted && !isFinished;
bool get gameWon => isRunning && isStarted && isFinished;
bool get canGiveTip => (buttonTipsCountdown == 0);
bool checkBoardIsSolved() {
if (board.isSolved()) {
printlog('-> ok suguru solved!');
return true;
final BoardCells cells = board.cells;
// reset conflict states
for (CellLocation location in board.getCellLocations()) {
boardConflicts[location.row][location.col] = 0;
for (CellLocation location in board.getCellLocations()) {
final int value = board.get(location).value;
if (value != 0) {
if (board.cellHasSiblingWithSameValue(location)) {
boardConflicts[location.row][location.col]++;
}
}
}
// check blocks does not contain duplicates
for (String blockId in board.getBlockIds()) {
List<int> values = [];
List<int> duplicateValues = [];
for (CellLocation location in board.getCellLocations()) {
if (board.get(location).blockId == blockId) {
final int value = board.get(location).value;
if (value != 0) {
if (!values.contains(value)) {
values.add(value);
} else {
duplicateValues.add(value);
}
}
}
}
for (int duplicateValue in duplicateValues) {
for (CellLocation location in board.getCellLocations()) {
if (board.get(location).blockId == blockId &&
board.get(location).value == duplicateValue) {
boardConflicts[location.row][location.col]++;
}
}
}
}
}
void showTip(ActivityCubit activityCubit) {
bool tipGiven = false;
if (selectedCell == null) {
// no selected cell -> pick one
tipGiven = helpSelectCell(activityCubit);
} else {
// currently selected cell -> set value
tipGiven = helpFillCell(activityCubit, selectedCell?.location);
}
if (tipGiven) {
activityCubit.increaseGivenTipsCount();
}
}
bool helpSelectCell(ActivityCubit activityCubit) {
final List<CellLocation> wrongValueCells = getCellsWithWrongValue();
if (wrongValueCells.isNotEmpty) {
printlog('pick from wrongValueCells');
wrongValueCells.shuffle();
cellLocationToFix = wrongValueCells[0];
}
// pick one of conflicting cells, if found
final List<CellLocation> conflictingCells = getCellsWithConflicts();
if (conflictingCells.isNotEmpty) {
printlog('pick from conflictingCells');
conflictingCells.shuffle();
cellLocationToFix = conflictingCells[0];
if (cellLocationToFix != null) {
printlog('picked cell with wrong or conflicting value...');
activityCubit.selectCell(cellLocationToFix);
return true;
final Move? nextMove = SuguruSolver.pickNextMove(board);
if (nextMove == null) {
printlog('no easy cell to select...');
return false;
activityCubit.selectCell(nextMove.location);
return true;
}
bool helpFillCell(ActivityCubit activityCubit, CellLocation? location) {
final Move? nextMove = SuguruSolver.pickNextMove(board, location);
if (nextMove == null) {
printlog('unable to compute cell value for $location');
activityCubit.unselectCell();
return false;
activityCubit.updateCellValue(nextMove.location, nextMove.value);
activityCubit.unselectCell();
return true;
final BoardCells cells = board.cells;
final BoardCells cellsSolved = board.solvedCells;
for (CellLocation location in board.getCellLocations()) {
if (cells[location.row][location.col].value != 0 &&
cells[location.row][location.col].value !=
cellsSolved[location.row][location.col].value) {
cellsWithWrongValue.add(location);
}
}
return cellsWithWrongValue;
}
List<CellLocation> getCellsWithConflicts() {
List<CellLocation> cellsWithConflict = [];
for (CellLocation location in board.getCellLocations()) {
if (boardConflicts[location.row][location.col] != 0) {
cellsWithConflict.add(location);
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
}
}
return cellsWithConflict;
}
void dump() {
printlog('');
printlog('## Current game dump:');
printlog('');
printlog('$Activity:');
printlog(' Settings');
activitySettings.dump();
printlog(' State');
printlog(' isRunning: $isRunning');
printlog(' isStarted: $isStarted');
printlog(' isFinished: $isFinished');
printlog(' animationInProgress: $animationInProgress');
printlog(' Game data');
printlog(' selectedCell: ${selectedCell?.toString() ?? ''}');
printlog(' showConflicts: $showConflicts');
printlog(' givenTipsCount: $givenTipsCount');
printlog(' buttonTipsCountdown: $buttonTipsCountdown');
printlog('');
board.dump();
printlog('');
}
@override
String toString() {
return '$Activity(${toJson()})';
}
Map<String, dynamic>? toJson() {
return <String, dynamic>{
// Settings
'activitySettings': activitySettings.toJson(),
// State
'isRunning': isRunning,
'isStarted': isStarted,
'isFinished': isFinished,
'animationInProgress': animationInProgress,
'boardAnimated': boardAnimated,
// Base data
'board': board.toJson(),
// Game data
'boardConflicts': boardConflicts,
'selectedCell': selectedCell?.toJson(),
'showConflicts': showConflicts,
'givenTipsCount': givenTipsCount,
'buttonTipsCountdown': buttonTipsCountdown,
};
}
}