super_tik_tac_toe/lib/super.dart

148 lines
3.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:super_tic_tac_toe/game.dart';
import 'package:super_tic_tac_toe/state.dart';
import 'package:super_tic_tac_toe/util.dart';
class SuperGame extends StatefulWidget {
const SuperGame({super.key});
@override
State<SuperGame> createState() => _SuperGameState();
}
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,
TTCState.empty,
TTCState.empty,
TTCState.empty,
TTCState.empty,
TTCState.empty,
TTCState.empty,
TTCState.empty,
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;
}
}
Widget _subGameDialog(int subGame) {
return Dialog(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"${Util.stateText(turn)} select cell",
style: const TextStyle(fontWeight: FontWeight.bold),
),
TTCGame(
turn: turn,
cellOnTapCallback: (int i) {
nextPlay = i;
setState(() {
data[subGame][i] = turn;
_swapTurn();
});
Navigator.pop(context);
},
data: data[subGame],
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Close"))
],
),
],
),
),
);
}
void _showSubGameDialog(int i) {
if (nextPlay == i || nextPlay == -1) {
showDialog(
context: context,
builder: (context) => _subGameDialog(i),
);
} else {
ScaffoldMessenger.of(context)
..clearSnackBars()
..showSnackBar(
SnackBar(
content: Text(
"You can only play at [${(nextPlay % 3) + 1},"
"${(nextPlay / 3).floor() + 1}]",
),
),
);
}
}
@override
Widget build(BuildContext context) {
return Column(
children: [
const Spacer(flex: 5),
Center(
child: Text(
"${Util.stateText(turn)}'s Turn",
style: const TextStyle(fontSize: 25),
),
),
const Spacer(flex: 1),
GameHash(
cellOnTapCallback: _showSubGameDialog,
children: Iterable.generate(data.length)
.map(
(i) => DecoratedBox(
decoration: BoxDecoration(
color: nextPlay == i ? Colors.lightGreenAccent : null,
),
child: Padding(
padding: const EdgeInsets.all(15),
child: TTCGame(
turn: turn,
data: data[i],
),
),
),
)
.toList()),
const Spacer(flex: 5),
],
);
}
}