From 4af8ceda5d461297fa85ba5c0bf158a4874f1090 Mon Sep 17 00:00:00 2001 From: fdai7775 Date: Tue, 30 Jan 2024 17:15:30 +0000 Subject: [PATCH] refactoring: using an auxiliary feature for win conditions --- src/main/c/GameTic_Tac_Toe/game.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/c/GameTic_Tac_Toe/game.c b/src/main/c/GameTic_Tac_Toe/game.c index 278b1e5..0765c17 100644 --- a/src/main/c/GameTic_Tac_Toe/game.c +++ b/src/main/c/GameTic_Tac_Toe/game.c @@ -17,13 +17,19 @@ void displayBoard() { if (i < 2) printf("---------\n"); } } - +char checkLine(char a, char b, char c) { + if (a == b && b == c) return a; + return ' '; +} // Funktion zur Überprüfung des Gewinners char checkWinner() { // Überprüfe horizontale und vertikale Linien - for (int i = 0; i < 3; i++) { - if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return board[i][0]; - if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return board[0][i]; + for (int i = 0; i < 3; i++) { + char horizontalWinner = checkLine(board[i][0], board[i][1], board[i][2]); + char verticalWinner = checkLine(board[0][i], board[1][i], board[2][i]); + + if (horizontalWinner != ' ') return horizontalWinner; + if (verticalWinner != ' ') return verticalWinner; } // Überprüfe diagonale Linien if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return board[0][0];