import 'state.dart'; class Util { static Iterable getRow(int index, List data) => data.getRange(index, index + 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[3], 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]); } }