This version is working but I still have to add the Special Stones
main
Lachfrosch 2024-06-17 10:48:31 +02:00
parent 480dbdacc6
commit 7cf6ab5f4d
4 changed files with 64 additions and 32 deletions

View File

@ -11,18 +11,12 @@ import 'stone/StoneLocation.dart';
class Board { class Board {
static const int boardSize = 8; static const int boardSize = 8;
final Game game; final Game game;
late final List<List<Stone?>> stones = late final List<List<Stone?>> stones = List.generate(boardSize, (i) => List.generate(boardSize, (j) => Stone()));
List.filled(boardSize, List.filled(boardSize, Stone()));
Board(this.game) { Board(this.game) {
for (var row in stones) {
for (var stone in row) {
stone = Stone();
}
}
// The map generates with pairs of 3+ stones already in place. // The map generates with pairs of 3+ stones already in place.
// To fix that we just let the game remove all 3+ stone pairs until none are left // To fix that we just let the game remove all 3+ stone pairs until none are left
//while (checkBoard()); while (checkBoard()){}
} }
void updateBoard() { void updateBoard() {
game.updateBoard(); game.updateBoard();
@ -187,7 +181,7 @@ class Board {
if (!game.isRunning()) { if (!game.isRunning()) {
// If game is not running, just give the stones a new color // If game is not running, just give the stones a new color
stones[sl.row][sl.column]!.stoneColor = stones[sl.row][sl.column]!.stoneColor =
StoneColors.values[Random().nextInt(StoneColors.values.length)]; StoneColors.values[Random().nextInt(StoneColors.values.length -1)];
} else { } else {
// Otherwise mark stones as deleted // Otherwise mark stones as deleted
stones[sl.row][sl.column] = null; stones[sl.row][sl.column] = null;
@ -215,10 +209,10 @@ class Board {
stonesHaveFallenDown); // Continue doing so until all deleted Stones are at the top most position stonesHaveFallenDown); // Continue doing so until all deleted Stones are at the top most position
// Then generate new stones that rain from the sky) // Then generate new stones that rain from the sky)
for (var row in stones) { for (int i = 0; i < boardSize; i++){
for (var stone in row) { for (int j = 0; j < boardSize; j++){
if (stone == null) { if (stones[i][j] == null){
stone = Stone(); stones[i][j] = Stone();
} }
} }
} }

View File

@ -41,19 +41,14 @@ class Game {
void endGame() { void endGame() {
running = false; running = false;
counterTimer?.cancel(); counterTimer.cancel();
gameConsumer.gameStopped(); gameConsumer.gameStopped();
} }
Future<void> countDown() async { void countDown() {
do { timeInSeconds--;
timeInSeconds--; gameConsumer.updateTime();
gameConsumer.updateTime(); if (timeInSeconds == 0 && running) {
if (timeInSeconds > 0) {
await Future.delayed(Duration(seconds: 1));
}
} while (timeInSeconds > 0);
if (running) {
endGame(); endGame();
} }
} }
@ -110,8 +105,8 @@ class Game {
} }
bool performSpecialStone(StoneLocation sl) { bool performSpecialStone(StoneLocation sl) {
Stone? s = getStone(sl); /*Stone? s = getStone(sl);
/*if (s is TriggerableSpecialStone) { if (s is TriggerableSpecialStone) {
s.setActivationLocation(sl); s.setActivationLocation(sl);
board.performSpecialStone(s, sl); board.performSpecialStone(s, sl);
return true; return true;

View File

@ -24,6 +24,6 @@ class Stone {
/// method that sets a random color /// method that sets a random color
void setRandomColor() { void setRandomColor() {
stoneColor = stoneColor =
StoneColors.values[Random().nextInt(StoneColors.values.length)]; StoneColors.values[Random().nextInt(StoneColors.values.length - 1)];
} }
} }

View File

@ -51,7 +51,9 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> implements IGameConsumer { class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
late List<List<Stone?>> _grid; late List<List<Stone?>> _grid;
late Game game; late Game game;
final int _score = 0; int _score = 0;
int _time = 0;
StoneLocation? sl1;
@override @override
void initState() { void initState() {
@ -67,6 +69,12 @@ class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(widget.title), title: Text(widget.title),
actions: [
Padding(
padding: EdgeInsets.only(right: 20.0),
child: Text("Time: $_time s"),
),
],
), ),
body: Center( body: Center(
child: Column( child: Column(
@ -88,17 +96,32 @@ class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
int row = index ~/ widget.gridSize; int row = index ~/ widget.gridSize;
int col = index % widget.gridSize; int col = index % widget.gridSize;
StoneLocation location = StoneLocation(row: row, column: col); 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( return GestureDetector(
onTap: () { onTap: () {
setState(() { setState(() {
//game.handleTap(row, col); handleTap(row, col);
}); });
}, },
child: Container( child: Container(
margin: const EdgeInsets.all(2), margin: const EdgeInsets.all(2),
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
color: _getColorForStone(game.getStone(location)), 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),
),
), ),
), ),
); );
@ -122,11 +145,18 @@ class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
return Colors.blue; return Colors.blue;
case StoneColors.yellow: case StoneColors.yellow:
return Colors.yellow; return Colors.yellow;
case StoneColors.pink:
return Colors.pink;
default: default:
return Colors.grey; return Colors.grey;
} }
} }
String _getStoneColorText(Stone? stone) {
if (stone == null) return 'Unknown';
return stone.getStoneColor().toString().split('.').last;
}
@override @override
void gameStopped() { void gameStopped() {
// TODO: implement gameStopped // TODO: implement gameStopped
@ -134,7 +164,7 @@ class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
@override @override
void updatePoints() { void updatePoints() {
int _score = game.getPoints(); _score = game.getPoints();
} }
@override @override
@ -151,10 +181,23 @@ class _MyHomePageState extends State<MyHomePage> implements IGameConsumer {
@override @override
void updateTime() { void updateTime() {
// TODO: implement updateTime setState(() {
_time = game.getTimeInSeconds();
});
} }
Game getGame() { Game getGame() {
return game; 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;
}
}
} }