From 3821c555b1408fa95e3efb3540f45350f41bf5fd Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Fri, 6 Dec 2024 21:31:56 -0500 Subject: [PATCH] handle tapping on previously played tiles --- lib/main.dart | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 8f9251f..ad06f84 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -78,7 +78,10 @@ class _MyHomePageState extends State { 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? data; - /// hook to retrieve the turn state of the game + /// hook into acction of tapping on square + final Function? onClick; @override State createState() => _TTCGameState(); @@ -111,16 +114,24 @@ class _TTCGameState extends State { late List 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(