cleanup of super game rendering

This commit is contained in:
Andrei Stoica 2024-12-30 12:32:52 -05:00
parent e9362706a1
commit a174ffc7c2
1 changed files with 8 additions and 45 deletions

View File

@ -13,47 +13,16 @@ class SuperGame extends StatefulWidget {
class _SuperGameState extends State<SuperGame> { class _SuperGameState extends State<SuperGame> {
TTCState turn = TTCState.x; TTCState turn = TTCState.x;
List<List<TTCState>> data = Util.emptyBoardSuper; List<List<TTCState>> data = Util.emptyBoardSuper;
TTCState winner = TTCState.empty;
bool gameEnded() => winner != TTCState.empty;
List<TTCState> subGameWinners = [ TTCState subGameWinner(int index) => Util.checkWin(data[index]);
TTCState.empty, List<TTCState> get subGameWinners => data.map(Util.checkWin).toList();
TTCState.empty, bool subGameEnded(int i) => subGameWinner(i) != TTCState.empty;
TTCState.empty, TTCState get winner => Util.checkWin(subGameWinners);
TTCState.empty, bool gameEnded() => winner != TTCState.empty;
TTCState.empty,
TTCState.empty,
TTCState.empty,
TTCState.empty,
TTCState.empty,
];
bool subGameEnded(int i) => subGameWinners[i] != TTCState.empty;
int nextPlay = -1; int nextPlay = -1;
void _swapTurn() { void _swapTurn() => turn = Util.nextTurn(turn);
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;
}
bool _checkValidChoice(List<TTCState> game, int index) => bool _checkValidChoice(List<TTCState> game, int index) =>
game[index] == TTCState.empty; game[index] == TTCState.empty;
@ -78,8 +47,6 @@ class _SuperGameState extends State<SuperGame> {
setState(() { setState(() {
data[subGame][i] = turn; data[subGame][i] = turn;
_validateSubGame(subGame);
_validateGame();
nextPlay = subGameEnded(i) ? -1 : i; nextPlay = subGameEnded(i) ? -1 : i;
if (!gameEnded()) { if (!gameEnded()) {
_swapTurn(); _swapTurn();
@ -132,7 +99,7 @@ class _SuperGameState extends State<SuperGame> {
..clearSnackBars() ..clearSnackBars()
..showSnackBar( ..showSnackBar(
SnackBar( SnackBar(
content: Text("${Util.stateText(subGameWinners[index])}" content: Text("${Util.stateText(subGameWinner(index))}"
" already won the game at " " already won the game at "
"[${Util.cellAddress(index)}]"), "[${Util.cellAddress(index)}]"),
), ),
@ -182,10 +149,6 @@ class _SuperGameState extends State<SuperGame> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Iterable.generate(subGameWinners.length)
.map((i) => subGameEnded(i))
.forEach(print);
return Column( return Column(
children: [ children: [
const Spacer(flex: 5), const Spacer(flex: 5),
@ -214,7 +177,7 @@ class _SuperGameState extends State<SuperGame> {
data: data[i], data: data[i],
) )
: Text( : Text(
subGameWinners[i].name.toUpperCase(), subGameWinner(i).name.toUpperCase(),
style: const TextStyle(fontSize: 40), style: const TextStyle(fontSize: 40),
), ),
), ),