handle tapping on previously played tiles

This commit is contained in:
Andrei Stoica 2024-12-06 21:31:56 -05:00
parent 8dba377266
commit 3821c555b1
1 changed files with 23 additions and 12 deletions

View File

@ -78,7 +78,10 @@ class _MyHomePageState extends State<MyHomePage> {
const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
)),
const Spacer(flex: 1),
game,
TTCGame(
turn: turn,
onClick: onTurnEnd,
),
const Spacer(flex: 5),
],
)),
@ -97,10 +100,10 @@ class TTCGame extends StatefulWidget {
});
final TTCState turn;
final Function? onClick;
final List<TTCState>? data;
/// hook to retrieve the turn state of the game
/// hook into acction of tapping on square
final Function? onClick;
@override
State<StatefulWidget> createState() => _TTCGameState();
@ -111,16 +114,24 @@ class _TTCGameState extends State<TTCGame> {
late List<TTCState> data;
void setCellState(int index) {
setState(() {
data[index] = turn;
switch (data[index]) {
case TTCState.empty:
setState(() {
data[index] = turn;
turn = switch (turn) {
TTCState.x => TTCState.o,
TTCState.o => TTCState.x,
TTCState.empty => TTCState.empty,
};
});
widget.onClick?.call();
turn = switch (turn) {
TTCState.x => TTCState.o,
TTCState.o => TTCState.x,
TTCState.empty => TTCState.empty,
};
});
widget.onClick?.call();
break;
default:
ScaffoldMessenger.of(context)
..clearSnackBars()
..showSnackBar(const SnackBar(content: Text("Invalid Choice")));
}
}
Widget _genCell(int index, TTCState state) => Container(