363 lines
10 KiB
Dart
363 lines
10 KiB
Dart
import 'package:bubbletwist/enums/stone_color.dart';
|
|
import 'package:bubbletwist/game/game.dart';
|
|
import 'package:bubbletwist/game/i_game_consumer.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'game/stone/stone.dart';
|
|
import 'game/stone/stone_location.dart';
|
|
|
|
void main() {
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
final int gridSize = 8;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Bubble-Twist',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: ThemeData(
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
useMaterial3: true,
|
|
),
|
|
home: const MyHomePage(title: 'Bubble-Twist', gridSize: 8),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
const MyHomePage({super.key, required this.title, required this.gridSize});
|
|
|
|
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;
|
|
bool _isGameOver = false;
|
|
bool _isScoreboardVisible = false;
|
|
String _playerName = "";
|
|
List<Map<String, dynamic>> _scoreboard = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
game = Game(this);
|
|
_grid = List.generate(widget.gridSize,
|
|
(index) => List.generate(widget.gridSize, (index) => Stone()));
|
|
_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;
|
|
});
|
|
}
|
|
|
|
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;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.title),
|
|
actions: [
|
|
Padding(
|
|
padding: const EdgeInsets.only(right: 20.0),
|
|
child: Text("Time: $_time s"),
|
|
),
|
|
],
|
|
),
|
|
body: Center(
|
|
child: _isGameOver
|
|
? _isScoreboardVisible
|
|
? _buildScoreboard()
|
|
: _buildGameOverMessage()
|
|
: game.running
|
|
? _buildGameGrid()
|
|
: _buildStartButton(),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildStartButton() {
|
|
return ElevatedButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
game.start();
|
|
});
|
|
},
|
|
child: const Text("Start"),
|
|
);
|
|
}
|
|
|
|
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),
|
|
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),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_isScoreboardVisible = false;
|
|
_isGameOver = false;
|
|
game.start();
|
|
});
|
|
},
|
|
child: const Text('Restart'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
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,
|
|
),
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
const Spacer(),
|
|
],
|
|
);
|
|
}
|
|
|
|
Color _getColorForStone(Stone? stone) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void gameStopped() {
|
|
setState(() {
|
|
_isGameOver = true;
|
|
});
|
|
}
|
|
|
|
@override
|
|
void updatePoints() {
|
|
_score = game.getPoints();
|
|
}
|
|
|
|
@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));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@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;
|
|
}
|
|
}
|
|
}
|