76 lines
2.1 KiB
Dart
76 lines
2.1 KiB
Dart
import 'state.dart';
|
|
|
|
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 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, 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]);
|
|
}
|
|
}
|