Browse Source

Merge branch 'Ariana' into 'main'

refactoring: using an auxiliary feature for win conditions

See merge request pmuw_projekt/pmuw_projekt_notebinder!23
remotes/origin/fdai7775-main-patch-54732
fdai7775 11 months ago
parent
commit
51c4117814
  1. 21
      src/main/c/GameTic_Tac_Toe/game.c

21
src/main/c/GameTic_Tac_Toe/game.c

@ -17,17 +17,26 @@ void displayBoard() {
if (i < 2) printf("---------\n"); 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 // Funktion zur Überprüfung des Gewinners
char checkWinner() { char checkWinner() {
// Überprüfe horizontale und vertikale Linien // Ü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 // Überprüfe diagonale Linien
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return board[0][0];
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return board[0][2];
char diagonal1Winner = checkLine(board[0][0], board[1][1], board[2][2]);
char diagonal2Winner = checkLine(board[0][2], board[1][1], board[2][0]);
if (diagonal1Winner != ' ') return diagonal1Winner;
if (diagonal2Winner != ' ') return diagonal2Winner;
// Kein Gewinner // Kein Gewinner
return ' '; return ' ';

Loading…
Cancel
Save