From ba0c367bfdafc1d6be4a12c86cda0257755fe892 Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Thu, 4 Sep 2025 22:09:12 -0300 Subject: [PATCH 1/7] for --- lib/dash_sweeper.dart | 61 +++++-------------------------------------- 1 file changed, 6 insertions(+), 55 deletions(-) diff --git a/lib/dash_sweeper.dart b/lib/dash_sweeper.dart index 8cbce5e..0bfbcf2 100644 --- a/lib/dash_sweeper.dart +++ b/lib/dash_sweeper.dart @@ -53,54 +53,10 @@ class _DashSweeperState extends State { return (row * widget.columns) + column; } - (int row, int column) getPointForIndex(int index) { + ({int row, int column}) getPointForIndex(int index) { final row = (index / widget.columns).floor(); final column = index % widget.columns; - return (row, column); - } - - int? topLeftIndex(int row, int column) { - final targetRow = row - 1; - final targetColumn = column - 1; - return getIndex(targetRow, targetColumn); - } - - int? topCenterIndex(int row, int column) { - final targetRow = row - 1; - return getIndex(targetRow, column); - } - - int? topRightIndex(int row, int column) { - final targetRow = row - 1; - final targetColumn = column + 1; - return getIndex(targetRow, targetColumn); - } - - int? leftIndex(int row, int column) { - final targetColumn = column - 1; - return getIndex(row, targetColumn); - } - - int? rightIndex(int row, int column) { - final targetColumn = column + 1; - return getIndex(row, targetColumn); - } - - int? bottomLeftIndex(int row, int column) { - final targetRow = row + 1; - final targetColumn = column - 1; - return getIndex(targetRow, targetColumn); - } - - int? bottomCenterIndex(int row, int column) { - final targetRow = row + 1; - return getIndex(targetRow, column); - } - - int? bottomRightIndex(int row, int column) { - final targetRow = row + 1; - final targetColumn = column + 1; - return getIndex(targetRow, targetColumn); + return (row: row, column: column); } List getNeighbouringDashes(int index) { @@ -110,16 +66,11 @@ class _DashSweeperState extends State { } Iterable getNeighbouringIndeces(int index) { - final (row, column) = getPointForIndex(index); + final point = getPointForIndex(index); return [ - topLeftIndex(row, column), - topCenterIndex(row, column), - topRightIndex(row, column), - leftIndex(row, column), - rightIndex(row, column), - bottomLeftIndex(row, column), - bottomCenterIndex(row, column), - bottomRightIndex(row, column), + for (var row = point.row - 1; row < point.row + 2; row++) + for (var column = point.column - 1; column < point.column + 2; column++) + getIndex(row, column), ].nonNulls; } From 23056636c358ae82f436292f75de40061201aac8 Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Thu, 4 Sep 2025 22:11:32 -0300 Subject: [PATCH 2/7] first extension --- lib/dash_sweeper.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/dash_sweeper.dart b/lib/dash_sweeper.dart index 0bfbcf2..47099f1 100644 --- a/lib/dash_sweeper.dart +++ b/lib/dash_sweeper.dart @@ -68,8 +68,8 @@ class _DashSweeperState extends State { Iterable getNeighbouringIndeces(int index) { final point = getPointForIndex(index); return [ - for (var row = point.row - 1; row < point.row + 2; row++) - for (var column = point.column - 1; column < point.column + 2; column++) + for (var row in point.row.adjacents) + for (var column in point.column.adjacents) getIndex(row, column), ].nonNulls; } @@ -348,3 +348,7 @@ class TileItem extends StatelessWidget { ); } } + +extension on int { + List get adjacents => [this - 1, this, this + 1]; +} From 314122dc75647799f54229372a5f0ea0568a59f5 Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Thu, 4 Sep 2025 22:15:25 -0300 Subject: [PATCH 3/7] final extension version --- lib/dash_sweeper.dart | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/dash_sweeper.dart b/lib/dash_sweeper.dart index 47099f1..bc614d2 100644 --- a/lib/dash_sweeper.dart +++ b/lib/dash_sweeper.dart @@ -53,7 +53,7 @@ class _DashSweeperState extends State { return (row * widget.columns) + column; } - ({int row, int column}) getPointForIndex(int index) { + NamedPoint getPointForIndex(int index) { final row = (index / widget.columns).floor(); final column = index % widget.columns; return (row: row, column: column); @@ -68,9 +68,7 @@ class _DashSweeperState extends State { Iterable getNeighbouringIndeces(int index) { final point = getPointForIndex(index); return [ - for (var row in point.row.adjacents) - for (var column in point.column.adjacents) - getIndex(row, column), + for (var (:row, :column) in point.adjacents) getIndex(row, column), ].nonNulls; } @@ -349,6 +347,17 @@ class TileItem extends StatelessWidget { } } +typedef NamedPoint = ({int row, int column}); + extension on int { List get adjacents => [this - 1, this, this + 1]; } + +extension on NamedPoint { + List get adjacents { + return [ + for (var row in row.adjacents) + for (var column in column.adjacents) (row: row, column: column), + ]; + } +} From 79e0de2a31ce54936ba0e1b71652c6a6bce196dd Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Thu, 4 Sep 2025 22:18:23 -0300 Subject: [PATCH 4/7] small refactor --- lib/dash_sweeper.dart | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/dash_sweeper.dart b/lib/dash_sweeper.dart index bc614d2..31375d3 100644 --- a/lib/dash_sweeper.dart +++ b/lib/dash_sweeper.dart @@ -48,7 +48,8 @@ class _DashSweeperState extends State { column < widget.columns; } - int? getIndex(int row, int column) { + int? getIndex(NamedPoint point) { + var (:row, :column) = point; if (!inBounds(row, column)) return null; return (row * widget.columns) + column; } @@ -67,9 +68,7 @@ class _DashSweeperState extends State { Iterable getNeighbouringIndeces(int index) { final point = getPointForIndex(index); - return [ - for (var (:row, :column) in point.adjacents) getIndex(row, column), - ].nonNulls; + return point.adjacents.map(getIndex).nonNulls; } void handleOnLongPress(int index) { From ba04b5b56a39ece779d60f71ec4416af75a41c47 Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Thu, 4 Sep 2025 23:01:19 -0300 Subject: [PATCH 5/7] New properties to TileState enum and improve TileItem widget behavior --- lib/dash_sweeper.dart | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/dash_sweeper.dart b/lib/dash_sweeper.dart index 31375d3..c645bb6 100644 --- a/lib/dash_sweeper.dart +++ b/lib/dash_sweeper.dart @@ -225,7 +225,15 @@ class _DashSweeperState extends State { } } -enum TileState { idle, revealed, flagged } +enum TileState { + idle, + revealed, + flagged; + + bool get isIdle => this == TileState.idle; + bool get isRevealed => this == TileState.revealed; + bool get isFlagged => this == TileState.flagged; +} sealed class Tile { final TileState state; @@ -323,18 +331,18 @@ class TileItem extends StatelessWidget { @override Widget build(BuildContext context) { return Card( - elevation: state != TileState.idle ? 0 : 4, + elevation: state.isIdle ? 4 : 0, clipBehavior: Clip.antiAliasWithSaveLayer, child: InkWell( - onTap: onTap, - onLongPress: onLongPress, + onTap: state.isIdle ? onTap : null, + onLongPress: state.isIdle ? onLongPress : null, child: Center( child: AnimatedSwitcher( duration: Duration(milliseconds: 300), child: switch (state) { - TileState.idle => SizedBox(), + TileState.idle => const SizedBox.shrink(), TileState.revealed => child, - TileState.flagged => Icon( + TileState.flagged => const Icon( Icons.flag, color: Colors.red, ), From 4e4d0dbe3d0b87262c77e5d3303ff47eb6371114 Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Thu, 4 Sep 2025 23:05:05 -0300 Subject: [PATCH 6/7] Refactor neighbouring dashes calculation to improve readability and performance --- lib/dash_sweeper.dart | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/dash_sweeper.dart b/lib/dash_sweeper.dart index c645bb6..d71f6d4 100644 --- a/lib/dash_sweeper.dart +++ b/lib/dash_sweeper.dart @@ -60,10 +60,10 @@ class _DashSweeperState extends State { return (row: row, column: column); } - List getNeighbouringDashes(int index) { - return getNeighbouringIndeces( - index, - ).where((tileIndex) => tiles[tileIndex] is DashTile).toList(); + int countNeighbouringDashes(int index) { + return getNeighbouringIndeces(index) // formatting hack + .where((tileIndex) => tiles[tileIndex] is DashTile) + .length; } Iterable getNeighbouringIndeces(int index) { @@ -184,8 +184,8 @@ class _DashSweeperState extends State { toReveal.add(index); checked.add(index); - final dashes = getNeighbouringDashes(index); - if (dashes.isNotEmpty) return toReveal; + final dashes = countNeighbouringDashes(index); + if (dashes > 0) return toReveal; final neighbours = getNeighbouringIndeces(index); if (checked.containsAll(neighbours)) return toReveal; @@ -213,7 +213,7 @@ class _DashSweeperState extends State { onLongPress: () => handleOnLongPress(index), ), EmptyTile(:final state) => EmptyTileItem( - amount: getNeighbouringDashes(index).length, + amount: countNeighbouringDashes(index), state: state, onTap: () => handleOnTap(index), onLongPress: () => handleOnLongPress(index), From 2aa72c573b9f3121973320e20f69c31e84815618 Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Fri, 5 Sep 2025 00:07:19 -0300 Subject: [PATCH 7/7] a bit more refactoring --- lib/dash_sweeper.dart | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/dash_sweeper.dart b/lib/dash_sweeper.dart index d71f6d4..fb73d08 100644 --- a/lib/dash_sweeper.dart +++ b/lib/dash_sweeper.dart @@ -29,16 +29,10 @@ class _DashSweeperState extends State { final dashAmount = amount ~/ 10; // TODO customize percentage final emptyAmount = amount - dashAmount; - // create tiles - final dashTiles = List.generate( - dashAmount, - (_) => DashTile(TileState.idle), - ); - final emptyTiles = List.generate( - emptyAmount, - (_) => EmptyTile(TileState.idle), - ); - tiles = [...dashTiles, ...emptyTiles]..shuffle(); + tiles = [ + for (var _ in dashAmount.thisLengthList) DashTile(TileState.idle), + for (var _ in emptyAmount.thisLengthList) EmptyTile(TileState.idle), + ]..shuffle(); } bool inBounds(int row, int column) { @@ -87,7 +81,7 @@ class _DashSweeperState extends State { void handleOnTap(int index) { final tile = tiles[index]; - if (tile.state != TileState.idle) return; + if (!tile.state.isIdle) return; if (tile is DashTile) { _gameOver(); @@ -358,6 +352,8 @@ typedef NamedPoint = ({int row, int column}); extension on int { List get adjacents => [this - 1, this, this + 1]; + + List get thisLengthList => [for (var i = 0; i < this; i++) i]; } extension on NamedPoint {