2024-06-17 16:24:51 +02:00
|
|
|
import 'package:bubbletwist/enums/stone_color.dart';
|
|
|
|
import 'package:bubbletwist/game/game.dart';
|
|
|
|
import 'package:bubbletwist/game/i_game_consumer.dart';
|
2024-04-24 16:21:26 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2024-06-19 19:16:49 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2024-04-24 16:21:26 +02:00
|
|
|
|
2024-06-17 16:24:51 +02:00
|
|
|
import 'game/stone/stone.dart';
|
|
|
|
import 'game/stone/stone_location.dart';
|
2024-06-16 23:56:48 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
2024-05-08 15:57:00 +02:00
|
|
|
title: 'Bubble-Twist',
|
2024-06-19 18:52:46 +02:00
|
|
|
debugShowCheckedModeBanner: false,
|
2024-04-24 16:21:26 +02:00
|
|
|
theme: ThemeData(
|
|
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
|
|
useMaterial3: true,
|
|
|
|
),
|
2024-06-17 16:24:51 +02:00
|
|
|
home: const MyHomePage(title: 'Bubble-Twist', 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
|
|
|
|
|
|
|
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-06-19 18:52:46 +02:00
|
|
|
bool _isGameOver = false;
|
2024-06-19 19:16:49 +02:00
|
|
|
bool _isScoreboardVisible = false;
|
|
|
|
String _playerName = "";
|
|
|
|
List<Map<String, dynamic>> _scoreboard = [];
|
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()));
|
2024-06-19 19:16:49 +02:00
|
|
|
_loadScoreboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _loadScoreboard() async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
final List<Map<String, dynamic>> loadedScoreboard =
|
|
|
|
List<Map<String, dynamic>>.from(
|
|
|
|
(prefs.getStringList('scoreboard') ?? [])
|
|
|
|
.map((e) => Map<String, dynamic>.from(e as Map)));
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
_scoreboard = loadedScoreboard;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-19 22:08:49 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
if (game.isRunning()) {
|
|
|
|
game.endGame(true);
|
|
|
|
}
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:16:49 +02:00
|
|
|
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;
|
|
|
|
});
|
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(
|
2024-06-17 16:24:51 +02:00
|
|
|
padding: const EdgeInsets.only(right: 20.0),
|
2024-06-17 10:48:31 +02:00
|
|
|
child: Text("Time: $_time s"),
|
|
|
|
),
|
|
|
|
],
|
2024-04-24 16:21:26 +02:00
|
|
|
),
|
|
|
|
body: Center(
|
2024-06-19 18:52:46 +02:00
|
|
|
child: _isGameOver
|
2024-06-19 19:16:49 +02:00
|
|
|
? _isScoreboardVisible
|
|
|
|
? _buildScoreboard()
|
|
|
|
: _buildGameOverMessage()
|
2024-06-19 18:52:46 +02:00
|
|
|
: game.running
|
|
|
|
? _buildGameGrid()
|
|
|
|
: _buildStartButton(),
|
2024-06-19 14:20:09 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildStartButton() {
|
|
|
|
return ElevatedButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
game.start();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: const Text("Start"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-19 18:52:46 +02:00
|
|
|
Widget _buildGameOverMessage() {
|
|
|
|
return Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const Text(
|
|
|
|
'Game Over',
|
|
|
|
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
Text(
|
|
|
|
'Final Score: $_score',
|
|
|
|
style: const TextStyle(fontSize: 24),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 20),
|
2024-06-19 19:16:49 +02:00
|
|
|
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),
|
2024-06-19 18:52:46 +02:00
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
2024-06-19 19:16:49 +02:00
|
|
|
setState(() {
|
|
|
|
_isScoreboardVisible = false;
|
|
|
|
_isGameOver = false;
|
|
|
|
game.start();
|
|
|
|
});
|
2024-06-19 18:52:46 +02:00
|
|
|
},
|
|
|
|
child: const Text('Restart'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-19 14:20:09 +02:00
|
|
|
Widget _buildGameGrid() {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(
|
|
|
|
'Score: $_score',
|
|
|
|
style: const TextStyle(fontSize: 24),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: LayoutBuilder(
|
|
|
|
builder: (BuildContext context, BoxConstraints constraints) {
|
|
|
|
double gridSize = widget.gridSize * 104.0;
|
|
|
|
double maxGridSize =
|
|
|
|
constraints.maxWidth < constraints.maxHeight
|
|
|
|
? constraints.maxWidth
|
|
|
|
: constraints.maxHeight;
|
|
|
|
|
|
|
|
double adjustedGridSize =
|
|
|
|
gridSize > maxGridSize ? maxGridSize : gridSize;
|
|
|
|
|
|
|
|
return SizedBox(
|
|
|
|
width: adjustedGridSize,
|
|
|
|
height: adjustedGridSize,
|
|
|
|
child: GridView.builder(
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
crossAxisCount: widget.gridSize,
|
2024-06-16 12:18:59 +02:00
|
|
|
),
|
2024-06-19 14:20:09 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
bool isSelected = (sl1 != null &&
|
|
|
|
sl1!.row == row &&
|
|
|
|
sl1!.column == col);
|
|
|
|
|
|
|
|
return GestureDetector(
|
|
|
|
onTap: () {
|
|
|
|
setState(() {
|
|
|
|
handleTap(row, col);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-06-16 12:18:59 +02:00
|
|
|
),
|
2024-06-19 14:20:09 +02:00
|
|
|
),
|
2024-04-24 16:21:26 +02:00
|
|
|
),
|
2024-06-19 14:20:09 +02:00
|
|
|
const Spacer(),
|
|
|
|
],
|
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) {
|
|
|
|
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-16 23:56:48 +02:00
|
|
|
@override
|
|
|
|
void gameStopped() {
|
2024-06-19 18:52:46 +02:00
|
|
|
setState(() {
|
|
|
|
_isGameOver = true;
|
|
|
|
});
|
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(() {
|
|
|
|
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
|
|
|
|
2024-06-19 14:20:09 +02:00
|
|
|
void handleTap(int row, int col) {
|
|
|
|
if (sl1 == null) {
|
2024-06-19 23:40:31 +02:00
|
|
|
if (game.getStoneColor(StoneLocation(row: row, column: col)) ==
|
|
|
|
StoneColors.special) {
|
|
|
|
game.performSpecialStone(StoneLocation(row: row, column: col));
|
|
|
|
sl1 = null;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
sl1 = StoneLocation(row: row, column: col);
|
|
|
|
return;
|
|
|
|
}
|
2024-06-19 14:20:09 +02:00
|
|
|
} else {
|
2024-06-17 10:48:31 +02:00
|
|
|
game.swapStones(sl1!, StoneLocation(row: row, column: col));
|
|
|
|
sl1 = null;
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 14:20:09 +02:00
|
|
|
}
|