From 7cf6ab5f4d120eb6bc10b906a414a718759380bd Mon Sep 17 00:00:00 2001 From: Lachfrosch Date: Mon, 17 Jun 2024 10:48:31 +0200 Subject: [PATCH] Fixi Fix This version is working but I still have to add the Special Stones --- lib/game/Board.dart | 20 +++++--------- lib/game/Game.dart | 19 +++++--------- lib/game/stone/Stone.dart | 2 +- lib/main.dart | 55 ++++++++++++++++++++++++++++++++++----- 4 files changed, 64 insertions(+), 32 deletions(-) diff --git a/lib/game/Board.dart b/lib/game/Board.dart index bbd9c24..85aa161 100644 --- a/lib/game/Board.dart +++ b/lib/game/Board.dart @@ -11,18 +11,12 @@ import 'stone/StoneLocation.dart'; class Board { static const int boardSize = 8; final Game game; - late final List> stones = - List.filled(boardSize, List.filled(boardSize, Stone())); + late final List> stones = List.generate(boardSize, (i) => List.generate(boardSize, (j) => Stone())); 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. // To fix that we just let the game remove all 3+ stone pairs until none are left - //while (checkBoard()); + while (checkBoard()){} } void updateBoard() { game.updateBoard(); @@ -187,7 +181,7 @@ class Board { if (!game.isRunning()) { // If game is not running, just give the stones a new color stones[sl.row][sl.column]!.stoneColor = - StoneColors.values[Random().nextInt(StoneColors.values.length)]; + StoneColors.values[Random().nextInt(StoneColors.values.length -1)]; } else { // Otherwise mark stones as deleted 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 // Then generate new stones that rain from the sky) - for (var row in stones) { - for (var stone in row) { - if (stone == null) { - stone = Stone(); + for (int i = 0; i < boardSize; i++){ + for (int j = 0; j < boardSize; j++){ + if (stones[i][j] == null){ + stones[i][j] = Stone(); } } } diff --git a/lib/game/Game.dart b/lib/game/Game.dart index ab46144..4921864 100644 --- a/lib/game/Game.dart +++ b/lib/game/Game.dart @@ -41,19 +41,14 @@ class Game { void endGame() { running = false; - counterTimer?.cancel(); + counterTimer.cancel(); gameConsumer.gameStopped(); } - Future countDown() async { - do { - timeInSeconds--; - gameConsumer.updateTime(); - if (timeInSeconds > 0) { - await Future.delayed(Duration(seconds: 1)); - } - } while (timeInSeconds > 0); - if (running) { + void countDown() { + timeInSeconds--; + gameConsumer.updateTime(); + if (timeInSeconds == 0 && running) { endGame(); } } @@ -110,8 +105,8 @@ class Game { } bool performSpecialStone(StoneLocation sl) { - Stone? s = getStone(sl); - /*if (s is TriggerableSpecialStone) { + /*Stone? s = getStone(sl); + if (s is TriggerableSpecialStone) { s.setActivationLocation(sl); board.performSpecialStone(s, sl); return true; diff --git a/lib/game/stone/Stone.dart b/lib/game/stone/Stone.dart index b145b73..cbaa7e8 100644 --- a/lib/game/stone/Stone.dart +++ b/lib/game/stone/Stone.dart @@ -24,6 +24,6 @@ class Stone { /// method that sets a random color void setRandomColor() { stoneColor = - StoneColors.values[Random().nextInt(StoneColors.values.length)]; + StoneColors.values[Random().nextInt(StoneColors.values.length - 1)]; } } diff --git a/lib/main.dart b/lib/main.dart index 8d63c45..44a297f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -51,7 +51,9 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State implements IGameConsumer { late List> _grid; late Game game; - final int _score = 0; + int _score = 0; + int _time = 0; + StoneLocation? sl1; @override void initState() { @@ -67,6 +69,12 @@ class _MyHomePageState extends State implements IGameConsumer { 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( @@ -88,17 +96,32 @@ class _MyHomePageState extends State implements IGameConsumer { 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(() { - //game.handleTap(row, col); + handleTap(row, col); }); }, child: Container( margin: const EdgeInsets.all(2), decoration: BoxDecoration( 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 implements IGameConsumer { 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 @@ -134,7 +164,7 @@ class _MyHomePageState extends State implements IGameConsumer { @override void updatePoints() { - int _score = game.getPoints(); + _score = game.getPoints(); } @override @@ -151,10 +181,23 @@ class _MyHomePageState extends State implements IGameConsumer { @override void updateTime() { - // TODO: implement 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; + } + } +} \ No newline at end of file