203 lines
5.6 KiB
Dart
203 lines
5.6 KiB
Dart
import 'package:bubbletwist/enums/StoneColor.dart';
|
|
import 'package:bubbletwist/game/Game.dart';
|
|
import 'package:bubbletwist/game/IGameConsumer.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'game/stone/Stone.dart';
|
|
import 'game/stone/StoneLocation.dart';
|
|
|
|
void main() {
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
final int gridSize = 8;
|
|
|
|
// This widget is the root of your application.
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Bubble-Twist',
|
|
theme: ThemeData(
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
useMaterial3: true,
|
|
),
|
|
home: const MyHomePage(title: 'Flutter Demo Home Page', gridSize: 8),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
const MyHomePage({super.key, required this.title, required this.gridSize});
|
|
|
|
// 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;
|
|
final int gridSize;
|
|
|
|
@override
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
}
|
|
|
|
class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
|
|
late List<List<Stone?>> _grid;
|
|
late Game game;
|
|
int _score = 0;
|
|
int _time = 0;
|
|
StoneLocation? sl1;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
game = Game(this);
|
|
_grid = List.generate(widget.gridSize,
|
|
(index) => List.generate(widget.gridSize, (index) => Stone()));
|
|
game.start();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.title),
|
|
actions: [
|
|
Padding(
|
|
padding: EdgeInsets.only(right: 20.0),
|
|
child: Text("Time: $_time s"),
|
|
),
|
|
],
|
|
),
|
|
body: Center(
|
|
child: Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text(
|
|
'Score: $_score',
|
|
style: const TextStyle(fontSize: 24),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: GridView.builder(
|
|
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;
|
|
StoneLocation location = StoneLocation(row: row, column: col);
|
|
Stone? stone = game.getStone(location);
|
|
|
|
// Überprüfen, ob der Stein in sl1 gespeichert ist
|
|
bool isSelected = (sl1 != null && sl1!.row == row && sl1!.column == col);
|
|
|
|
return GestureDetector(
|
|
onTap: () {
|
|
setState(() {
|
|
handleTap(row, col);
|
|
});
|
|
},
|
|
child: Container(
|
|
margin: const EdgeInsets.all(2),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(8),
|
|
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),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Color _getColorForStone(Stone? stone) {
|
|
// Beispielhafte Farben basierend auf dem Stein.
|
|
switch (stone?.getStoneColor()) {
|
|
case StoneColors.red:
|
|
return Colors.red;
|
|
case StoneColors.green:
|
|
return Colors.green;
|
|
case StoneColors.blue:
|
|
return Colors.blue;
|
|
case StoneColors.yellow:
|
|
return Colors.yellow;
|
|
case StoneColors.pink:
|
|
return Colors.pink;
|
|
default:
|
|
return Colors.grey;
|
|
}
|
|
}
|
|
|
|
String _getStoneColorText(Stone? stone) {
|
|
if (stone == null) return 'Unknown';
|
|
return stone.getStoneColor().toString().split('.').last;
|
|
}
|
|
|
|
@override
|
|
void gameStopped() {
|
|
// TODO: implement gameStopped
|
|
}
|
|
|
|
@override
|
|
void updatePoints() {
|
|
_score = game.getPoints();
|
|
}
|
|
|
|
@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));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void updateTime() {
|
|
setState(() {
|
|
_time = game.getTimeInSeconds();
|
|
});
|
|
}
|
|
|
|
Game getGame() {
|
|
return game;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |