handle tapping on previously played tiles
This commit is contained in:
parent
8dba377266
commit
3821c555b1
|
|
@ -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,16 +114,24 @@ class _TTCGameState extends State<TTCGame> {
|
||||||
late List<TTCState> data;
|
late List<TTCState> data;
|
||||||
|
|
||||||
void setCellState(int index) {
|
void setCellState(int index) {
|
||||||
setState(() {
|
switch (data[index]) {
|
||||||
data[index] = turn;
|
case TTCState.empty:
|
||||||
|
setState(() {
|
||||||
|
data[index] = turn;
|
||||||
|
|
||||||
turn = switch (turn) {
|
turn = switch (turn) {
|
||||||
TTCState.x => TTCState.o,
|
TTCState.x => TTCState.o,
|
||||||
TTCState.o => TTCState.x,
|
TTCState.o => TTCState.x,
|
||||||
TTCState.empty => TTCState.empty,
|
TTCState.empty => TTCState.empty,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
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(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue