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