Compare commits
4 Commits
astoica/pr
...
25483be0ae
| Author | SHA1 | Date | |
|---|---|---|---|
| 25483be0ae | |||
| dbd492969a | |||
| 03987378c0 | |||
| 79bf2b015d |
@@ -23,8 +23,8 @@ class _ClassicGameState extends State<ClassicGame> {
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
];
|
||||
TTCState get winner => Util.checkWin(data);
|
||||
bool get ended => winner != TTCState.empty;
|
||||
bool ended = false;
|
||||
TTCState? winner;
|
||||
|
||||
String get turnText => switch (turn) {
|
||||
TTCState.empty => "",
|
||||
@@ -32,7 +32,13 @@ class _ClassicGameState extends State<ClassicGame> {
|
||||
TTCState.o => "O",
|
||||
};
|
||||
|
||||
void _nextTurn() => turn = Util.nextTurn(turn);
|
||||
void _nextTurn() {
|
||||
turn = switch (turn) {
|
||||
TTCState.x => TTCState.o,
|
||||
TTCState.o => TTCState.x,
|
||||
_ => TTCState.x
|
||||
};
|
||||
}
|
||||
|
||||
Widget _invalidChoiceAlert(TTCState existingValue) {
|
||||
return Dialog(
|
||||
@@ -46,7 +52,7 @@ class _ClassicGameState extends State<ClassicGame> {
|
||||
"INVALID CHOICE",
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text("${Util.stateText(existingValue)} already claimed that"),
|
||||
Text("${existingValue.name.toUpperCase()} already claimed that"),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text("Ok")),
|
||||
@@ -74,6 +80,10 @@ class _ClassicGameState extends State<ClassicGame> {
|
||||
|
||||
setState(() {
|
||||
data[index] = turn;
|
||||
winner = Util.checkWin(data);
|
||||
if (winner != null && winner != TTCState.empty) {
|
||||
ended = true;
|
||||
}
|
||||
_nextTurn();
|
||||
});
|
||||
}
|
||||
@@ -93,7 +103,7 @@ class _ClassicGameState extends State<ClassicGame> {
|
||||
"GAME OVER",
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text("${Util.stateText(winner)} has already won"),
|
||||
Text("${winner?.name.toUpperCase()} has already won"),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text("Ok")),
|
||||
@@ -112,7 +122,7 @@ class _ClassicGameState extends State<ClassicGame> {
|
||||
const Spacer(flex: 5),
|
||||
Center(
|
||||
child: Text(
|
||||
!ended ? "$turnText's turn" : "${Util.stateText(winner)} wins",
|
||||
!ended ? "$turnText's turn" : "${winner?.name.toUpperCase()} wins",
|
||||
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
|
||||
)),
|
||||
const Spacer(flex: 1),
|
||||
|
||||
148
lib/super.dart
148
lib/super.dart
@@ -13,49 +13,47 @@ class SuperGame extends StatefulWidget {
|
||||
class _SuperGameState extends State<SuperGame> {
|
||||
TTCState turn = TTCState.x;
|
||||
List<List<TTCState>> data = Util.emptyBoardSuper;
|
||||
List<bool> subGameEnded = [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
];
|
||||
|
||||
TTCState subGameWinner(int index) => Util.checkWin(data[index]);
|
||||
List<TTCState> get subGameWinners => data.map(Util.checkWin).toList();
|
||||
bool subGameEnded(int i) => subGameWinner(i) != TTCState.empty;
|
||||
TTCState get winner => Util.checkWin(subGameWinners);
|
||||
bool gameEnded() => winner != TTCState.empty;
|
||||
|
||||
List<TTCState> subGameWinners = [
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
TTCState.empty,
|
||||
];
|
||||
int nextPlay = -1;
|
||||
|
||||
void _swapTurn() => turn = Util.nextTurn(turn);
|
||||
void _swapTurn() {
|
||||
switch (turn) {
|
||||
case TTCState.x:
|
||||
turn = TTCState.o;
|
||||
break;
|
||||
case TTCState.o:
|
||||
turn = TTCState.x;
|
||||
break;
|
||||
default:
|
||||
turn = TTCState.x;
|
||||
}
|
||||
}
|
||||
|
||||
bool _checkValidChoice(List<TTCState> game, int index) =>
|
||||
game[index] == TTCState.empty;
|
||||
|
||||
void Function(int) subGameCellOnTapCallback(int subGame) {
|
||||
return (int i) {
|
||||
if (!_checkValidChoice(data[subGame], i)) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
content: Text("${data[subGame][i].name.toUpperCase()}"
|
||||
" already claimed "
|
||||
"[${i % 3}, ${(i / 3).floor()}]"),
|
||||
actions: [
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text("Close"))
|
||||
],
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
data[subGame][i] = turn;
|
||||
nextPlay = subGameEnded(i) ? -1 : i;
|
||||
if (!gameEnded()) {
|
||||
_swapTurn();
|
||||
}
|
||||
});
|
||||
Navigator.pop(context);
|
||||
};
|
||||
}
|
||||
|
||||
Widget _subGameDialog(int subGame) {
|
||||
return Dialog(
|
||||
child: Padding(
|
||||
@@ -72,7 +70,31 @@ class _SuperGameState extends State<SuperGame> {
|
||||
),
|
||||
TTCGame(
|
||||
turn: turn,
|
||||
cellOnTapCallback: subGameCellOnTapCallback(subGame),
|
||||
cellOnTapCallback: (int i) {
|
||||
if (!_checkValidChoice(data[subGame], i)) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
content:
|
||||
Text("${data[subGame][i].name.toUpperCase()}"
|
||||
" already claimed "
|
||||
"[${i % 3}, ${(i / 3).floor()}]"),
|
||||
actions: [
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text("Close"))
|
||||
],
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
nextPlay = i;
|
||||
setState(() {
|
||||
data[subGame][i] = turn;
|
||||
_swapTurn();
|
||||
});
|
||||
Navigator.pop(context);
|
||||
},
|
||||
data: data[subGame],
|
||||
),
|
||||
Padding(
|
||||
@@ -94,18 +116,6 @@ class _SuperGameState extends State<SuperGame> {
|
||||
);
|
||||
}
|
||||
|
||||
void endedSubGameNotification(int index) {
|
||||
ScaffoldMessenger.of(context)
|
||||
..clearSnackBars()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("${Util.stateText(subGameWinner(index))}"
|
||||
" already won the game at "
|
||||
"[${Util.cellAddress(index)}]"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showSubGameDialog(int i) {
|
||||
if (nextPlay == i || nextPlay == -1) {
|
||||
showDialog(
|
||||
@@ -126,27 +136,6 @@ class _SuperGameState extends State<SuperGame> {
|
||||
}
|
||||
}
|
||||
|
||||
void _gameEndedReminder() {
|
||||
ScaffoldMessenger.of(context)
|
||||
..clearSnackBars()
|
||||
..showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("${Util.stateText(winner)} already won the game"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _cellOnTapCallback(int index) {
|
||||
if (subGameEnded(index)) {
|
||||
endedSubGameNotification(index);
|
||||
return;
|
||||
} else if (gameEnded()) {
|
||||
_gameEndedReminder();
|
||||
return;
|
||||
}
|
||||
_showSubGameDialog(index);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
@@ -154,15 +143,13 @@ class _SuperGameState extends State<SuperGame> {
|
||||
const Spacer(flex: 5),
|
||||
Center(
|
||||
child: Text(
|
||||
gameEnded()
|
||||
? "${Util.stateText(winner)} Wins"
|
||||
: "${Util.stateText(turn)}'s Turn",
|
||||
"${Util.stateText(turn)}'s Turn",
|
||||
style: const TextStyle(fontSize: 25),
|
||||
),
|
||||
),
|
||||
const Spacer(flex: 1),
|
||||
GameHash(
|
||||
cellOnTapCallback: _cellOnTapCallback,
|
||||
cellOnTapCallback: _showSubGameDialog,
|
||||
children: Iterable.generate(data.length)
|
||||
.map(
|
||||
(i) => DecoratedBox(
|
||||
@@ -171,15 +158,10 @@ class _SuperGameState extends State<SuperGame> {
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(15),
|
||||
child: !subGameEnded(i)
|
||||
? TTCGame(
|
||||
turn: turn,
|
||||
data: data[i],
|
||||
)
|
||||
: Text(
|
||||
subGameWinner(i).name.toUpperCase(),
|
||||
style: const TextStyle(fontSize: 40),
|
||||
),
|
||||
child: TTCGame(
|
||||
turn: turn,
|
||||
data: data[i],
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -25,24 +25,10 @@ class Util {
|
||||
emptyBoardClassic,
|
||||
];
|
||||
|
||||
static TTCState nextTurn(TTCState currentPlayer,
|
||||
{TTCState defaultState = TTCState.x}) {
|
||||
switch (currentPlayer) {
|
||||
case TTCState.x:
|
||||
return TTCState.o;
|
||||
case TTCState.o:
|
||||
return TTCState.x;
|
||||
default:
|
||||
return defaultState;
|
||||
}
|
||||
}
|
||||
|
||||
static String stateText(TTCState state) => state.name.toUpperCase();
|
||||
static String cellAddress(int index) =>
|
||||
"${index % 3}, ${(index / 3).floor()}";
|
||||
|
||||
static Iterable<TTCState> getRow(int index, List<TTCState> data) =>
|
||||
data.getRange(index * 3, index * 3 + 3);
|
||||
data.getRange(index, index + 3);
|
||||
static Iterable<TTCState> getCol(int index, List<TTCState> data) => [
|
||||
data[index],
|
||||
data[index + 3],
|
||||
@@ -52,7 +38,7 @@ class Util {
|
||||
if (index == 0) {
|
||||
return [data[0], data[4], data[8]];
|
||||
}
|
||||
return [data[2], data[4], data[6]];
|
||||
return [data[3], data[4], data[6]];
|
||||
}
|
||||
|
||||
static TTCState checkRow(Iterable<TTCState> row) =>
|
||||
|
||||
Reference in New Issue
Block a user