Browse Source

refactoring: using an auxiliary feature for win conditions

remotes/origin/Ariana
Ariana Ginju 11 months ago
committed by David Moeller
parent
commit
990b115c96
  1. 14
      src/main/c/GameTic_Tac_Toe/game.c

14
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];

Loading…
Cancel
Save