MCPcopy Create free account
hub / github.com/careercup/ctci / hasWon4

Method hasWon4

java/Chapter 17/Question17_2/QuestionB.java:156–195  ·  view source on GitHub ↗
(Piece[][] board)

Source from the content-addressed store, hash-verified

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) {

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected