Starting to work more object oriented / move stuff into classes

main
Lachfrosch 2024-06-16 12:18:59 +02:00
parent 578ddab021
commit 3b8eb7c0e6
7 changed files with 106 additions and 41 deletions

View File

@ -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)
}

View File

View File

@ -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();
}

View File

@ -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)];
}
}

View File

@ -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<MyHomePage> {
late List<List<int>> _grid;
int _selectedRow = -1;
int _selectedCol = -1;
int _score = 0;
@override
void initState() {
@ -82,16 +69,31 @@ class _MyHomePageState extends State<MyHomePage> {
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: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Score: $_score',
style: TextStyle(fontSize: 24),
),
),
Expanded(
child: GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
@ -114,10 +116,13 @@ class _MyHomePageState extends State<MyHomePage> {
},
),
),
],
),
),
);
}
Color _getColor(int value) {
switch (value) {
case 0:
@ -208,12 +213,14 @@ class _MyHomePageState extends State<MyHomePage> {
bool _removeMatches() {
List<List<int>> allMatches = [];
int scoreIncrement = 0;
for (int row = 0; row < widget.gridSize; row++) {
for (int col = 0; col < widget.gridSize; col++) {
List<List<int>> 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<MyHomePage> {
_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;

View File

@ -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:

View File

@ -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