Compare commits
5 Commits
wip
...
a174ffc7c2
| Author | SHA1 | Date | |
|---|---|---|---|
| a174ffc7c2 | |||
| e9362706a1 | |||
| 5c334577bf | |||
| fedfbb3dbc | |||
| ef96a6623e |
@@ -23,8 +23,8 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||||||
TTCState.empty,
|
TTCState.empty,
|
||||||
TTCState.empty,
|
TTCState.empty,
|
||||||
];
|
];
|
||||||
bool ended = false;
|
TTCState get winner => Util.checkWin(data);
|
||||||
TTCState? winner;
|
bool get ended => winner != TTCState.empty;
|
||||||
|
|
||||||
String get turnText => switch (turn) {
|
String get turnText => switch (turn) {
|
||||||
TTCState.empty => "",
|
TTCState.empty => "",
|
||||||
@@ -32,13 +32,7 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||||||
TTCState.o => "O",
|
TTCState.o => "O",
|
||||||
};
|
};
|
||||||
|
|
||||||
void _nextTurn() {
|
void _nextTurn() => turn = Util.nextTurn(turn);
|
||||||
turn = switch (turn) {
|
|
||||||
TTCState.x => TTCState.o,
|
|
||||||
TTCState.o => TTCState.x,
|
|
||||||
_ => TTCState.x
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _invalidChoiceAlert(TTCState existingValue) {
|
Widget _invalidChoiceAlert(TTCState existingValue) {
|
||||||
return Dialog(
|
return Dialog(
|
||||||
@@ -52,7 +46,7 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||||||
"INVALID CHOICE",
|
"INVALID CHOICE",
|
||||||
style: TextStyle(fontWeight: FontWeight.bold),
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
),
|
),
|
||||||
Text("${existingValue.name.toUpperCase()} already claimed that"),
|
Text("${Util.stateText(existingValue)} already claimed that"),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () => Navigator.pop(context),
|
onPressed: () => Navigator.pop(context),
|
||||||
child: const Text("Ok")),
|
child: const Text("Ok")),
|
||||||
@@ -80,10 +74,6 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
data[index] = turn;
|
data[index] = turn;
|
||||||
winner = Util.checkWin(data);
|
|
||||||
if (winner != null && winner != TTCState.empty) {
|
|
||||||
ended = true;
|
|
||||||
}
|
|
||||||
_nextTurn();
|
_nextTurn();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -103,7 +93,7 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||||||
"GAME OVER",
|
"GAME OVER",
|
||||||
style: TextStyle(fontWeight: FontWeight.bold),
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
),
|
),
|
||||||
Text("${winner?.name.toUpperCase()} has already won"),
|
Text("${Util.stateText(winner)} has already won"),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () => Navigator.pop(context),
|
onPressed: () => Navigator.pop(context),
|
||||||
child: const Text("Ok")),
|
child: const Text("Ok")),
|
||||||
@@ -122,7 +112,7 @@ class _ClassicGameState extends State<ClassicGame> {
|
|||||||
const Spacer(flex: 5),
|
const Spacer(flex: 5),
|
||||||
Center(
|
Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
!ended ? "$turnText's turn" : "${winner?.name.toUpperCase()} wins",
|
!ended ? "$turnText's turn" : "${Util.stateText(winner)} wins",
|
||||||
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
|
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
|
||||||
)),
|
)),
|
||||||
const Spacer(flex: 1),
|
const Spacer(flex: 1),
|
||||||
|
|||||||
@@ -14,38 +14,15 @@ 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<TTCState> subGameWinners = [
|
TTCState subGameWinner(int index) => Util.checkWin(data[index]);
|
||||||
TTCState.empty,
|
List<TTCState> get subGameWinners => data.map(Util.checkWin).toList();
|
||||||
TTCState.empty,
|
bool subGameEnded(int i) => subGameWinner(i) != TTCState.empty;
|
||||||
TTCState.empty,
|
TTCState get winner => Util.checkWin(subGameWinners);
|
||||||
TTCState.empty,
|
bool gameEnded() => winner != TTCState.empty;
|
||||||
TTCState.empty,
|
|
||||||
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() => turn = Util.nextTurn(turn);
|
||||||
switch (turn) {
|
|
||||||
case TTCState.x:
|
|
||||||
turn = TTCState.o;
|
|
||||||
break;
|
|
||||||
case TTCState.o:
|
|
||||||
turn = TTCState.x;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
turn = TTCState.x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TTCState _validateSubGame(int index) {
|
|
||||||
subGameWinners[index] = Util.checkWin(data[index]);
|
|
||||||
return subGameWinners[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool _checkValidChoice(List<TTCState> game, int index) =>
|
bool _checkValidChoice(List<TTCState> game, int index) =>
|
||||||
game[index] == TTCState.empty;
|
game[index] == TTCState.empty;
|
||||||
@@ -68,11 +45,12 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextPlay = subGameEnded(i) ? -1 : i;
|
|
||||||
setState(() {
|
setState(() {
|
||||||
data[subGame][i] = turn;
|
data[subGame][i] = turn;
|
||||||
_validateSubGame(subGame);
|
nextPlay = subGameEnded(i) ? -1 : i;
|
||||||
_swapTurn();
|
if (!gameEnded()) {
|
||||||
|
_swapTurn();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
};
|
};
|
||||||
@@ -121,7 +99,7 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
..clearSnackBars()
|
..clearSnackBars()
|
||||||
..showSnackBar(
|
..showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text("${Util.stateText(subGameWinners[index])}"
|
content: Text("${Util.stateText(subGameWinner(index))}"
|
||||||
" already won the game at "
|
" already won the game at "
|
||||||
"[${Util.cellAddress(index)}]"),
|
"[${Util.cellAddress(index)}]"),
|
||||||
),
|
),
|
||||||
@@ -148,26 +126,43 @@ 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: (i) => subGameEnded(i)
|
cellOnTapCallback: _cellOnTapCallback,
|
||||||
? endedSubGameNotification(i)
|
|
||||||
: _showSubGameDialog(i),
|
|
||||||
children: Iterable.generate(data.length)
|
children: Iterable.generate(data.length)
|
||||||
.map(
|
.map(
|
||||||
(i) => DecoratedBox(
|
(i) => DecoratedBox(
|
||||||
@@ -182,7 +177,7 @@ class _SuperGameState extends State<SuperGame> {
|
|||||||
data: data[i],
|
data: data[i],
|
||||||
)
|
)
|
||||||
: Text(
|
: Text(
|
||||||
subGameWinners[i].name.toUpperCase(),
|
subGameWinner(i).name.toUpperCase(),
|
||||||
style: const TextStyle(fontSize: 40),
|
style: const TextStyle(fontSize: 40),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -25,12 +25,24 @@ class Util {
|
|||||||
emptyBoardClassic,
|
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 stateText(TTCState state) => state.name.toUpperCase();
|
||||||
static String cellAddress(int index) =>
|
static String cellAddress(int index) =>
|
||||||
"${index % 3}, ${(index / 3).floor()}";
|
"${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],
|
||||||
@@ -40,7 +52,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