diff --git a/lib/main.dart b/lib/main.dart index 2a8b23e..72501d5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'package:bubbletwist/enums/stone_color.dart'; import 'package:bubbletwist/game/game.dart'; import 'package:bubbletwist/game/i_game_consumer.dart'; import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'game/stone/stone.dart'; import 'game/stone/stone_location.dart'; @@ -46,6 +47,9 @@ class _MyHomePageState extends State implements IGameConsumer { int _time = 0; StoneLocation? sl1; bool _isGameOver = false; + bool _isScoreboardVisible = false; + String _playerName = ""; + List> _scoreboard = []; @override void initState() { @@ -53,6 +57,29 @@ class _MyHomePageState extends State implements IGameConsumer { game = Game(this); _grid = List.generate(widget.gridSize, (index) => List.generate(widget.gridSize, (index) => Stone())); + _loadScoreboard(); + } + + void _loadScoreboard() async { + final prefs = await SharedPreferences.getInstance(); + final List> loadedScoreboard = + List>.from( + (prefs.getStringList('scoreboard') ?? []) + .map((e) => Map.from(e as Map))); + + setState(() { + _scoreboard = loadedScoreboard; + }); + } + + void _saveScore(String name, int score) async { + final prefs = await SharedPreferences.getInstance(); + _scoreboard.add({'name': name, 'score': score}); + _scoreboard.sort((a, b) => b['score'].compareTo(a['score'])); + await prefs.setString('scoreboard', _scoreboard.toString()); + setState(() { + _isScoreboardVisible = true; + }); } @override @@ -69,7 +96,9 @@ class _MyHomePageState extends State implements IGameConsumer { ), body: Center( child: _isGameOver - ? _buildGameOverMessage() + ? _isScoreboardVisible + ? _buildScoreboard() + : _buildGameOverMessage() : game.running ? _buildGameGrid() : _buildStartButton(), @@ -102,10 +131,90 @@ class _MyHomePageState extends State implements IGameConsumer { style: const TextStyle(fontSize: 24), ), const SizedBox(height: 20), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 20.0), + child: TextField( + decoration: const InputDecoration(labelText: 'Enter your name'), + onChanged: (value) { + setState(() { + _playerName = value; + }); + }, + ), + ), + const SizedBox(height: 20), + ElevatedButton( + onPressed: _playerName.isEmpty + ? null + : () { + _saveScore(_playerName, _score); + }, + child: const Text('Submit'), + ), + ], + ); + } + + Widget _buildScoreboard() { + return Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text( + 'Scoreboard', + style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold), + ), + const SizedBox(height: 20), + const Padding( + padding: EdgeInsets.symmetric(horizontal: 16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Expanded( + child: Center( + child: Text('Name', + style: TextStyle( + fontWeight: FontWeight.bold, fontSize: 18)))), + Expanded( + child: Center( + child: Text('Score', + style: TextStyle( + fontWeight: FontWeight.bold, fontSize: 18)))), + ], + ), + ), + const SizedBox(height: 10), + Expanded( + child: ListView.builder( + itemCount: _scoreboard.length, + itemBuilder: (context, index) { + final player = _scoreboard[index]; + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Expanded( + child: Center( + child: Text(player['name'], + style: const TextStyle(fontSize: 16)))), + Expanded( + child: Center( + child: Text(player['score'].toString(), + style: const TextStyle(fontSize: 16)))), + ], + ), + ); + }, + ), + ), + const SizedBox(height: 20), ElevatedButton( onPressed: () { - _isGameOver = false; - game.start(); + setState(() { + _isScoreboardVisible = false; + _isGameOver = false; + game.start(); + }); }, child: const Text('Restart'), ), diff --git a/pubspec.lock b/pubspec.lock index f5c3299..f94cd3e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -57,6 +57,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + ffi: + dependency: transitive + description: + name: ffi + sha256: "493f37e7df1804778ff3a53bd691d8692ddf69702cf4c1c1096a2e41b4779e21" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + file: + dependency: transitive + description: + name: file + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" + url: "https://pub.dev" + source: hosted + version: "7.0.0" flutter: dependency: "direct main" description: flutter @@ -75,6 +91,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" leak_tracker: dependency: transitive description: @@ -139,6 +160,102 @@ packages: url: "https://pub.dev" source: hosted version: "1.9.0" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279 + url: "https://pub.dev" + source: hosted + version: "2.2.1" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334" + url: "https://pub.dev" + source: hosted + version: "2.1.2" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: "8bc9f22eee8690981c22aa7fc602f5c85b497a6fb2ceb35ee5a5e5ed85ad8170" + url: "https://pub.dev" + source: hosted + version: "2.2.1" + platform: + dependency: transitive + description: + name: platform + sha256: "9b71283fc13df574056616011fb138fd3b793ea47cc509c189a6c3fa5f8a1a65" + url: "https://pub.dev" + source: hosted + version: "3.1.5" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02" + url: "https://pub.dev" + source: hosted + version: "2.1.8" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180 + url: "https://pub.dev" + source: hosted + version: "2.2.3" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "93d0ec9dd902d85f326068e6a899487d1f65ffcd5798721a95330b26c8131577" + url: "https://pub.dev" + source: hosted + version: "2.2.3" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "0a8a893bf4fd1152f93fec03a415d11c27c74454d96e2318a7ac38dd18683ab7" + url: "https://pub.dev" + source: hosted + version: "2.4.0" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a" + url: "https://pub.dev" + source: hosted + version: "2.3.0" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59" + url: "https://pub.dev" + source: hosted + version: "2.3.2" sky_engine: dependency: transitive description: flutter @@ -208,6 +325,30 @@ packages: url: "https://pub.dev" source: hosted version: "14.2.1" + web: + dependency: transitive + description: + name: web + sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27" + url: "https://pub.dev" + source: hosted + version: "0.5.1" + win32: + dependency: transitive + description: + name: win32 + sha256: a79dbe579cb51ecd6d30b17e0cae4e0ea15e2c0e66f69ad4198f22a6789e94f4 + url: "https://pub.dev" + source: hosted + version: "5.5.1" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: faea9dee56b520b55a566385b84f2e8de55e7496104adada9962e0bd11bcff1d + url: "https://pub.dev" + source: hosted + version: "1.0.4" sdks: - dart: ">=3.3.2 <4.0.0" - flutter: ">=3.18.0-18.0.pre.54" + dart: ">=3.4.0 <4.0.0" + flutter: ">=3.22.0" diff --git a/pubspec.yaml b/pubspec.yaml index 4961b29..98b9910 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + shared_preferences: ^2.0.6 dev_dependencies: flutter_test: