| 154 | |
| 155 | /* Shortest of the NxN solutions */ |
| 156 | public static Piece hasWon4(Piece[][] board) { |
| 157 | int N = board.length; |
| 158 | int i, j; |
| 159 | |
| 160 | Piece[] pieces = {Piece.Red, Piece.Blue}; |
| 161 | for (Piece color : pieces) { |
| 162 | // Check rows and columns |
| 163 | for (i = 0; i < N; i++) { |
| 164 | boolean maybe_column = true; |
| 165 | boolean maybe_row = true; |
| 166 | for (j = 0; j < N; j++) { |
| 167 | if (board[i][j] != color) { // row |
| 168 | maybe_row = false; |
| 169 | } |
| 170 | if (board[j][i] != color) { // columns |
| 171 | maybe_column = false; |
| 172 | } |
| 173 | } |
| 174 | if (maybe_column || maybe_row) { |
| 175 | return color; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Check diagonals |
| 180 | boolean maybe_diag1 = true; |
| 181 | boolean maybe_diag2 = true; |
| 182 | for (i = 0; i < N; i++) { |
| 183 | if (board[i][i] != color) { // normal diag |
| 184 | maybe_diag1 = false; |
| 185 | } |
| 186 | if (board[N-i-1][i] != color) { // reverse diag |
| 187 | maybe_diag2 = false; |
| 188 | } |
| 189 | } |
| 190 | if (maybe_diag1 || maybe_diag2) { |
| 191 | return color; |
| 192 | } |
| 193 | } |
| 194 | return Piece.Empty; |
| 195 | } |
| 196 | |
| 197 | public static Piece convertIntToPiece(int i) { |
| 198 | if (i == 1) { |