Compare commits
No commits in common. "c9a43f505c1f30c3525b28df88d6bc22ba71c5ad" and "c8bcf620ef8849fc4666723bac65477319a352ca" have entirely different histories.
c9a43f505c
...
c8bcf620ef
139
lib/classic.dart
139
lib/classic.dart
|
|
@ -1,139 +0,0 @@
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'state.dart';
|
|
||||||
import 'game.dart';
|
|
||||||
import "util.dart";
|
|
||||||
|
|
||||||
class ClassicGame extends StatefulWidget {
|
|
||||||
const ClassicGame({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<ClassicGame> createState() => _ClassicGameState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _ClassicGameState extends State<ClassicGame> {
|
|
||||||
TTCState turn = TTCState.x;
|
|
||||||
List<TTCState> data = [
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
TTCState.empty,
|
|
||||||
];
|
|
||||||
bool ended = false;
|
|
||||||
TTCState? winner;
|
|
||||||
|
|
||||||
String get turnText => switch (turn) {
|
|
||||||
TTCState.empty => "",
|
|
||||||
TTCState.x => "X",
|
|
||||||
TTCState.o => "O",
|
|
||||||
};
|
|
||||||
|
|
||||||
void _nextTurn() {
|
|
||||||
turn = switch (turn) {
|
|
||||||
TTCState.x => TTCState.o,
|
|
||||||
TTCState.o => TTCState.x,
|
|
||||||
_ => TTCState.x
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _invalidChoiceAlert(TTCState existingValue) {
|
|
||||||
return Dialog(
|
|
||||||
child: Padding(
|
|
||||||
padding: const 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;
|
|
||||||
winner = Util.checkWin(data);
|
|
||||||
if (winner != null && winner != TTCState.empty) {
|
|
||||||
ended = true;
|
|
||||||
}
|
|
||||||
_nextTurn();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
void _endedCellOnTap(int index) {
|
|
||||||
showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (context) {
|
|
||||||
return Dialog(
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.all(10),
|
|
||||||
child: Column(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
const Text(
|
|
||||||
"GAME OVER",
|
|
||||||
style: TextStyle(fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
Text("${winner?.name.toUpperCase()} has already won"),
|
|
||||||
ElevatedButton(
|
|
||||||
onPressed: () => Navigator.pop(context),
|
|
||||||
child: const Text("Ok")),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Column(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
children: [
|
|
||||||
const Spacer(flex: 5),
|
|
||||||
Center(
|
|
||||||
child: Text(
|
|
||||||
!ended ? "$turnText's turn" : "${winner?.name.toUpperCase()} wins",
|
|
||||||
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
|
|
||||||
)),
|
|
||||||
const Spacer(flex: 1),
|
|
||||||
TTCGame(
|
|
||||||
turn: turn,
|
|
||||||
cellOnTapCallback: !ended ? _cellOnTap : _endedCellOnTap,
|
|
||||||
data: data,
|
|
||||||
cellTextStyle: const TextStyle(fontSize: 40),
|
|
||||||
),
|
|
||||||
const Spacer(flex: 5),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,98 +0,0 @@
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'state.dart';
|
|
||||||
|
|
||||||
/// Board of a single game of tic tac toe
|
|
||||||
class TTCGame extends StatelessWidget {
|
|
||||||
const TTCGame({
|
|
||||||
super.key,
|
|
||||||
required this.turn,
|
|
||||||
required this.cellOnTapCallback,
|
|
||||||
required this.data,
|
|
||||||
this.cellTextStyle,
|
|
||||||
});
|
|
||||||
|
|
||||||
final TTCState turn;
|
|
||||||
final List<TTCState> data;
|
|
||||||
|
|
||||||
/// styling for text in cell
|
|
||||||
final TextStyle? cellTextStyle;
|
|
||||||
|
|
||||||
/// hook into end of turn cycle;
|
|
||||||
final Function cellOnTapCallback;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
List<Widget> cells = data
|
|
||||||
.map((state) => Text(
|
|
||||||
state != TTCState.empty ? state.name.toUpperCase() : "",
|
|
||||||
style: cellTextStyle,
|
|
||||||
))
|
|
||||||
.toList();
|
|
||||||
return GameHash(
|
|
||||||
cellOnTapCallback: cellOnTapCallback,
|
|
||||||
children: cells,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class GameHash extends StatelessWidget {
|
|
||||||
const GameHash({super.key, required this.children, this.cellOnTapCallback});
|
|
||||||
final List<Widget> children;
|
|
||||||
|
|
||||||
final Function? cellOnTapCallback;
|
|
||||||
|
|
||||||
Border _genCellBorder(
|
|
||||||
int index, {
|
|
||||||
BorderSide borderSide = const BorderSide(),
|
|
||||||
}) {
|
|
||||||
return Border(
|
|
||||||
top: index < 3 ? BorderSide.none : borderSide,
|
|
||||||
bottom: index > 5 ? BorderSide.none : borderSide,
|
|
||||||
left: [0, 3, 6].contains(index) ? BorderSide.none : borderSide,
|
|
||||||
right: [2, 5, 8].contains(index) ? BorderSide.none : borderSide,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
MapEntry<int, Widget> _genCell(int index, Widget content) {
|
|
||||||
return MapEntry(
|
|
||||||
index,
|
|
||||||
Container(
|
|
||||||
decoration: BoxDecoration(border: _genCellBorder(index)),
|
|
||||||
child: HashCell(
|
|
||||||
stateSetCallback: () {
|
|
||||||
if (cellOnTapCallback != null) {
|
|
||||||
cellOnTapCallback!(index);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
child: content,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return GridView.count(
|
|
||||||
crossAxisCount: 3,
|
|
||||||
shrinkWrap: true,
|
|
||||||
children: children.asMap().map(_genCell).values.toList(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class HashCell extends StatelessWidget {
|
|
||||||
const HashCell({
|
|
||||||
super.key,
|
|
||||||
required this.child,
|
|
||||||
this.stateSetCallback,
|
|
||||||
});
|
|
||||||
|
|
||||||
final Widget child;
|
|
||||||
final VoidCallback? stateSetCallback;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) => InkWell(
|
|
||||||
onTap: stateSetCallback,
|
|
||||||
child: Center(child: child),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
262
lib/main.dart
262
lib/main.dart
|
|
@ -1,5 +1,4 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'clasic.dart';
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
|
|
@ -30,52 +29,239 @@ class MyHomePage extends StatefulWidget {
|
||||||
State<MyHomePage> createState() => _MyHomePageState();
|
State<MyHomePage> createState() => _MyHomePageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
enum GameType { classicTTC, superTTC }
|
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
GameType type = GameType.classicTTC;
|
TTCState turn = TTCState.x;
|
||||||
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
late TTCGame game;
|
||||||
|
|
||||||
|
String get turnText => switch (turn) {
|
||||||
|
TTCState.empty => "",
|
||||||
|
TTCState.x => "X",
|
||||||
|
TTCState.o => "O",
|
||||||
|
};
|
||||||
|
|
||||||
|
void onTurnEnd() {
|
||||||
|
setState(() {
|
||||||
|
turn = switch (turn) {
|
||||||
|
TTCState.x => TTCState.o,
|
||||||
|
TTCState.o => TTCState.x,
|
||||||
|
_ => TTCState.x
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
game = TTCGame(
|
||||||
|
turn: turn,
|
||||||
|
onClick: onTurnEnd,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
drawer: Drawer(
|
|
||||||
key: _scaffoldKey,
|
|
||||||
child: ListView(
|
|
||||||
children: [
|
|
||||||
const DrawerHeader(
|
|
||||||
child: Text(
|
|
||||||
"Game Types",
|
|
||||||
style: TextStyle(fontSize: 25),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
ListTile(
|
|
||||||
title: const Text("Classic"),
|
|
||||||
onTap: () {
|
|
||||||
setState(() {
|
|
||||||
type = GameType.classicTTC;
|
|
||||||
});
|
|
||||||
Navigator.pop(context);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
ListTile(
|
|
||||||
title: const Text("Super"),
|
|
||||||
onTap: () {
|
|
||||||
setState(() {
|
|
||||||
type = GameType.superTTC;
|
|
||||||
});
|
|
||||||
Navigator.pop(context);
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
)),
|
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(widget.title),
|
title: Text(widget.title),
|
||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||||
),
|
),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: const EdgeInsets.all(10),
|
padding: const EdgeInsets.all(10),
|
||||||
child: type == GameType.classicTTC
|
child: Column(
|
||||||
? const ClassicGame()
|
mainAxisSize: MainAxisSize.min,
|
||||||
: const Center(child: Text("Under Construction")),
|
children: [
|
||||||
|
const Spacer(flex: 5),
|
||||||
|
Center(
|
||||||
|
child: Text(
|
||||||
|
"$turnText's turn",
|
||||||
|
style:
|
||||||
|
const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
|
||||||
|
)),
|
||||||
|
const Spacer(flex: 1),
|
||||||
|
TTCGame(
|
||||||
|
turn: turn,
|
||||||
|
onClick: onTurnEnd,
|
||||||
|
),
|
||||||
|
const Spacer(flex: 5),
|
||||||
|
],
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum TTCState { empty, x, o }
|
||||||
|
|
||||||
|
/// Board of a single game of tic tac toe
|
||||||
|
class TTCGame extends StatefulWidget {
|
||||||
|
const TTCGame({
|
||||||
|
super.key,
|
||||||
|
required this.turn,
|
||||||
|
this.data,
|
||||||
|
this.onClick,
|
||||||
|
});
|
||||||
|
|
||||||
|
final TTCState turn;
|
||||||
|
final List<TTCState>? data;
|
||||||
|
|
||||||
|
/// hook into acction of tapping on square
|
||||||
|
final Function? onClick;
|
||||||
|
|
||||||
|
@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")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterable<TTCState> getRow(int index) => data.getRange(index, index + 3);
|
||||||
|
Iterable<TTCState> getCol(int index) => [
|
||||||
|
data[index],
|
||||||
|
data[index + 3],
|
||||||
|
data[index + 3 * 2],
|
||||||
|
];
|
||||||
|
Iterable<TTCState> getCross(int index) {
|
||||||
|
if (index == 0) {
|
||||||
|
return [data[0], data[4], data[8]];
|
||||||
|
}
|
||||||
|
return [data[3], data[4], data[6]];
|
||||||
|
}
|
||||||
|
|
||||||
|
TTCState _checkRow(Iterable<TTCState> row) => row.reduce((value, element) =>
|
||||||
|
(element == TTCState.empty || value == TTCState.empty || element != value)
|
||||||
|
? TTCState.empty
|
||||||
|
: value);
|
||||||
|
|
||||||
|
TTCState _checkBoard(Iterable<TTCState> rowValues) =>
|
||||||
|
rowValues.reduce((value, element) {
|
||||||
|
if (value != TTCState.empty) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
if (element != TTCState.empty) {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TTCState.empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
TTCState _checkWin() {
|
||||||
|
Iterable<TTCState> rows = Iterable.generate(3, (i) => _checkRow(getRow(i)));
|
||||||
|
Iterable<TTCState> cols = Iterable.generate(3, (i) => _checkRow(getCol(i)));
|
||||||
|
Iterable<TTCState> crosses =
|
||||||
|
Iterable.generate(2, (i) => _checkRow(getCross(i)));
|
||||||
|
|
||||||
|
return _checkBoard([...rows, ...cols, ...crosses]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void notifyWin() {
|
||||||
|
TTCState state = _checkWin();
|
||||||
|
|
||||||
|
if (state != TTCState.empty) {
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
..clearSnackBars()
|
||||||
|
..showSnackBar(
|
||||||
|
SnackBar(content: Text("${state.name.toUpperCase()} wins")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Border _genCellBorder(
|
||||||
|
int index, {
|
||||||
|
BorderSide borderSide = const BorderSide(),
|
||||||
|
}) {
|
||||||
|
return Border(
|
||||||
|
top: index < 3 ? BorderSide.none : borderSide,
|
||||||
|
bottom: index > 5 ? BorderSide.none : borderSide,
|
||||||
|
left: [0, 3, 6].contains(index) ? BorderSide.none : borderSide,
|
||||||
|
right: [2, 5, 8].contains(index) ? BorderSide.none : borderSide,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _genCell(int index, TTCState state) => Container(
|
||||||
|
decoration: BoxDecoration(border: _genCellBorder(index)),
|
||||||
|
child: TTCCell(
|
||||||
|
state: state,
|
||||||
|
stateSetCallback: () {
|
||||||
|
setCellState(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 = [];
|
||||||
|
for (final (index, state) in data.indexed) {
|
||||||
|
cells.add(_genCell(index, state));
|
||||||
|
}
|
||||||
|
return GridView.count(
|
||||||
|
crossAxisCount: 3,
|
||||||
|
shrinkWrap: true,
|
||||||
|
children: cells,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TTCCell extends StatelessWidget {
|
||||||
|
const TTCCell({
|
||||||
|
super.key,
|
||||||
|
required this.state,
|
||||||
|
this.stateSetCallback,
|
||||||
|
this.textStyle,
|
||||||
|
});
|
||||||
|
|
||||||
|
final TTCState state;
|
||||||
|
final VoidCallback? stateSetCallback;
|
||||||
|
final TextStyle? textStyle;
|
||||||
|
|
||||||
|
String get text => switch (state) {
|
||||||
|
TTCState.empty => "",
|
||||||
|
TTCState.x => "X",
|
||||||
|
TTCState.o => "O",
|
||||||
|
};
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) => InkWell(
|
||||||
|
onTap: stateSetCallback,
|
||||||
|
child: Center(
|
||||||
|
child: Text(text, style: textStyle),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
enum TTCState { empty, x, o }
|
|
||||||
|
|
@ -1,47 +0,0 @@
|
||||||
import 'state.dart';
|
|
||||||
|
|
||||||
class Util {
|
|
||||||
static Iterable<TTCState> getRow(int index, List<TTCState> data) =>
|
|
||||||
data.getRange(index, index + 3);
|
|
||||||
static Iterable<TTCState> getCol(int index, List<TTCState> data) => [
|
|
||||||
data[index],
|
|
||||||
data[index + 3],
|
|
||||||
data[index + 3 * 2],
|
|
||||||
];
|
|
||||||
static Iterable<TTCState> getCross(int index, List<TTCState> data) {
|
|
||||||
if (index == 0) {
|
|
||||||
return [data[0], data[4], data[8]];
|
|
||||||
}
|
|
||||||
return [data[3], data[4], data[6]];
|
|
||||||
}
|
|
||||||
|
|
||||||
static TTCState checkRow(Iterable<TTCState> row) =>
|
|
||||||
row.reduce((value, element) => (element == TTCState.empty ||
|
|
||||||
value == TTCState.empty ||
|
|
||||||
element != value)
|
|
||||||
? TTCState.empty
|
|
||||||
: value);
|
|
||||||
|
|
||||||
static TTCState checkBoard(Iterable<TTCState> rowValues) =>
|
|
||||||
rowValues.reduce((value, element) {
|
|
||||||
if (value != TTCState.empty) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
if (element != TTCState.empty) {
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
return TTCState.empty;
|
|
||||||
});
|
|
||||||
|
|
||||||
static TTCState checkWin(List<TTCState> data) {
|
|
||||||
Iterable<TTCState> rows =
|
|
||||||
Iterable.generate(3, (i) => checkRow(getRow(i, data)));
|
|
||||||
Iterable<TTCState> cols =
|
|
||||||
Iterable.generate(3, (i) => checkRow(getCol(i, data)));
|
|
||||||
Iterable<TTCState> crosses =
|
|
||||||
Iterable.generate(2, (i) => checkRow(getCross(i, data)));
|
|
||||||
|
|
||||||
return checkBoard([...rows, ...cols, ...crosses]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue