Skip to content

Commit 5d5bf67

Browse files
handle generateMaze
1 parent f83fad9 commit 5d5bf67

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

lib/features/grid/view_model/grid_notifier.dart

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ part 'grid_notifier_state.dart';
88
class GridNotifierCubit extends StateNotifier<GridNotifierState> {
99
GridNotifierCubit() : super(GridNotifierState());
1010
final int columnSquares = 20;
11+
static const Duration scaleAppearDurationForWall = Duration(milliseconds: 700);
12+
1113
bool isTapDown = false;
1214

13-
void updateGridLayout(BoxConstraints constraints) {
14-
final screenWidth = constraints.maxWidth;
15-
final screenHeight = constraints.maxHeight;
15+
void updateGridLayout(Size size) {
16+
final screenWidth = size.width;
17+
final screenHeight = size.height;
1618

1719
final double gridSize =
1820
(screenWidth < screenHeight) ? screenWidth / columnSquares : screenHeight / columnSquares;
@@ -136,39 +138,55 @@ class GridNotifierCubit extends StateNotifier<GridNotifierState> {
136138
}
137139

138140
void generateMaze() {
139-
final gridData = List<GridStatus>.filled(state.gridCount, GridStatus.empty);
141+
final gridData = List<GridStatus>.from(state.gridData);
142+
143+
// Clear the maze but keep start and target points
144+
for (int i = 0; i < gridData.length; i++) {
145+
if (gridData[i] != GridStatus.startPoint && gridData[i] != GridStatus.targetPoint) {
146+
gridData[i] = GridStatus.empty;
147+
}
148+
}
149+
150+
// Start the division from the full grid
140151
_recursiveDivision(gridData, 0, state.rowMainAxisCount, 0, state.columnCrossAxisCount);
141-
state = state.copyWith(gridData: gridData);
142152
}
143153

144-
void _recursiveDivision(List<GridStatus> gridData, int rowStart, int rowEnd, int colStart, int colEnd) {
145-
if (rowEnd - rowStart <= 1 || colEnd - colStart <= 1) return;
154+
Future<void> _recursiveDivision(
155+
List<GridStatus> gridData, int rowStart, int rowEnd, int colStart, int colEnd) async {
156+
if (rowEnd - rowStart < 2 || colEnd - colStart < 2) return; // Avoid too small segments
146157

147-
bool horizontal = (rowEnd - rowStart > colEnd - colStart);
148-
if (horizontal) {
149-
int row = (rowStart + rowEnd) ~/ 2;
150-
for (int col = colStart; col < colEnd; col++) {
151-
gridData[row * state.columnCrossAxisCount + col] = GridStatus.wall;
152-
}
153-
_recursiveDivision(gridData, rowStart, row, colStart, colEnd);
154-
_recursiveDivision(gridData, row + 1, rowEnd, colStart, colEnd);
155-
} else {
156-
int col = (colStart + colEnd) ~/ 2;
158+
bool divideVertically = Random().nextBool();
159+
160+
if (divideVertically) {
161+
int splitCol = Random().nextInt(colEnd - colStart - 1) + colStart + 1;
157162
for (int row = rowStart; row < rowEnd; row++) {
158-
gridData[row * state.columnCrossAxisCount + col] = GridStatus.wall;
163+
if (row == rowStart || row == rowEnd - 1) continue; // Skip the boundary
164+
gridData[row * state.columnCrossAxisCount + splitCol] = GridStatus.wall;
165+
state = state.copyWith(gridData: List<GridStatus>.from(gridData));
166+
await Future.delayed(const Duration(milliseconds: 10)); // Delay for animation
159167
}
160-
_recursiveDivision(gridData, rowStart, rowEnd, colStart, col);
161-
_recursiveDivision(gridData, rowStart, rowEnd, col + 1, colEnd);
162-
}
163168

164-
// Add a passage through the wall
165-
int passageIndex;
166-
if (horizontal) {
167-
passageIndex = Random().nextInt(colEnd - colStart) + colStart;
168-
gridData[(rowStart + rowEnd) ~/ 2 * state.columnCrossAxisCount + passageIndex] = GridStatus.empty;
169+
// Open a passage
170+
int passageAt = Random().nextInt(rowEnd - rowStart) + rowStart;
171+
gridData[passageAt * state.columnCrossAxisCount + splitCol] = GridStatus.empty;
172+
173+
await _recursiveDivision(gridData, rowStart, rowEnd, colStart, splitCol);
174+
await _recursiveDivision(gridData, rowStart, rowEnd, splitCol + 1, colEnd);
169175
} else {
170-
passageIndex = Random().nextInt(rowEnd - rowStart) + rowStart;
171-
gridData[passageIndex * state.columnCrossAxisCount + (colStart + colEnd) ~/ 2] = GridStatus.empty;
176+
int splitRow = Random().nextInt(rowEnd - rowStart - 1) + rowStart + 1;
177+
for (int col = colStart; col < colEnd; col++) {
178+
if (col == colStart || col == colEnd - 1) continue; // Skip the boundary
179+
gridData[splitRow * state.columnCrossAxisCount + col] = GridStatus.wall;
180+
state = state.copyWith(gridData: List<GridStatus>.from(gridData));
181+
await Future.delayed(const Duration(milliseconds: 10)); // Delay for animation
182+
}
183+
184+
// Open a passage
185+
int passageAt = Random().nextInt(colEnd - colStart) + colStart;
186+
gridData[splitRow * state.columnCrossAxisCount + passageAt] = GridStatus.empty;
187+
188+
await _recursiveDivision(gridData, rowStart, splitRow, colStart, colEnd);
189+
await _recursiveDivision(gridData, splitRow + 1, rowEnd, colStart, colEnd);
172190
}
173191
}
174192
}

0 commit comments

Comments
 (0)