Skip to content

Commit 45eb9e7

Browse files
authored
Tidy the winning coordinate check for TicTacToe
1 parent b80e887 commit 45eb9e7

File tree

1 file changed

+64
-126
lines changed

1 file changed

+64
-126
lines changed

Kepler-Server/src/main/java/org/alexdev/kepler/game/games/gamehalls/GameTicTacToe.java

Lines changed: 64 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -201,143 +201,81 @@ private void announceWinningSide(Pair<Character, List<int[]>> variables) {
201201
/**
202202
* Check for the winner.
203203
*
204-
* @return a variable containing the character who won, and the coords of the winning tiles
204+
* @return a pair containing the winning character and coordinates of winning tiles, or null if no winner
205205
*/
206206
private Pair<Character, List<int[]>> hasGameFinished() {
207-
List<int[]> winningCoordinates = new ArrayList<>();
208-
209-
// Check rows across
210-
for (int i = 0; i < MAX_WIDTH; i++) {
211-
for (int j = 0; j < MAX_LENGTH; j++) {
212-
char letter = this.gameMap[i][j];
213-
winningCoordinates.clear();
214-
215-
if (letter == '0') {
216-
continue;
217-
}
218-
219-
for (int k = 0; k < NUM_IN_ROW; k++) {
220-
if ((j + k) >= MAX_LENGTH) {
221-
continue;
222-
}
223-
224-
char newLetter = this.gameMap[i][j + k];
225-
226-
if (newLetter != '0' && newLetter == letter) {
227-
winningCoordinates.add(new int[]{i, j + k});
228-
letter = newLetter;
229-
230-
if (winningCoordinates.size() >= NUM_IN_ROW) {
231-
return Pair.of(letter, winningCoordinates);
232-
}
233-
} else {
234-
winningCoordinates.clear();
235-
}
236-
}
237-
}
238-
}
239-
240-
// Check rows down
241-
for (int i = 0; i < MAX_WIDTH; i++) {
242-
for (int j = 0; j < MAX_LENGTH; j++) {
243-
char letter = this.gameMap[i][j];
244-
winningCoordinates.clear();
245-
246-
if (letter == '0') {
207+
// Direction vectors: horizontal, vertical, diagonal (\), diagonal (/)
208+
int[][] directions = {
209+
{0, 1}, // horizontal
210+
{1, 0}, // vertical
211+
{1, 1}, // diagonal \
212+
{-1, 1} // diagonal /
213+
};
214+
215+
for (int row = 0; row < MAX_WIDTH; row++) {
216+
for (int col = 0; col < MAX_LENGTH; col++) {
217+
char startChar = gameMap[row][col];
218+
219+
if (startChar == '0') {
247220
continue;
248221
}
249-
250-
for (int k = 0; k < NUM_IN_ROW; k++) {
251-
if ((i + k) >= MAX_WIDTH) {
252-
continue;
253-
}
254-
255-
char newLetter = this.gameMap[i + k][j];
256-
257-
if (newLetter != '0' && newLetter == letter) {
258-
winningCoordinates.add(new int[]{i + k, j});
259-
letter = newLetter;
260-
261-
if (winningCoordinates.size() >= NUM_IN_ROW) {
262-
return Pair.of(letter, winningCoordinates);
263-
}
264-
} else {
265-
winningCoordinates.clear();
222+
223+
// Check each direction from this starting position
224+
for (int[] direction : directions) {
225+
List<int[]> winningCoords = checkDirection(row, col, direction[0], direction[1], startChar);
226+
227+
if (winningCoords != null && winningCoords.size() >= NUM_IN_ROW) {
228+
return Pair.of(startChar, winningCoords);
266229
}
267230
}
268231
}
269232
}
270-
271-
// Check top left to bottom right
272-
for (int i = 0; i < MAX_WIDTH; i++) {
273-
for (int j = 0; j < MAX_LENGTH; j++) {
274-
char letter = this.gameMap[i][j];
275-
winningCoordinates.clear();
276-
277-
if (letter == '0') {
278-
continue;
279-
}
280-
281-
for (int k = 0; k < NUM_IN_ROW; k++) {
282-
if ((i + k) >= MAX_WIDTH || (j + k) >= MAX_WIDTH) {
283-
continue;
284-
}
285-
286-
char newLetter = this.gameMap[i + k][j + k];
287-
288-
if (newLetter != '0' && newLetter == letter) {
289-
winningCoordinates.add(new int[]{i + k, j + k});
290-
letter = newLetter;
291-
292-
if (winningCoordinates.size() >= NUM_IN_ROW) {
293-
return Pair.of(letter, winningCoordinates);
294-
}
295-
} else {
296-
winningCoordinates.clear();
297-
}
298-
}
233+
234+
return null;
235+
}
236+
237+
/**
238+
* Check for consecutive matching characters in a specific direction.
239+
*
240+
* @param startRow starting row position
241+
* @param startCol starting column position
242+
* @param deltaRow row direction increment
243+
* @param deltaCol column direction increment
244+
* @param targetChar character to match
245+
* @return list of winning coordinates if found, null otherwise
246+
*/
247+
private List<int[]> checkDirection(int startRow, int startCol, int deltaRow, int deltaCol, char targetChar) {
248+
List<int[]> coordinates = new ArrayList<>();
249+
250+
for (int step = 0; step < NUM_IN_ROW; step++) {
251+
int currentRow = startRow + (step * deltaRow);
252+
int currentCol = startCol + (step * deltaCol);
253+
254+
if (!isValidPosition(currentRow, currentCol)) {
255+
return null;
299256
}
300-
}
301-
302-
// Check top right to bottom left
303-
for (int i = 0; i < MAX_WIDTH; i++) {
304-
for (int j = 0; j < MAX_LENGTH; j++) {
305-
char letter = this.gameMap[i][j];
306-
winningCoordinates.clear();
307-
308-
if (letter == '0') {
309-
continue;
310-
}
311-
312-
for (int k = 0; k < NUM_IN_ROW; k++) {
313-
int newX = i - k;
314-
int newY = j + k;
315-
316-
if (newX < 0) {
317-
continue;
318-
}
319-
320-
if (newX >= MAX_WIDTH || newY >= MAX_WIDTH) {
321-
continue;
322-
}
323-
324-
char newLetter = this.gameMap[newX][newY];
325-
326-
if (newLetter != '0' && newLetter == letter) {
327-
winningCoordinates.add(new int[]{newX, newY});
328-
letter = newLetter;
329-
330-
if (winningCoordinates.size() >= NUM_IN_ROW) {
331-
return Pair.of(letter, winningCoordinates);
332-
}
333-
} else {
334-
winningCoordinates.clear();
335-
}
336-
}
257+
258+
char currentChar = gameMap[currentRow][currentCol];
259+
260+
if (currentChar != targetChar) {
261+
return null;
337262
}
263+
264+
coordinates.add(new int[]{currentRow, currentCol});
338265
}
339-
340-
return null;
266+
267+
return coordinates;
268+
}
269+
270+
/**
271+
* Check if the given position is within board bounds.
272+
*
273+
* @param row row position
274+
* @param col column position
275+
* @return true if position is valid, false otherwise
276+
*/
277+
private boolean isValidPosition(int row, int col) {
278+
return row >= 0 && row < MAX_WIDTH && col >= 0 && col < MAX_LENGTH;
341279
}
342280

343281
/**

0 commit comments

Comments
 (0)