2024-06-16 23:56:48 +02:00
|
|
|
import 'package:bubbletwist/enums/StoneColor.dart';
|
|
|
|
import 'package:bubbletwist/game/Game.dart';
|
|
|
|
import 'package:bubbletwist/game/IGameConsumer.dart';
|
2024-04-24 16:21:26 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
import 'game/stone/Stone.dart';
|
|
|
|
import 'game/stone/StoneLocation.dart';
|
|
|
|
|
2024-04-24 16:21:26 +02:00
|
|
|
void main() {
|
|
|
|
runApp(const MyApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
|
const MyApp({super.key});
|
2024-05-08 16:24:32 +02:00
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
final int gridSize = 8;
|
2024-04-24 16:21:26 +02:00
|
|
|
|
|
|
|
// This widget is the root of your application.
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
2024-05-08 15:57:00 +02:00
|
|
|
title: 'Bubble-Twist',
|
2024-04-24 16:21:26 +02:00
|
|
|
theme: ThemeData(
|
|
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
|
|
useMaterial3: true,
|
|
|
|
),
|
2024-06-16 23:56:48 +02:00
|
|
|
home: const MyHomePage(title: 'Flutter Demo Home Page', gridSize: 8),
|
2024-04-24 16:21:26 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyHomePage extends StatefulWidget {
|
2024-05-08 16:05:52 +02:00
|
|
|
const MyHomePage({super.key, required this.title, required this.gridSize});
|
2024-04-24 16:21:26 +02:00
|
|
|
|
|
|
|
// This widget is the home page of your application. It is stateful, meaning
|
|
|
|
// that it has a State object (defined below) that contains fields that affect
|
|
|
|
// how it looks.
|
|
|
|
|
|
|
|
// This class is the configuration for the state. It holds the values (in this
|
|
|
|
// case the title) provided by the parent (in this case the App widget) and
|
|
|
|
// used by the build method of the State. Fields in a Widget subclass are
|
|
|
|
// always marked "final".
|
|
|
|
|
|
|
|
final String title;
|
2024-05-08 16:05:52 +02:00
|
|
|
final int gridSize;
|
2024-04-24 16:21:26 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
|
|
}
|
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
|
|
|
|
late List<List<Stone?>> _grid;
|
|
|
|
late Game game;
|
2024-06-17 10:48:31 +02:00
|
|
|
int _score = 0;
|
|
|
|
int _time = 0;
|
|
|
|
StoneLocation? sl1;
|
2024-04-24 16:21:26 +02:00
|
|
|
|
|
|
|
@override
|
2024-05-08 16:05:52 +02:00
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-06-16 23:56:48 +02:00
|
|
|
game = Game(this);
|
2024-05-08 16:24:32 +02:00
|
|
|
_grid = List.generate(widget.gridSize,
|
2024-06-16 23:56:48 +02:00
|
|
|
(index) => List.generate(widget.gridSize, (index) => Stone()));
|
|
|
|
game.start();
|
2024-05-08 16:05:52 +02:00
|
|
|
}
|
|
|
|
|
2024-05-08 16:18:42 +02:00
|
|
|
@override
|
2024-04-24 16:21:26 +02:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-06-16 23:56:48 +02:00
|
|
|
title: Text(widget.title),
|
2024-06-17 10:48:31 +02:00
|
|
|
actions: [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(right: 20.0),
|
|
|
|
child: Text("Time: $_time s"),
|
|
|
|
),
|
|
|
|
],
|
2024-04-24 16:21:26 +02:00
|
|
|
),
|
|
|
|
body: Center(
|
2024-06-16 12:18:59 +02:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(
|
|
|
|
'Score: $_score',
|
2024-06-16 23:56:48 +02:00
|
|
|
style: const TextStyle(fontSize: 24),
|
2024-06-16 12:18:59 +02:00
|
|
|
),
|
2024-05-08 16:18:42 +02:00
|
|
|
),
|
2024-06-16 12:18:59 +02:00
|
|
|
Expanded(
|
|
|
|
child: GridView.builder(
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
crossAxisCount: widget.gridSize,
|
2024-05-08 15:57:00 +02:00
|
|
|
),
|
2024-06-16 12:18:59 +02:00
|
|
|
itemCount: widget.gridSize * widget.gridSize,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
int row = index ~/ widget.gridSize;
|
|
|
|
int col = index % widget.gridSize;
|
2024-06-16 23:56:48 +02:00
|
|
|
StoneLocation location = StoneLocation(row: row, column: col);
|
2024-06-17 10:48:31 +02:00
|
|
|
Stone? stone = game.getStone(location);
|
|
|
|
|
|
|
|
// Überprüfen, ob der Stein in sl1 gespeichert ist
|
|
|
|
bool isSelected = (sl1 != null && sl1!.row == row && sl1!.column == col);
|
|
|
|
|
2024-06-16 12:18:59 +02:00
|
|
|
return GestureDetector(
|
2024-06-16 23:56:48 +02:00
|
|
|
onTap: () {
|
|
|
|
setState(() {
|
2024-06-17 10:48:31 +02:00
|
|
|
handleTap(row, col);
|
2024-06-16 23:56:48 +02:00
|
|
|
});
|
|
|
|
},
|
2024-06-16 12:18:59 +02:00
|
|
|
child: Container(
|
|
|
|
margin: const EdgeInsets.all(2),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
2024-06-17 10:48:31 +02:00
|
|
|
color: _getColorForStone(stone),
|
|
|
|
border: Border.all(
|
|
|
|
color: isSelected ? Colors.black : Colors.transparent,
|
|
|
|
width: 5,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: Center(
|
|
|
|
child: Text(
|
|
|
|
_getStoneColorText(stone) + row.toString() + col.toString(),
|
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
),
|
2024-06-16 12:18:59 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2024-04-24 16:21:26 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-08 15:57:00 +02:00
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
Color _getColorForStone(Stone? stone) {
|
|
|
|
// Beispielhafte Farben basierend auf dem Stein.
|
|
|
|
switch (stone?.getStoneColor()) {
|
|
|
|
case StoneColors.red:
|
2024-05-08 15:57:00 +02:00
|
|
|
return Colors.red;
|
2024-06-16 23:56:48 +02:00
|
|
|
case StoneColors.green:
|
2024-05-08 15:57:00 +02:00
|
|
|
return Colors.green;
|
2024-06-16 23:56:48 +02:00
|
|
|
case StoneColors.blue:
|
|
|
|
return Colors.blue;
|
|
|
|
case StoneColors.yellow:
|
2024-05-08 15:57:00 +02:00
|
|
|
return Colors.yellow;
|
2024-06-17 10:48:31 +02:00
|
|
|
case StoneColors.pink:
|
|
|
|
return Colors.pink;
|
2024-05-08 15:57:00 +02:00
|
|
|
default:
|
2024-06-16 23:56:48 +02:00
|
|
|
return Colors.grey;
|
2024-05-08 15:57:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 10:48:31 +02:00
|
|
|
String _getStoneColorText(Stone? stone) {
|
|
|
|
if (stone == null) return 'Unknown';
|
|
|
|
return stone.getStoneColor().toString().split('.').last;
|
|
|
|
}
|
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
@override
|
|
|
|
void gameStopped() {
|
|
|
|
// TODO: implement gameStopped
|
2024-05-08 16:24:32 +02:00
|
|
|
}
|
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
@override
|
|
|
|
void updatePoints() {
|
2024-06-17 10:48:31 +02:00
|
|
|
_score = game.getPoints();
|
2024-05-24 12:38:38 +02:00
|
|
|
}
|
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
@override
|
|
|
|
void updateStones() {
|
|
|
|
setState(() {
|
|
|
|
// _grid aktualisieren
|
|
|
|
for (int row = 0; row < widget.gridSize; row++) {
|
|
|
|
for (int col = 0; col < widget.gridSize; col++) {
|
|
|
|
_grid[row][col] = game.getStone(StoneLocation(row: row, column: col));
|
2024-05-24 12:38:38 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-16 12:18:59 +02:00
|
|
|
});
|
2024-05-24 12:38:38 +02:00
|
|
|
}
|
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
@override
|
|
|
|
void updateTime() {
|
2024-06-17 10:48:31 +02:00
|
|
|
setState(() {
|
|
|
|
_time = game.getTimeInSeconds();
|
|
|
|
});
|
2024-05-24 12:38:38 +02:00
|
|
|
}
|
|
|
|
|
2024-06-16 23:56:48 +02:00
|
|
|
Game getGame() {
|
|
|
|
return game;
|
2024-05-08 15:57:00 +02:00
|
|
|
}
|
2024-06-17 10:48:31 +02:00
|
|
|
|
|
|
|
void handleTap(int row, int col){
|
|
|
|
if(sl1 == null){
|
|
|
|
sl1 = StoneLocation(row: row, column: col);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
game.swapStones(sl1!, StoneLocation(row: row, column: col));
|
|
|
|
sl1 = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|