removing state from TTCGame
This commit is contained in:
parent
a9df9ab96b
commit
6caec73c0d
|
|
@ -11,7 +11,17 @@ class ClassicGame extends StatefulWidget {
|
|||
|
||||
class _ClassicGameState extends State<ClassicGame> {
|
||||
TTCState turn = TTCState.x;
|
||||
late TTCGame game;
|
||||
List<TTCState> data = [
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
];
|
||||
|
||||
String get turnText => switch (turn) {
|
||||
TTCState.empty => "",
|
||||
|
|
@ -19,25 +29,58 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||
TTCState.o => "O",
|
||||
};
|
||||
|
||||
void onTurnEnd() {
|
||||
setState(() {
|
||||
turn = switch (turn) {
|
||||
TTCState.x => TTCState.o,
|
||||
TTCState.o => TTCState.x,
|
||||
_ => TTCState.x
|
||||
};
|
||||
});
|
||||
void _nextTurn() {
|
||||
turn = switch (turn) {
|
||||
TTCState.x => TTCState.o,
|
||||
TTCState.o => TTCState.x,
|
||||
_ => TTCState.x
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
game = TTCGame(
|
||||
turn: turn,
|
||||
onClick: onTurnEnd,
|
||||
Widget _invalidChoiceAlert(TTCState existingValue) {
|
||||
return Dialog(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
const Text(
|
||||
"INVALID CHOICE",
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text("${existingValue.name.toUpperCase()} already claimed that"),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text("Ok")),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool _checkValidChoice(int index) {
|
||||
if (data[index] == TTCState.empty) return true;
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => _invalidChoiceAlert(data[index]),
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void _cellOnTap(int index) {
|
||||
if (!_checkValidChoice(index)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
data[index] = turn;
|
||||
_nextTurn();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
|
|
@ -52,7 +95,9 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||
const Spacer(flex: 1),
|
||||
TTCGame(
|
||||
turn: turn,
|
||||
onClick: onTurnEnd,
|
||||
cellOnTapCallback: _cellOnTap,
|
||||
data: data,
|
||||
cellTextStyle: const TextStyle(fontSize: 40),
|
||||
),
|
||||
const Spacer(flex: 5),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -1,63 +1,24 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'state.dart';
|
||||
import 'util.dart';
|
||||
|
||||
/// Board of a single game of tic tac toe
|
||||
class TTCGame extends StatefulWidget {
|
||||
class TTCGame extends StatelessWidget {
|
||||
const TTCGame({
|
||||
super.key,
|
||||
required this.turn,
|
||||
this.data,
|
||||
this.onClick,
|
||||
required this.cellOnTapCallback,
|
||||
required this.data,
|
||||
this.cellTextStyle,
|
||||
});
|
||||
|
||||
final TTCState turn;
|
||||
final List<TTCState>? data;
|
||||
final List<TTCState> data;
|
||||
|
||||
/// hook into acction of tapping on square
|
||||
final Function? onClick;
|
||||
/// styling for text in cell
|
||||
final TextStyle? cellTextStyle;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _TTCGameState();
|
||||
}
|
||||
|
||||
class _TTCGameState extends State<TTCGame> {
|
||||
late TTCState turn;
|
||||
late List<TTCState> data;
|
||||
|
||||
void setCellState(int index) {
|
||||
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();
|
||||
notifyWin();
|
||||
break;
|
||||
default:
|
||||
ScaffoldMessenger.of(context)
|
||||
..clearSnackBars()
|
||||
..showSnackBar(const SnackBar(
|
||||
content: Text("Invalid Choice: cell already has value")));
|
||||
}
|
||||
}
|
||||
|
||||
void notifyWin() {
|
||||
TTCState state = Util.checkWin(data);
|
||||
|
||||
if (state != TTCState.empty) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..clearSnackBars()
|
||||
..showSnackBar(
|
||||
SnackBar(content: Text("${state.name.toUpperCase()} wins")));
|
||||
}
|
||||
}
|
||||
/// hook into end of turn cycle;
|
||||
final Function cellOnTapCallback;
|
||||
|
||||
Border _genCellBorder(
|
||||
int index, {
|
||||
|
|
@ -75,29 +36,12 @@ class _TTCGameState extends State<TTCGame> {
|
|||
decoration: BoxDecoration(border: _genCellBorder(index)),
|
||||
child: TTCCell(
|
||||
state: state,
|
||||
textStyle: cellTextStyle,
|
||||
stateSetCallback: () {
|
||||
setCellState(index);
|
||||
cellOnTapCallback(index);
|
||||
},
|
||||
));
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
turn = widget.turn;
|
||||
data = widget.data ??
|
||||
<TTCState>[
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<Widget> cells = [];
|
||||
|
|
|
|||
Loading…
Reference in New Issue