diff --git a/lib/super.dart b/lib/super.dart index bc6e084..eccf32b 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)}]"), ), @@ -182,10 +149,6 @@ class _SuperGameState extends State { @override Widget build(BuildContext context) { - Iterable.generate(subGameWinners.length) - .map((i) => subGameEnded(i)) - .forEach(print); - return Column( children: [ const Spacer(flex: 5), @@ -214,7 +177,7 @@ class _SuperGameState extends State { data: data[i], ) : Text( - subGameWinners[i].name.toUpperCase(), + subGameWinner(i).name.toUpperCase(), style: const TextStyle(fontSize: 40), ), ),