validate subgame
This commit is contained in:
parent
25483be0ae
commit
7f6131e773
105
lib/super.dart
105
lib/super.dart
|
|
@ -13,17 +13,6 @@ class SuperGame extends StatefulWidget {
|
|||
class _SuperGameState extends State<SuperGame> {
|
||||
TTCState turn = TTCState.x;
|
||||
List<List<TTCState>> data = Util.emptyBoardSuper;
|
||||
List<bool> subGameEnded = [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
];
|
||||
|
||||
List<TTCState> subGameWinners = [
|
||||
TTCState.empty,
|
||||
|
|
@ -36,6 +25,8 @@ class _SuperGameState extends State<SuperGame> {
|
|||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
];
|
||||
bool subGameEnded(int i) => subGameWinners[i] != TTCState.empty;
|
||||
|
||||
int nextPlay = -1;
|
||||
|
||||
void _swapTurn() {
|
||||
|
|
@ -51,9 +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) {
|
||||
return Dialog(
|
||||
child: Padding(
|
||||
|
|
@ -70,31 +94,7 @@ class _SuperGameState extends State<SuperGame> {
|
|||
),
|
||||
TTCGame(
|
||||
turn: turn,
|
||||
cellOnTapCallback: (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 = i;
|
||||
setState(() {
|
||||
data[subGame][i] = turn;
|
||||
_swapTurn();
|
||||
});
|
||||
Navigator.pop(context);
|
||||
},
|
||||
cellOnTapCallback: subGameCellOnTapCallback(subGame),
|
||||
data: data[subGame],
|
||||
),
|
||||
Padding(
|
||||
|
|
@ -116,6 +116,18 @@ class _SuperGameState extends State<SuperGame> {
|
|||
);
|
||||
}
|
||||
|
||||
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) {
|
||||
if (nextPlay == i || nextPlay == -1) {
|
||||
showDialog(
|
||||
|
|
@ -138,6 +150,10 @@ class _SuperGameState extends State<SuperGame> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Iterable.generate(subGameWinners.length)
|
||||
.map((i) => subGameEnded(i))
|
||||
.forEach(print);
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
const Spacer(flex: 5),
|
||||
|
|
@ -149,7 +165,9 @@ class _SuperGameState extends State<SuperGame> {
|
|||
),
|
||||
const Spacer(flex: 1),
|
||||
GameHash(
|
||||
cellOnTapCallback: _showSubGameDialog,
|
||||
cellOnTapCallback: (i) => subGameEnded(i)
|
||||
? endedSubGameNotification(i)
|
||||
: _showSubGameDialog(i),
|
||||
children: Iterable.generate(data.length)
|
||||
.map(
|
||||
(i) => DecoratedBox(
|
||||
|
|
@ -158,10 +176,15 @@ class _SuperGameState extends State<SuperGame> {
|
|||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(15),
|
||||
child: TTCGame(
|
||||
turn: turn,
|
||||
data: data[i],
|
||||
),
|
||||
child: !subGameEnded(i)
|
||||
? TTCGame(
|
||||
turn: turn,
|
||||
data: data[i],
|
||||
)
|
||||
: Text(
|
||||
subGameWinners[i].name.toUpperCase(),
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ class Util {
|
|||
];
|
||||
|
||||
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) =>
|
||||
data.getRange(index, index + 3);
|
||||
|
|
|
|||
Loading…
Reference in New Issue