Compare commits

..

2 Commits
wip ... main

Author SHA1 Message Date
Andrei Stoica fedfbb3dbc game validation logic bugfix 2024-12-29 23:56:42 -05:00
Andrei Stoica ef96a6623e supergame ui 2024-12-29 23:56:07 -05:00
3 changed files with 40 additions and 9 deletions

View File

@ -13,6 +13,8 @@ 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 = [ List<TTCState> subGameWinners = [
TTCState.empty, TTCState.empty,
@ -44,9 +46,15 @@ class _SuperGameState extends State<SuperGame> {
TTCState _validateSubGame(int index) { TTCState _validateSubGame(int index) {
subGameWinners[index] = Util.checkWin(data[index]); subGameWinners[index] = Util.checkWin(data[index]);
print("validated $index");
return subGameWinners[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;
@ -68,11 +76,14 @@ class _SuperGameState extends State<SuperGame> {
return; return;
} }
nextPlay = subGameEnded(i) ? -1 : i;
setState(() { setState(() {
data[subGame][i] = turn; data[subGame][i] = turn;
_validateSubGame(subGame); _validateSubGame(subGame);
_validateGame();
nextPlay = subGameEnded(i) ? -1 : i;
if (!gameEnded()) {
_swapTurn(); _swapTurn();
}
}); });
Navigator.pop(context); Navigator.pop(context);
}; };
@ -148,6 +159,27 @@ class _SuperGameState extends State<SuperGame> {
} }
} }
void _gameEndedReminder() {
ScaffoldMessenger.of(context)
..clearSnackBars()
..showSnackBar(
SnackBar(
content: Text("${Util.stateText(winner)} already won the game"),
),
);
}
void _cellOnTapCallback(int index) {
if (subGameEnded(index)) {
endedSubGameNotification(index);
return;
} else if (gameEnded()) {
_gameEndedReminder();
return;
}
_showSubGameDialog(index);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Iterable.generate(subGameWinners.length) Iterable.generate(subGameWinners.length)
@ -159,15 +191,15 @@ class _SuperGameState extends State<SuperGame> {
const Spacer(flex: 5), const Spacer(flex: 5),
Center( Center(
child: Text( child: Text(
"${Util.stateText(turn)}'s Turn", gameEnded()
? "${Util.stateText(winner)} Wins"
: "${Util.stateText(turn)}'s Turn",
style: const TextStyle(fontSize: 25), style: const TextStyle(fontSize: 25),
), ),
), ),
const Spacer(flex: 1), const Spacer(flex: 1),
GameHash( GameHash(
cellOnTapCallback: (i) => subGameEnded(i) cellOnTapCallback: _cellOnTapCallback,
? endedSubGameNotification(i)
: _showSubGameDialog(i),
children: Iterable.generate(data.length) children: Iterable.generate(data.length)
.map( .map(
(i) => DecoratedBox( (i) => DecoratedBox(

View File

@ -30,7 +30,7 @@ class Util {
"${index % 3}, ${(index / 3).floor()}"; "${index % 3}, ${(index / 3).floor()}";
static Iterable<TTCState> getRow(int index, List<TTCState> data) => static Iterable<TTCState> getRow(int index, List<TTCState> data) =>
data.getRange(index, index + 3); data.getRange(index * 3, index * 3 + 3);
static Iterable<TTCState> getCol(int index, List<TTCState> data) => [ static Iterable<TTCState> getCol(int index, List<TTCState> data) => [
data[index], data[index],
data[index + 3], data[index + 3],
@ -40,7 +40,7 @@ class Util {
if (index == 0) { if (index == 0) {
return [data[0], data[4], data[8]]; return [data[0], data[4], data[8]];
} }
return [data[3], data[4], data[6]]; return [data[2], data[4], data[6]];
} }
static TTCState checkRow(Iterable<TTCState> row) => static TTCState checkRow(Iterable<TTCState> row) =>

1
todo
View File

@ -1,2 +1 @@
- ui for super game
- firebase for multiplayer game - firebase for multiplayer game