Compare commits
2 Commits
03987378c0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| fedfbb3dbc | |||
| ef96a6623e |
155
lib/super.dart
155
lib/super.dart
@@ -13,17 +13,8 @@ class SuperGame extends StatefulWidget {
|
|||||||
class _SuperGameState extends State<SuperGame> {
|
class _SuperGameState extends State<SuperGame> {
|
||||||
TTCState turn = TTCState.x;
|
TTCState turn = TTCState.x;
|
||||||
List<List<TTCState>> data = Util.emptyBoardSuper;
|
List<List<TTCState>> data = Util.emptyBoardSuper;
|
||||||
List<bool> subGameEnded = [
|
TTCState winner = TTCState.empty;
|
||||||
false,
|
bool gameEnded() => winner != TTCState.empty;
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
];
|
|
||||||
|
|
||||||
List<TTCState> subGameWinners = [
|
List<TTCState> subGameWinners = [
|
||||||
TTCState.empty,
|
TTCState.empty,
|
||||||
@@ -36,6 +27,8 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
TTCState.empty,
|
TTCState.empty,
|
||||||
TTCState.empty,
|
TTCState.empty,
|
||||||
];
|
];
|
||||||
|
bool subGameEnded(int i) => subGameWinners[i] != TTCState.empty;
|
||||||
|
|
||||||
int nextPlay = -1;
|
int nextPlay = -1;
|
||||||
|
|
||||||
void _swapTurn() {
|
void _swapTurn() {
|
||||||
@@ -51,6 +44,51 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TTCState _validateSubGame(int index) {
|
||||||
|
subGameWinners[index] = Util.checkWin(data[index]);
|
||||||
|
print("validated $index");
|
||||||
|
return subGameWinners[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
TTCState _validateGame() {
|
||||||
|
winner = Util.checkWin(subGameWinners);
|
||||||
|
return winner;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
_validateSubGame(subGame);
|
||||||
|
_validateGame();
|
||||||
|
nextPlay = subGameEnded(i) ? -1 : i;
|
||||||
|
if (!gameEnded()) {
|
||||||
|
_swapTurn();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Navigator.pop(context);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Widget _subGameDialog(int subGame) {
|
Widget _subGameDialog(int subGame) {
|
||||||
return Dialog(
|
return Dialog(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
@@ -58,31 +96,30 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Padding(
|
||||||
"${Util.stateText(turn)} select cell",
|
padding: const EdgeInsets.only(bottom: 5),
|
||||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
child: Text(
|
||||||
|
"${Util.stateText(turn)} select cell",
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
TTCGame(
|
TTCGame(
|
||||||
turn: turn,
|
turn: turn,
|
||||||
cellOnTapCallback: (int i) {
|
cellOnTapCallback: subGameCellOnTapCallback(subGame),
|
||||||
nextPlay = i;
|
|
||||||
setState(() {
|
|
||||||
data[subGame][i] = turn;
|
|
||||||
_swapTurn();
|
|
||||||
});
|
|
||||||
Navigator.pop(context);
|
|
||||||
},
|
|
||||||
data: data[subGame],
|
data: data[subGame],
|
||||||
),
|
),
|
||||||
Row(
|
Padding(
|
||||||
mainAxisAlignment: MainAxisAlignment.end,
|
padding: const EdgeInsets.only(top: 5),
|
||||||
children: [
|
child: Row(
|
||||||
ElevatedButton(
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
onPressed: () {
|
children: [
|
||||||
Navigator.pop(context);
|
ElevatedButton(
|
||||||
},
|
onPressed: () {
|
||||||
child: const Text("Close"))
|
Navigator.pop(context);
|
||||||
],
|
},
|
||||||
|
child: const Text("Close"))
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -90,6 +127,18 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void endedSubGameNotification(int index) {
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
..clearSnackBars()
|
||||||
|
..showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text("${Util.stateText(subGameWinners[index])}"
|
||||||
|
" already won the game at "
|
||||||
|
"[${Util.cellAddress(index)}]"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void _showSubGameDialog(int i) {
|
void _showSubGameDialog(int i) {
|
||||||
if (nextPlay == i || nextPlay == -1) {
|
if (nextPlay == i || nextPlay == -1) {
|
||||||
showDialog(
|
showDialog(
|
||||||
@@ -110,20 +159,47 @@ 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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
Iterable.generate(subGameWinners.length)
|
||||||
|
.map((i) => subGameEnded(i))
|
||||||
|
.forEach(print);
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
const Spacer(flex: 5),
|
const Spacer(flex: 5),
|
||||||
Center(
|
Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
"${Util.stateText(turn)}'s Turn",
|
gameEnded()
|
||||||
|
? "${Util.stateText(winner)} Wins"
|
||||||
|
: "${Util.stateText(turn)}'s Turn",
|
||||||
style: const TextStyle(fontSize: 25),
|
style: const TextStyle(fontSize: 25),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const Spacer(flex: 1),
|
const Spacer(flex: 1),
|
||||||
GameHash(
|
GameHash(
|
||||||
cellOnTapCallback: _showSubGameDialog,
|
cellOnTapCallback: _cellOnTapCallback,
|
||||||
children: Iterable.generate(data.length)
|
children: Iterable.generate(data.length)
|
||||||
.map(
|
.map(
|
||||||
(i) => DecoratedBox(
|
(i) => DecoratedBox(
|
||||||
@@ -132,10 +208,15 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
),
|
),
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(15),
|
padding: const EdgeInsets.all(15),
|
||||||
child: TTCGame(
|
child: !subGameEnded(i)
|
||||||
turn: turn,
|
? TTCGame(
|
||||||
data: data[i],
|
turn: turn,
|
||||||
),
|
data: data[i],
|
||||||
|
)
|
||||||
|
: Text(
|
||||||
|
subGameWinners[i].name.toUpperCase(),
|
||||||
|
style: const TextStyle(fontSize: 40),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -26,9 +26,11 @@ class Util {
|
|||||||
];
|
];
|
||||||
|
|
||||||
static String stateText(TTCState state) => state.name.toUpperCase();
|
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) =>
|
static Iterable<TTCState> getRow(int index, List<TTCState> data) =>
|
||||||
data.getRange(index, index + 3);
|
data.getRange(index * 3, index * 3 + 3);
|
||||||
static Iterable<TTCState> getCol(int index, List<TTCState> data) => [
|
static Iterable<TTCState> getCol(int index, List<TTCState> data) => [
|
||||||
data[index],
|
data[index],
|
||||||
data[index + 3],
|
data[index + 3],
|
||||||
@@ -38,7 +40,7 @@ class Util {
|
|||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
return [data[0], data[4], data[8]];
|
return [data[0], data[4], data[8]];
|
||||||
}
|
}
|
||||||
return [data[3], data[4], data[6]];
|
return [data[2], data[4], data[6]];
|
||||||
}
|
}
|
||||||
|
|
||||||
static TTCState checkRow(Iterable<TTCState> row) =>
|
static TTCState checkRow(Iterable<TTCState> row) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user