From 769ff78b6de86850b896f78e2b0dcbd90b3b0ba4 Mon Sep 17 00:00:00 2001 From: Sabina Grisi Date: Fri, 9 Feb 2024 22:08:00 +0100 Subject: [PATCH] refactoring neue Funktion int checkForWin(pos1, ...) --- src/TicTacToe.c | 85 ++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 58 deletions(-) diff --git a/src/TicTacToe.c b/src/TicTacToe.c index b25926e..388d666 100644 --- a/src/TicTacToe.c +++ b/src/TicTacToe.c @@ -8,6 +8,9 @@ void drawBoard(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int p // Funktion zum Wechseln des aktuellen Spielers int switchPlayer(int currentPlayer); +// Funktion, um zu prüfen, ob es einen Gewinner gibt +int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int currentPlayer); + int main() { int pos1 = 1; @@ -56,64 +59,7 @@ int main() { } drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); - // Prüfung auf Gewinn - if (pos1 == player && pos2 == player && pos3 == player) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } - } else if (pos4 == player && pos5 == player && pos6 == player) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } - } else if (pos7 == player && pos8 == player && pos9 == player) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } - } else if (pos1 == player && pos4 == player && pos7 == player) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } - } else if (pos2 == player && pos5 == player && pos8 == player) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } - } else if (pos3 == player && pos6 == player && pos9 == player) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } - } else if (pos1 == player && pos5 == player && pos9 == player) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } - } else if (pos3 == player && pos5 == player && pos7 == player) { + if (checkForWin(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, player)) { if(player == 10){ printf("\nSpieler X hat gewonnen!\n\n"); break; @@ -220,4 +166,27 @@ int switchPlayer(int player) { } else { return 10; } +} + +int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int pos7, int pos8, int pos9, int player) { + // Prüfung auf Gewinn + if (pos1 == player && pos2 == player && pos3 == player) { + return 1; + } else if (pos4 == player && pos5 == player && pos6 == player) { + return 1; + } else if (pos7 == player && pos8 == player && pos9 == player) { + return 1; + } else if (pos1 == player && pos4 == player && pos7 == player) { + return 1; + } else if (pos2 == player && pos5 == player && pos8 == player) { + return 1; + } else if (pos3 == player && pos6 == player && pos9 == player) { + return 1; + } else if (pos1 == player && pos5 == player && pos9 == player) { + return 1; + } else if (pos3 == player && pos5 == player && pos7 == player) { + return 1; + } else { + return 0; + } } \ No newline at end of file