Compare commits
4 Commits
03987378c0
...
astoica/fi
| Author | SHA1 | Date | |
|---|---|---|---|
| 69a038c84f | |||
| ed7ca12753 | |||
| fedfbb3dbc | |||
| ef96a6623e |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -41,3 +41,8 @@ app.*.map.json
|
|||||||
/android/app/debug
|
/android/app/debug
|
||||||
/android/app/profile
|
/android/app/profile
|
||||||
/android/app/release
|
/android/app/release
|
||||||
|
|
||||||
|
# Firebase
|
||||||
|
lib/firebase_options.dart
|
||||||
|
android/app/google-services.json
|
||||||
|
firebase.json
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id "com.android.application"
|
id "com.android.application"
|
||||||
|
// START: FlutterFire Configuration
|
||||||
|
id 'com.google.gms.google-services'
|
||||||
|
// END: FlutterFire Configuration
|
||||||
id "kotlin-android"
|
id "kotlin-android"
|
||||||
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
|
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
|
||||||
id "dev.flutter.flutter-gradle-plugin"
|
id "dev.flutter.flutter-gradle-plugin"
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ pluginManagement {
|
|||||||
plugins {
|
plugins {
|
||||||
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
|
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
|
||||||
id "com.android.application" version "8.1.0" apply false
|
id "com.android.application" version "8.1.0" apply false
|
||||||
|
// START: FlutterFire Configuration
|
||||||
|
id "com.google.gms.google-services" version "4.3.15" apply false
|
||||||
|
// END: FlutterFire Configuration
|
||||||
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
|
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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),
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ class TTCGame extends StatelessWidget {
|
|||||||
const TTCGame({
|
const TTCGame({
|
||||||
super.key,
|
super.key,
|
||||||
required this.turn,
|
required this.turn,
|
||||||
required this.cellOnTapCallback,
|
|
||||||
required this.data,
|
required this.data,
|
||||||
|
this.cellOnTapCallback,
|
||||||
this.cellTextStyle,
|
this.cellTextStyle,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ class TTCGame extends StatelessWidget {
|
|||||||
final TextStyle? cellTextStyle;
|
final TextStyle? cellTextStyle;
|
||||||
|
|
||||||
/// hook into end of turn cycle;
|
/// hook into end of turn cycle;
|
||||||
final Function cellOnTapCallback;
|
final void Function(int)? cellOnTapCallback;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -39,7 +39,7 @@ class GameHash extends StatelessWidget {
|
|||||||
const GameHash({super.key, required this.children, this.cellOnTapCallback});
|
const GameHash({super.key, required this.children, this.cellOnTapCallback});
|
||||||
final List<Widget> children;
|
final List<Widget> children;
|
||||||
|
|
||||||
final Function? cellOnTapCallback;
|
final void Function(int)? cellOnTapCallback;
|
||||||
|
|
||||||
Border _genCellBorder(
|
Border _genCellBorder(
|
||||||
int index, {
|
int index, {
|
||||||
@@ -93,6 +93,8 @@ class HashCell extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => InkWell(
|
Widget build(BuildContext context) => InkWell(
|
||||||
onTap: stateSetCallback,
|
onTap: stateSetCallback,
|
||||||
child: Center(child: child),
|
child: IgnorePointer(
|
||||||
|
child: Center(child: child),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'clasic.dart';
|
import 'package:super_tic_tac_toe/super.dart';
|
||||||
|
import 'classic.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@@ -75,7 +76,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
padding: const EdgeInsets.all(10),
|
padding: const EdgeInsets.all(10),
|
||||||
child: type == GameType.classicTTC
|
child: type == GameType.classicTTC
|
||||||
? const ClassicGame()
|
? const ClassicGame()
|
||||||
: const Center(child: Text("Under Construction")),
|
: const SuperGame(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
198
lib/super.dart
Normal file
198
lib/super.dart
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:super_tic_tac_toe/game.dart';
|
||||||
|
import 'package:super_tic_tac_toe/state.dart';
|
||||||
|
import 'package:super_tic_tac_toe/util.dart';
|
||||||
|
|
||||||
|
class SuperGame extends StatefulWidget {
|
||||||
|
const SuperGame({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<SuperGame> createState() => _SuperGameState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SuperGameState extends State<SuperGame> {
|
||||||
|
TTCState turn = TTCState.x;
|
||||||
|
List<List<TTCState>> data = Util.emptyBoardSuper;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
int nextPlay = -1;
|
||||||
|
|
||||||
|
void _swapTurn() => turn = Util.nextTurn(turn);
|
||||||
|
|
||||||
|
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(
|
||||||
|
padding: const EdgeInsets.all(10),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(bottom: 5),
|
||||||
|
child: Text(
|
||||||
|
"${Util.stateText(turn)} select cell",
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
TTCGame(
|
||||||
|
turn: turn,
|
||||||
|
cellOnTapCallback: subGameCellOnTapCallback(subGame),
|
||||||
|
data: data[subGame],
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 5),
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
|
children: [
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
child: const Text("Close"))
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => _subGameDialog(i),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
..clearSnackBars()
|
||||||
|
..showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
"You can only play at [${(nextPlay % 3) + 1},"
|
||||||
|
"${(nextPlay / 3).floor() + 1}]",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _turnText() => Text(
|
||||||
|
gameEnded()
|
||||||
|
? "${Util.stateText(winner)} Wins"
|
||||||
|
: "${Util.stateText(turn)}'s Turn",
|
||||||
|
style: const TextStyle(fontSize: 25),
|
||||||
|
);
|
||||||
|
|
||||||
|
Widget _subGmaeWidget(int i) {
|
||||||
|
if (subGameEnded(i)) {
|
||||||
|
return Text(
|
||||||
|
subGameWinner(i).name.toUpperCase(),
|
||||||
|
style: const TextStyle(fontSize: 40),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return TTCGame(turn: turn, data: data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Widget> _generateCells() => Iterable.generate(data.length)
|
||||||
|
.map(
|
||||||
|
(i) => DecoratedBox(
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: nextPlay == i ? Colors.lightGreenAccent : null,
|
||||||
|
),
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(15),
|
||||||
|
child: _subGmaeWidget(i),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Column(
|
||||||
|
children: [
|
||||||
|
const Spacer(flex: 5),
|
||||||
|
Center(
|
||||||
|
child: _turnText(),
|
||||||
|
),
|
||||||
|
const Spacer(flex: 1),
|
||||||
|
GameHash(
|
||||||
|
cellOnTapCallback: _cellOnTapCallback,
|
||||||
|
children: _generateCells(),
|
||||||
|
),
|
||||||
|
const Spacer(flex: 5),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,48 @@
|
|||||||
import 'state.dart';
|
import 'state.dart';
|
||||||
|
|
||||||
class Util {
|
class Util {
|
||||||
|
static List<TTCState> get emptyBoardClassic => [
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
TTCState.empty,
|
||||||
|
];
|
||||||
|
|
||||||
|
static List<List<TTCState>> get emptyBoardSuper => [
|
||||||
|
emptyBoardClassic,
|
||||||
|
emptyBoardClassic,
|
||||||
|
emptyBoardClassic,
|
||||||
|
emptyBoardClassic,
|
||||||
|
emptyBoardClassic,
|
||||||
|
emptyBoardClassic,
|
||||||
|
emptyBoardClassic,
|
||||||
|
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 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],
|
||||||
@@ -12,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