3 Commits

Author SHA1 Message Date
7f6131e773 validate subgame 2024-12-28 19:54:07 -05:00
25483be0ae invalid choice dialog 2024-12-27 22:22:40 -05:00
dbd492969a valid choice 2024-12-27 21:32:06 -05:00
2 changed files with 87 additions and 36 deletions

View File

@@ -13,17 +13,6 @@ 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;
List<bool> subGameEnded = [
false,
false,
false,
false,
false,
false,
false,
false,
false,
];
List<TTCState> subGameWinners = [ List<TTCState> subGameWinners = [
TTCState.empty, TTCState.empty,
@@ -36,6 +25,8 @@ class _SuperGameState extends State<SuperGame> {
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() {
@@ -51,6 +42,42 @@ class _SuperGameState extends State<SuperGame> {
} }
} }
TTCState _validateSubGame(int index) {
subGameWinners[index] = Util.checkWin(data[index]);
return subGameWinners[index];
}
bool _checkValidChoice(List<TTCState> game, int index) =>
game[index] == TTCState.empty;
void Function(int) subGameCellOnTapCallback(int subGame) {
return (int i) {
if (!_checkValidChoice(data[subGame], i)) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text("${data[subGame][i].name.toUpperCase()}"
" already claimed "
"[${i % 3}, ${(i / 3).floor()}]"),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text("Close"))
],
));
return;
}
nextPlay = subGameEnded(i) ? -1 : i;
setState(() {
data[subGame][i] = turn;
_validateSubGame(subGame);
_swapTurn();
});
Navigator.pop(context);
};
}
Widget _subGameDialog(int subGame) { Widget _subGameDialog(int subGame) {
return Dialog( return Dialog(
child: Padding( child: Padding(
@@ -58,23 +85,21 @@ class _SuperGameState extends State<SuperGame> {
child: Column( child: Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
Text( Padding(
padding: const EdgeInsets.only(bottom: 5),
child: Text(
"${Util.stateText(turn)} select cell", "${Util.stateText(turn)} select cell",
style: const TextStyle(fontWeight: FontWeight.bold), style: const TextStyle(fontWeight: FontWeight.bold),
), ),
),
TTCGame( TTCGame(
turn: turn, turn: turn,
cellOnTapCallback: (int i) { cellOnTapCallback: subGameCellOnTapCallback(subGame),
nextPlay = i;
setState(() {
data[subGame][i] = turn;
_swapTurn();
});
Navigator.pop(context);
},
data: data[subGame], data: data[subGame],
), ),
Row( Padding(
padding: const EdgeInsets.only(top: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.end,
children: [ children: [
ElevatedButton( ElevatedButton(
@@ -84,12 +109,25 @@ class _SuperGameState extends State<SuperGame> {
child: const Text("Close")) child: const Text("Close"))
], ],
), ),
),
], ],
), ),
), ),
); );
} }
void endedSubGameNotification(int index) {
ScaffoldMessenger.of(context)
..clearSnackBars()
..showSnackBar(
SnackBar(
content: Text("${Util.stateText(subGameWinners[index])}"
" already won the game at "
"[${Util.cellAddress(index)}]"),
),
);
}
void _showSubGameDialog(int i) { void _showSubGameDialog(int i) {
if (nextPlay == i || nextPlay == -1) { if (nextPlay == i || nextPlay == -1) {
showDialog( showDialog(
@@ -112,6 +150,10 @@ 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),
@@ -123,7 +165,9 @@ class _SuperGameState extends State<SuperGame> {
), ),
const Spacer(flex: 1), const Spacer(flex: 1),
GameHash( GameHash(
cellOnTapCallback: _showSubGameDialog, cellOnTapCallback: (i) => subGameEnded(i)
? endedSubGameNotification(i)
: _showSubGameDialog(i),
children: Iterable.generate(data.length) children: Iterable.generate(data.length)
.map( .map(
(i) => DecoratedBox( (i) => DecoratedBox(
@@ -132,9 +176,14 @@ class _SuperGameState extends State<SuperGame> {
), ),
child: Padding( child: Padding(
padding: const EdgeInsets.all(15), padding: const EdgeInsets.all(15),
child: TTCGame( child: !subGameEnded(i)
? TTCGame(
turn: turn, turn: turn,
data: data[i], data: data[i],
)
: Text(
subGameWinners[i].name.toUpperCase(),
style: const TextStyle(fontSize: 40),
), ),
), ),
), ),

View File

@@ -26,6 +26,8 @@ class Util {
]; ];
static String stateText(TTCState state) => state.name.toUpperCase(); static String stateText(TTCState state) => state.name.toUpperCase();
static String cellAddress(int index) =>
"${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, index + 3);