Skip to content
This repository was archived by the owner on Sep 11, 2022. It is now read-only.

Commit a76ce8b

Browse files
committed
Don't Shuffle Tiles on First Run
1 parent 5535b05 commit a76ce8b

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

lib/puzzle/bloc/puzzle_bloc.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ class PuzzleBloc extends Bloc<PuzzleEvent, PuzzleState> {
2121
on<PuzzleReset>(_onPuzzleReset);
2222
}
2323

24-
void _onPuzzleInitialized(PuzzleInitialized _, Emitter<PuzzleState> emit) {
25-
final puzzle = _generatePuzzle(_size);
24+
void _onPuzzleInitialized(
25+
PuzzleInitialized event,
26+
Emitter<PuzzleState> emit,
27+
) {
28+
final puzzle = _generatePuzzle(_size, isFirstRun: event.isFirstRun);
2629
emit(
2730
PuzzleState(
2831
puzzle: puzzle.sort(),
@@ -82,7 +85,7 @@ class PuzzleBloc extends Bloc<PuzzleEvent, PuzzleState> {
8285
}
8386

8487
/// Build a randomized, solvable puzzle of the given size.
85-
Puzzle _generatePuzzle(int size) {
88+
Puzzle _generatePuzzle(int size, {bool isFirstRun = false}) {
8689
final correctPositions = <Position>[];
8790
final currentPositions = <Position>[];
8891
final whitespacePosition = Position(x: size, y: size);
@@ -101,7 +104,9 @@ class PuzzleBloc extends Bloc<PuzzleEvent, PuzzleState> {
101104
}
102105
}
103106

104-
currentPositions.shuffle(random);
107+
if (!isFirstRun) {
108+
currentPositions.shuffle(random);
109+
}
105110

106111
final tiles =
107112
_getTileListFromPositions(size, correctPositions, currentPositions);

lib/puzzle/bloc/puzzle_event.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ abstract class PuzzleEvent extends Equatable {
1010
}
1111

1212
class PuzzleInitialized extends PuzzleEvent {
13+
final bool isFirstRun;
1314
@override
14-
List<Object> get props => [];
15+
List<Object> get props => [isFirstRun];
1516

16-
const PuzzleInitialized();
17+
const PuzzleInitialized({this.isFirstRun = false});
1718
}
1819

1920
class TileTapped extends PuzzleEvent {

lib/puzzle/view/puzzle_page.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class PuzzleView extends StatelessWidget {
6565
providers: [
6666
BlocProvider(create: (_) => TimerBloc(ticker: const Ticker())),
6767
BlocProvider(
68-
create: (_) => PuzzleBloc(4)..add(const PuzzleInitialized()),
68+
create: (_) => PuzzleBloc(4)
69+
..add(const PuzzleInitialized(isFirstRun: true)),
6970
),
7071
],
7172
child: const _Puzzle(key: Key('puzzle_view_puzzle')),

0 commit comments

Comments
 (0)