From 3b8eb7c0e693cf14fdd5281bc6794cb22148b2c2 Mon Sep 17 00:00:00 2001 From: Lachfrosch Date: Sun, 16 Jun 2024 12:18:59 +0200 Subject: [PATCH] Starting to work more object oriented / move stuff into classes --- lib/enums/Color.dart | 10 +++++ lib/game/Game.dart | 0 lib/game/IGameConsumer.dart | 16 +++++++ lib/game/stone/Stone.dart | 27 ++++++++++++ lib/main.dart | 84 +++++++++++++++++++++---------------- pubspec.lock | 8 ++-- pubspec.yaml | 2 +- 7 files changed, 106 insertions(+), 41 deletions(-) create mode 100644 lib/enums/Color.dart create mode 100644 lib/game/Game.dart create mode 100644 lib/game/IGameConsumer.dart create mode 100644 lib/game/stone/Stone.dart diff --git a/lib/enums/Color.dart b/lib/enums/Color.dart new file mode 100644 index 0000000..ca61ed3 --- /dev/null +++ b/lib/enums/Color.dart @@ -0,0 +1,10 @@ +/// Enum for the stone colors. +enum Color { + red, + green, + blue, + yellow, + pink, + special, // not a "real" color; indicates that a stone is special (Not retarded, in a nice way, like mama always used to tell me i am her special child) +} + diff --git a/lib/game/Game.dart b/lib/game/Game.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/game/IGameConsumer.dart b/lib/game/IGameConsumer.dart new file mode 100644 index 0000000..974bfbe --- /dev/null +++ b/lib/game/IGameConsumer.dart @@ -0,0 +1,16 @@ +/// Interface for UI <--> game communication. +/// Informs the UI about changes on the state. +abstract class IGameConsumer { + /// Called when the score changes. + void updatePoints(); + + /// Called when the time changes. + void updateTime(); + + /// Called when there is a change on the board that should be shown to the user. + void updateStones(); + + /// Game stop signal. Called when the timer has run out. + void gameStopped(); +} + diff --git a/lib/game/stone/Stone.dart b/lib/game/stone/Stone.dart new file mode 100644 index 0000000..a4d24b9 --- /dev/null +++ b/lib/game/stone/Stone.dart @@ -0,0 +1,27 @@ +import 'dart:math'; +import '../../enums/Color.dart'; + +/// Class to represent a stone on the board. +class Stone { + /// Color of the stone + late Color color; + + Stone() { + setRandomColor(); + } + + /// Getter for the color. + /// \return color + Color getColor() => color; + + /// Setter for the color + /// \param color + void setColor(Color color) { + this.color = color; + } + + /// method that sets a random color + void setRandomColor() { + color = Color.values[Random().nextInt(Color.values.length)]; + } +} diff --git a/lib/main.dart b/lib/main.dart index 1d47e49..f2b8048 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,21 +17,6 @@ class MyApp extends StatelessWidget { return MaterialApp( title: 'Bubble-Twist', theme: ThemeData( - // This is the theme of your application. - // - // TRY THIS: Try running your application with "flutter run". You'll see - // the application has a purple toolbar. Then, without quitting the app, - // try changing the seedColor in the colorScheme below to Colors.green - // and then invoke "hot reload" (save your changes or press the "hot - // reload" button in a Flutter-supported IDE, or press "r" if you used - // the command line to start the app). - // - // Notice that the counter didn't reset back to zero; the application - // state is not lost during the reload. To reset the state, use hot - // restart instead. - // - // This works for code too, not just values: Most code changes can be - // tested with just a hot reload. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), @@ -63,6 +48,8 @@ class _MyHomePageState extends State { late List> _grid; int _selectedRow = -1; int _selectedCol = -1; + int _score = 0; + @override void initState() { @@ -82,42 +69,60 @@ class _MyHomePageState extends State { hasMatches = _removeMatches(); _applyGravity(); } while (hasMatches); + _score = 0; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text('Candy Crush'), + title: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text('Candy Crush'), + ], + ), ), body: Center( - child: Expanded( - child: GridView.builder( - shrinkWrap: true, - gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: widget.gridSize, + child: Column( + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + 'Score: $_score', + style: TextStyle(fontSize: 24), + ), ), - itemCount: widget.gridSize * widget.gridSize, - itemBuilder: (BuildContext context, int index) { - int row = index ~/ widget.gridSize; - int col = index % widget.gridSize; - return GestureDetector( - onTap: () => _onTileTap(row, col), - child: Container( - margin: const EdgeInsets.all(2), - decoration: BoxDecoration( - color: _getColor(_grid[row][col]), - borderRadius: BorderRadius.circular(8), - ), + Expanded( + child: GridView.builder( + shrinkWrap: true, + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: widget.gridSize, ), - ); - }, - ), + itemCount: widget.gridSize * widget.gridSize, + itemBuilder: (BuildContext context, int index) { + int row = index ~/ widget.gridSize; + int col = index % widget.gridSize; + return GestureDetector( + onTap: () => _onTileTap(row, col), + child: Container( + margin: const EdgeInsets.all(2), + decoration: BoxDecoration( + color: _getColor(_grid[row][col]), + borderRadius: BorderRadius.circular(8), + ), + ), + ); + }, + ), + ), + ], ), ), ); } + Color _getColor(int value) { switch (value) { case 0: @@ -208,12 +213,14 @@ class _MyHomePageState extends State { bool _removeMatches() { List> allMatches = []; + int scoreIncrement = 0; for (int row = 0; row < widget.gridSize; row++) { for (int col = 0; col < widget.gridSize; col++) { List> matches = _findMatches(row, col); if (matches.isNotEmpty) { allMatches.addAll(matches); + scoreIncrement++; // Increment score based on number of matches } } } @@ -222,9 +229,14 @@ class _MyHomePageState extends State { _grid[match[0]][match[1]] = -1; // Mark the tile as removed } + setState(() { + _score += scoreIncrement; + }); + return allMatches.isNotEmpty; } + void _applyGravity() { for (int col = 0; col < widget.gridSize; col++) { int emptyRow = widget.gridSize - 1; diff --git a/pubspec.lock b/pubspec.lock index 2c0dc73..f5c3299 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -66,10 +66,10 @@ packages: dependency: "direct dev" description: name: flutter_lints - sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04 + sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c" url: "https://pub.dev" source: hosted - version: "2.0.3" + version: "4.0.0" flutter_test: dependency: "direct dev" description: flutter @@ -103,10 +103,10 @@ packages: dependency: transitive description: name: lints - sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" + sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235" url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "4.0.0" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5ab6cde..4961b29 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -45,7 +45,7 @@ dev_dependencies: # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. - flutter_lints: ^2.0.0 + flutter_lints: ^4.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec