diff --git a/lib/classic.dart b/lib/classic.dart index b1ab5ae..dad63da 100644 --- a/lib/classic.dart +++ b/lib/classic.dart @@ -23,8 +23,8 @@ class _ClassicGameState extends State { TTCState.empty, TTCState.empty, ]; - bool ended = false; - TTCState? winner; + TTCState get winner => Util.checkWin(data); + bool get ended => winner != TTCState.empty; String get turnText => switch (turn) { TTCState.empty => "", @@ -32,13 +32,7 @@ class _ClassicGameState extends State { TTCState.o => "O", }; - void _nextTurn() { - turn = switch (turn) { - TTCState.x => TTCState.o, - TTCState.o => TTCState.x, - _ => TTCState.x - }; - } + void _nextTurn() => turn = Util.nextTurn(turn); Widget _invalidChoiceAlert(TTCState existingValue) { return Dialog( @@ -52,7 +46,7 @@ class _ClassicGameState extends State { "INVALID CHOICE", style: TextStyle(fontWeight: FontWeight.bold), ), - Text("${existingValue.name.toUpperCase()} already claimed that"), + Text("${Util.stateText(existingValue)} already claimed that"), ElevatedButton( onPressed: () => Navigator.pop(context), child: const Text("Ok")), @@ -80,10 +74,6 @@ class _ClassicGameState extends State { setState(() { data[index] = turn; - winner = Util.checkWin(data); - if (winner != null && winner != TTCState.empty) { - ended = true; - } _nextTurn(); }); } @@ -103,7 +93,7 @@ class _ClassicGameState extends State { "GAME OVER", style: TextStyle(fontWeight: FontWeight.bold), ), - Text("${winner?.name.toUpperCase()} has already won"), + Text("${Util.stateText(winner)} has already won"), ElevatedButton( onPressed: () => Navigator.pop(context), child: const Text("Ok")), @@ -122,7 +112,7 @@ class _ClassicGameState extends State { const Spacer(flex: 5), Center( child: Text( - !ended ? "$turnText's turn" : "${winner?.name.toUpperCase()} wins", + !ended ? "$turnText's turn" : "${Util.stateText(winner)} wins", style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold), )), const Spacer(flex: 1), diff --git a/lib/super.dart b/lib/super.dart index bc6e084..9d8331b 100644 --- a/lib/super.dart +++ b/lib/super.dart @@ -13,47 +13,16 @@ class SuperGame extends StatefulWidget { class _SuperGameState extends State { TTCState turn = TTCState.x; List> data = Util.emptyBoardSuper; - TTCState winner = TTCState.empty; - bool gameEnded() => winner != TTCState.empty; - List subGameWinners = [ - TTCState.empty, - TTCState.empty, - TTCState.empty, - TTCState.empty, - TTCState.empty, - TTCState.empty, - TTCState.empty, - TTCState.empty, - TTCState.empty, - ]; - bool subGameEnded(int i) => subGameWinners[i] != TTCState.empty; + TTCState subGameWinner(int index) => Util.checkWin(data[index]); + List get subGameWinners => data.map(Util.checkWin).toList(); + bool subGameEnded(int i) => subGameWinner(i) != TTCState.empty; + TTCState get winner => Util.checkWin(subGameWinners); + bool gameEnded() => winner != TTCState.empty; int nextPlay = -1; - void _swapTurn() { - switch (turn) { - case TTCState.x: - turn = TTCState.o; - break; - case TTCState.o: - turn = TTCState.x; - break; - default: - turn = TTCState.x; - } - } - - TTCState _validateSubGame(int index) { - subGameWinners[index] = Util.checkWin(data[index]); - print("validated $index"); - return subGameWinners[index]; - } - - TTCState _validateGame() { - winner = Util.checkWin(subGameWinners); - return winner; - } + void _swapTurn() => turn = Util.nextTurn(turn); bool _checkValidChoice(List game, int index) => game[index] == TTCState.empty; @@ -78,8 +47,6 @@ class _SuperGameState extends State { setState(() { data[subGame][i] = turn; - _validateSubGame(subGame); - _validateGame(); nextPlay = subGameEnded(i) ? -1 : i; if (!gameEnded()) { _swapTurn(); @@ -132,7 +99,7 @@ class _SuperGameState extends State { ..clearSnackBars() ..showSnackBar( SnackBar( - content: Text("${Util.stateText(subGameWinners[index])}" + content: Text("${Util.stateText(subGameWinner(index))}" " already won the game at " "[${Util.cellAddress(index)}]"), ), @@ -180,47 +147,50 @@ class _SuperGameState extends State { _showSubGameDialog(index); } + Widget _turnText() => Text( + gameEnded() + ? "${Util.stateText(winner)} Wins" + : "${Util.stateText(turn)}'s Turn", + style: const TextStyle(fontSize: 25), + ); + + Widget _subGmaeWidget(int i) { + if (subGameEnded(i)) { + return Text( + subGameWinner(i).name.toUpperCase(), + style: const TextStyle(fontSize: 40), + ); + } + return TTCGame(turn: turn, data: data[i]); + } + + List _generateCells() => Iterable.generate(data.length) + .map( + (i) => DecoratedBox( + decoration: BoxDecoration( + color: nextPlay == i ? Colors.lightGreenAccent : null, + ), + child: Padding( + padding: const EdgeInsets.all(15), + child: _subGmaeWidget(i), + ), + ), + ) + .toList(); + @override Widget build(BuildContext context) { - Iterable.generate(subGameWinners.length) - .map((i) => subGameEnded(i)) - .forEach(print); - return Column( children: [ const Spacer(flex: 5), Center( - child: Text( - gameEnded() - ? "${Util.stateText(winner)} Wins" - : "${Util.stateText(turn)}'s Turn", - style: const TextStyle(fontSize: 25), - ), + child: _turnText(), ), const Spacer(flex: 1), GameHash( - cellOnTapCallback: _cellOnTapCallback, - children: Iterable.generate(data.length) - .map( - (i) => DecoratedBox( - decoration: BoxDecoration( - color: nextPlay == i ? Colors.lightGreenAccent : null, - ), - child: Padding( - padding: const EdgeInsets.all(15), - child: !subGameEnded(i) - ? TTCGame( - turn: turn, - data: data[i], - ) - : Text( - subGameWinners[i].name.toUpperCase(), - style: const TextStyle(fontSize: 40), - ), - ), - ), - ) - .toList()), + cellOnTapCallback: _cellOnTapCallback, + children: _generateCells(), + ), const Spacer(flex: 5), ], ); diff --git a/lib/util.dart b/lib/util.dart index edaf5e1..aefa59d 100644 --- a/lib/util.dart +++ b/lib/util.dart @@ -25,6 +25,18 @@ class Util { emptyBoardClassic, ]; + static TTCState nextTurn(TTCState currentPlayer, + {TTCState defaultState = TTCState.x}) { + switch (currentPlayer) { + case TTCState.x: + return TTCState.o; + case TTCState.o: + return TTCState.x; + default: + return defaultState; + } + } + static String stateText(TTCState state) => state.name.toUpperCase(); static String cellAddress(int index) => "${index % 3}, ${(index / 3).floor()}";