Skip to content

Commit

Permalink
v0.4.4
Browse files Browse the repository at this point in the history
 - Refined game state mechanic
 - Image list excess content indicator fixed
 - Basic scoreboard implementation
  • Loading branch information
ashishbeck committed Feb 24, 2022
1 parent 055cec7 commit f34d0f7
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 88 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.4

- Refined game state mechanic
- Image list excess content indicator fixed
- Basic scoreboard implementation

## 0.4.3

- Bug fixes
Expand Down
9 changes: 7 additions & 2 deletions lib/code/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ConfigProvider extends ChangeNotifier {
bool get showNumbers => _showNumbers;
bool _hasStarted = false;
bool get hasStarted => _hasStarted;
GameState _gameState = GameState.starting;
GameState _gameState = GameState.waiting;
GameState get gamestate => _gameState;
bool _muted = false;
bool get muted => _muted;
Expand Down Expand Up @@ -124,6 +124,11 @@ class ConfigProvider extends ChangeNotifier {
notifyListeners();
}

void aiSolving() {
_gameState = GameState.aiSolving;
notifyListeners();
}

void toggleSound() {
_muted = !_muted;
AudioService.instance.isMuted = _muted;
Expand Down Expand Up @@ -190,4 +195,4 @@ class ScoreProvider extends ChangeNotifier {
}
}

enum GameState { starting, waiting, started, finished }
enum GameState { starting, waiting, started, finished, aiSolving }
23 changes: 17 additions & 6 deletions lib/code/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import 'package:slide_puzzle/code/providers.dart';
class Service {
bool shouldVibrate = true;

List<TilesModel> changePosition(List<TilesModel> tileList,
TilesModel thisTile, TilesModel whiteTile, ScoreProvider scoreProvider,
List<TilesModel> changePosition(
List<TilesModel> tileList,
TilesModel thisTile,
TilesModel whiteTile,
ScoreProvider scoreProvider,
ConfigProvider configProvider,
{int gridSize = 1}) {
int distance =
((thisTile.currentIndex - whiteTile.currentIndex) ~/ gridSize);
Expand All @@ -37,15 +41,19 @@ class Service {
}
AudioService.instance.slide(Duration(milliseconds: defaultTime * 1));
AudioService.instance.vibrate();
if (!scoreProvider.isRunning && scoreProvider.beginState) {
if (configProvider.gamestate == GameState.waiting) {
// if (!scoreProvider.isRunning && scoreProvider.beginState) {
scoreProvider.beginTimer();
// }
}
if (configProvider.gamestate == GameState.started) {
scoreProvider.incrementMoves();
}
scoreProvider.incrementMoves();
return tileList;
}

List<TilesModel>? moveWhite(List<TilesModel> tileList, Direction direction,
ScoreProvider scoreProvider) {
ScoreProvider scoreProvider, ConfigProvider configProvider) {
int gridSize = sqrt(tileList.length).toInt();
TilesModel whiteTile = tileList.singleWhere((element) => element.isWhite);
int row = whiteTile.coordinates.row;
Expand All @@ -65,6 +73,7 @@ class Service {
replaceableTile,
whiteTile,
scoreProvider,
configProvider,
);
case Direction.down:
if (top) break;
Expand All @@ -76,6 +85,7 @@ class Service {
replaceableTile,
whiteTile,
scoreProvider,
configProvider,
gridSize: gridSize,
);
case Direction.right:
Expand All @@ -88,14 +98,15 @@ class Service {
replaceableTile,
whiteTile,
scoreProvider,
configProvider,
);
case Direction.up:
if (bottom) break;
var replaceableTile = tileList.singleWhere((element) =>
element.coordinates.row == row + 1 &&
element.coordinates.column == column);
return changePosition(
tileList, replaceableTile, whiteTile, scoreProvider,
tileList, replaceableTile, whiteTile, scoreProvider, configProvider,
gridSize: gridSize);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MyApp extends StatelessWidget {
// primarySwatch: primaryColor,
brightness: Brightness.dark,
),
home: LandingPage(),
home: false ? LayoutPage() : LandingPage(),
),
);
}
Expand Down
10 changes: 9 additions & 1 deletion lib/screen/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class _LayoutPageState extends State<LayoutPage> {
void solve() async {
TileProvider tileProvider = context.read<TileProvider>();
ScoreProvider scoreProvider = context.read<ScoreProvider>();
ConfigProvider configProvider = context.read<ConfigProvider>();
configProvider.aiSolving();
scoreProvider.stopTimer();
List<TilesModel> tileList = tileProvider.getTileList;
bool isSolved = Service().isSolved(tileList);
if (isSolved) {
Expand Down Expand Up @@ -137,7 +140,12 @@ class _LayoutPageState extends State<LayoutPage> {
default:
return;
}
Service().moveWhite(tileList, direction, scoreProvider);
Service().moveWhite(
tileList,
direction,
scoreProvider,
configProvider,
);
tileProvider.updateNotifiers();
i++;
if (i == result.length) timer.cancel();
Expand Down
12 changes: 11 additions & 1 deletion lib/screen/landing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ class _LandingPageState extends State<LandingPage>
),
TextSpan(
text: "\nwith design help from ",
style: Theme.of(context).textTheme.labelSmall),
style: Theme.of(context)
.textTheme
.labelSmall!
.copyWith(
decorationStyle: TextDecorationStyle.wavy,
decorationThickness: 4,
decoration: TextDecoration.lineThrough)),
WidgetSpan(
child: MouseRegion(
onEnter: (event) =>
Expand All @@ -161,6 +167,10 @@ class _LandingPageState extends State<LandingPage>
.textTheme
.labelSmall!
.copyWith(
decorationStyle:
TextDecorationStyle.wavy,
decorationThickness: 4,
decoration: TextDecoration.lineThrough,
color: isHovering2
? Colors.white
: primaryColor),
Expand Down
85 changes: 67 additions & 18 deletions lib/screen/puzzle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,43 @@ class _PuzzleState extends State<Puzzle> {
RawKeyEvent event,
TileProvider tileProvider,
ScoreProvider scoreProvider,
ConfigProvider configProvider,
) {
if (event.runtimeType == RawKeyDownEvent) {
if (event.runtimeType == RawKeyDownEvent &&
configProvider.gamestate == GameState.started) {
var tileList = tileProvider.getTileList;
var whiteTile = tileList.singleWhere((element) => element.isWhite);
if (event.data.logicalKey == LogicalKeyboardKey.arrowUp) {
Service().moveWhite(tileList, Direction.up, scoreProvider);
Service().moveWhite(
tileList,
Direction.up,
scoreProvider,
configProvider,
);
}
if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
Service().moveWhite(tileList, Direction.down, scoreProvider);
Service().moveWhite(
tileList,
Direction.down,
scoreProvider,
configProvider,
);
}
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
Service().moveWhite(tileList, Direction.left, scoreProvider);
Service().moveWhite(
tileList,
Direction.left,
scoreProvider,
configProvider,
);
}
if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
Service().moveWhite(tileList, Direction.right, scoreProvider);
Service().moveWhite(
tileList,
Direction.right,
scoreProvider,
configProvider,
);
}
tileProvider.updateNotifiers();
}
Expand Down Expand Up @@ -87,6 +109,37 @@ class _PuzzleState extends State<Puzzle> {
),
];

_checkIfSolved(List<TilesModel> tileList, ScoreProvider scoreProvider,
ConfigProvider configProvider) {
isSolved = Service().isSolved(tileList);
bool aiSolved = configProvider.gamestate == GameState.aiSolving;
if (isSolved &&
tileList.isNotEmpty &&
(configProvider.gamestate == GameState.started || aiSolved)) {
print("Solved!!");
scoreProvider.stopTimer();
configProvider.finish();
_launchScoreBoard(aiSolved, scoreProvider);
}
}

_launchScoreBoard(bool aiSolved, ScoreProvider scoreProvider) async {
await Future.delayed(Duration(milliseconds: 100));
showDialog(
context: context,
barrierColor: Colors.black.withOpacity(0.5),
builder: (context) {
// return AlertDialog(
// content: Text("asd"),
// );
return Center(
child: Text(aiSolved
? "Solved by AI"
: "${scoreProvider.moves} in ${scoreProvider.seconds}"),
);
});
}

@override
void initState() {
super.initState();
Expand All @@ -105,14 +158,7 @@ class _PuzzleState extends State<Puzzle> {
ConfigProvider configProvider = context.read<ConfigProvider>();
List<TilesModel> tileList = tileProvider.getTileList;
gridSize = sqrt(tileList.length).toInt();
isSolved = Service().isSolved(tileList);
if (isSolved &&
tileList.isNotEmpty &&
configProvider.gamestate == GameState.started) {
print("Solved!!");
scoreProvider.stopTimer();
configProvider.finish();
}
_checkIfSolved(tileList, scoreProvider, configProvider);
// list.forEach((e) {
// bool solved = true;
// if (e.currentIndex != e.defaultIndex) {
Expand Down Expand Up @@ -145,7 +191,7 @@ class _PuzzleState extends State<Puzzle> {
autofocus: true,
focusNode: _focusNode,
onKey: (RawKeyEvent event) {
_handleKeyEvent(event, tileProvider, scoreProvider);
_handleKeyEvent(event, tileProvider, scoreProvider, configProvider);
},
child: LayoutBuilder(
builder: (context, constraints) {
Expand Down Expand Up @@ -265,7 +311,7 @@ class _PuzzleTileState extends State<PuzzleTile> with TickerProviderStateMixin {
(tweenProvider.tweenTopOffset ?? 0).abs() > tileSize / 2)) {
if (isSameRow || isSameColumn) {
Service().changePosition(
widget.tileList, thisTile, whiteTile, scoreProvider,
widget.tileList, thisTile, whiteTile, scoreProvider, configProvider,
gridSize: isSameColumn ? widget.gridSize : 1);
tileProvider.updateNotifiers();
}
Expand Down Expand Up @@ -486,9 +532,11 @@ class _PuzzleTileState extends State<PuzzleTile> with TickerProviderStateMixin {
: null);
},
onPanStart: (_) {
configProvider.setDuration(const Duration(milliseconds: 0));
AudioService.instance.drag();
AudioService.instance.vibrate();
if (configProvider.gamestate == GameState.started) {
configProvider.setDuration(const Duration(milliseconds: 0));
AudioService.instance.drag();
AudioService.instance.vibrate();
}
},
onPanEnd: (details) {
if (configProvider.gamestate == GameState.started) {
Expand Down Expand Up @@ -520,6 +568,7 @@ class _PuzzleTileState extends State<PuzzleTile> with TickerProviderStateMixin {
thisTile,
whiteTile,
scoreProvider,
configProvider,
gridSize: isSameColumn ? widget.gridSize : 1,
);
tileProvider.updateNotifiers();
Expand Down
Loading

0 comments on commit f34d0f7

Please sign in to comment.