From 412bb1a18c2780a8d12455246da3eaeb2a6563d7 Mon Sep 17 00:00:00 2001 From: Lachfrosch Date: Wed, 19 Jun 2024 14:20:09 +0200 Subject: [PATCH] Added Start button + minor UI changes --- lib/main.dart | 157 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 93 insertions(+), 64 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 9c5a0e6..9d30210 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -62,7 +62,6 @@ class _MyHomePageState extends State implements IGameConsumer { game = Game(this); _grid = List.generate(widget.gridSize, (index) => List.generate(widget.gridSize, (index) => Stone())); - game.start(); } @override @@ -78,63 +77,99 @@ class _MyHomePageState extends State implements IGameConsumer { ], ), 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), - ), - ), - ), - ); - }, - ), - ), - ], - ), + child: game.running ? _buildGameGrid() : _buildStartButton(), ), ); } + Widget _buildStartButton() { + return ElevatedButton( + onPressed: () { + setState(() { + game.start(); + }); + }, + child: const Text("Start"), + ); + } + + 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) { // Beispielhafte Farben basierend auf dem Stein. switch (stone?.getStoneColor()) { @@ -153,11 +188,6 @@ class _MyHomePageState extends State implements IGameConsumer { } } - String _getStoneColorText(Stone? stone) { - if (stone == null) return 'Unknown'; - return stone.getStoneColor().toString().split('.').last; - } - @override void gameStopped() { // TODO: implement gameStopped @@ -191,14 +221,13 @@ class _MyHomePageState extends State implements IGameConsumer { return game; } - void handleTap(int row, int col){ - if(sl1 == null){ + void handleTap(int row, int col) { + if (sl1 == null) { sl1 = StoneLocation(row: row, column: col); return; - } - else{ + } else { game.swapStones(sl1!, StoneLocation(row: row, column: col)); sl1 = null; } } -} \ No newline at end of file +}