import 'state.dart'; class Util { static List get emptyBoardClassic => [ TTCState.empty, TTCState.empty, TTCState.empty, TTCState.empty, TTCState.empty, TTCState.empty, TTCState.empty, TTCState.empty, TTCState.empty, ]; static List> 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 getRow(int index, List data) => data.getRange(index * 3, index * 3 + 3); static Iterable getCol(int index, List data) => [ data[index], data[index + 3], data[index + 3 * 2], ]; static Iterable getCross(int index, List data) { if (index == 0) { return [data[0], data[4], data[8]]; } return [data[2], data[4], data[6]]; } static TTCState checkRow(Iterable row) => row.reduce((value, element) => (element == TTCState.empty || value == TTCState.empty || element != value) ? TTCState.empty : value); static TTCState checkBoard(Iterable rowValues) => rowValues.reduce((value, element) { if (value != TTCState.empty) { return value; } if (element != TTCState.empty) { return element; } return TTCState.empty; }); static TTCState checkWin(List data) { Iterable rows = Iterable.generate(3, (i) => checkRow(getRow(i, data))); Iterable cols = Iterable.generate(3, (i) => checkRow(getCol(i, data))); Iterable crosses = Iterable.generate(2, (i) => checkRow(getCross(i, data))); return checkBoard([...rows, ...cols, ...crosses]); } }