From c9a43f505c1f30c3525b28df88d6bc22ba71c5ad Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Sat, 21 Dec 2024 08:34:49 -0500 Subject: [PATCH] classic game cleanup --- lib/{clasic.dart => classic.dart} | 39 ++++++++++++++-- lib/game.dart | 75 ++++++++++++++++++------------- 2 files changed, 81 insertions(+), 33 deletions(-) rename lib/{clasic.dart => classic.dart} (65%) diff --git a/lib/clasic.dart b/lib/classic.dart similarity index 65% rename from lib/clasic.dart rename to lib/classic.dart index 93091a2..b1ab5ae 100644 --- a/lib/clasic.dart +++ b/lib/classic.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'state.dart'; import 'game.dart'; +import "util.dart"; class ClassicGame extends StatefulWidget { const ClassicGame({super.key}); @@ -22,6 +23,8 @@ class _ClassicGameState extends State { TTCState.empty, TTCState.empty, ]; + bool ended = false; + TTCState? winner; String get turnText => switch (turn) { TTCState.empty => "", @@ -40,7 +43,7 @@ class _ClassicGameState extends State { Widget _invalidChoiceAlert(TTCState existingValue) { return Dialog( child: Padding( - padding: EdgeInsets.all(10), + padding: const EdgeInsets.all(10), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, @@ -77,10 +80,40 @@ class _ClassicGameState extends State { setState(() { data[index] = turn; + winner = Util.checkWin(data); + if (winner != null && winner != TTCState.empty) { + ended = true; + } _nextTurn(); }); } + void _endedCellOnTap(int index) { + showDialog( + context: context, + builder: (context) { + return Dialog( + child: Padding( + padding: const EdgeInsets.all(10), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + const Text( + "GAME OVER", + style: TextStyle(fontWeight: FontWeight.bold), + ), + Text("${winner?.name.toUpperCase()} has already won"), + ElevatedButton( + onPressed: () => Navigator.pop(context), + child: const Text("Ok")), + ], + ), + ), + ); + }); + } + @override Widget build(BuildContext context) { return Column( @@ -89,13 +122,13 @@ class _ClassicGameState extends State { const Spacer(flex: 5), Center( child: Text( - "$turnText's turn", + !ended ? "$turnText's turn" : "${winner?.name.toUpperCase()} wins", style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold), )), const Spacer(flex: 1), TTCGame( turn: turn, - cellOnTapCallback: _cellOnTap, + cellOnTapCallback: !ended ? _cellOnTap : _endedCellOnTap, data: data, cellTextStyle: const TextStyle(fontSize: 40), ), diff --git a/lib/game.dart b/lib/game.dart index 6be5f97..c147434 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -20,6 +20,27 @@ class TTCGame extends StatelessWidget { /// hook into end of turn cycle; final Function cellOnTapCallback; + @override + Widget build(BuildContext context) { + List cells = data + .map((state) => Text( + state != TTCState.empty ? state.name.toUpperCase() : "", + style: cellTextStyle, + )) + .toList(); + return GameHash( + cellOnTapCallback: cellOnTapCallback, + children: cells, + ); + } +} + +class GameHash extends StatelessWidget { + const GameHash({super.key, required this.children, this.cellOnTapCallback}); + final List children; + + final Function? cellOnTapCallback; + Border _genCellBorder( int index, { BorderSide borderSide = const BorderSide(), @@ -32,52 +53,46 @@ class TTCGame extends StatelessWidget { ); } - Widget _genCell(int index, TTCState state) => Container( - decoration: BoxDecoration(border: _genCellBorder(index)), - child: TTCCell( - state: state, - textStyle: cellTextStyle, - stateSetCallback: () { - cellOnTapCallback(index); - }, - )); + MapEntry _genCell(int index, Widget content) { + return MapEntry( + index, + Container( + decoration: BoxDecoration(border: _genCellBorder(index)), + child: HashCell( + stateSetCallback: () { + if (cellOnTapCallback != null) { + cellOnTapCallback!(index); + } + }, + child: content, + ), + ), + ); + } @override Widget build(BuildContext context) { - List cells = []; - for (final (index, state) in data.indexed) { - cells.add(_genCell(index, state)); - } return GridView.count( crossAxisCount: 3, shrinkWrap: true, - children: cells, + children: children.asMap().map(_genCell).values.toList(), ); } } -class TTCCell extends StatelessWidget { - const TTCCell({ +class HashCell extends StatelessWidget { + const HashCell({ super.key, - required this.state, + required this.child, this.stateSetCallback, - this.textStyle, }); - final TTCState state; + final Widget child; final VoidCallback? stateSetCallback; - final TextStyle? textStyle; - - String get text => switch (state) { - TTCState.empty => "", - TTCState.x => "X", - TTCState.o => "O", - }; @override Widget build(BuildContext context) => InkWell( - onTap: stateSetCallback, - child: Center( - child: Text(text, style: textStyle), - )); + onTap: stateSetCallback, + child: Center(child: child), + ); }