From 58fdfd5165e94c491c69811178b7fed67554d7dc Mon Sep 17 00:00:00 2001 From: Sabina Grisi Date: Fri, 9 Feb 2024 22:25:45 +0100 Subject: [PATCH] refactoring neue Funktion void messages(int message, char player) --- src/TicTacToe.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/TicTacToe.c b/src/TicTacToe.c index 388d666..594c3fb 100644 --- a/src/TicTacToe.c +++ b/src/TicTacToe.c @@ -11,8 +11,13 @@ 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); +// Funktion zur Anzeige von Nachrichten +void messages(int message, char player); + int main() { + messages(99, '\0'); + int pos1 = 1; int pos2 = 2; int pos3 = 3; @@ -29,7 +34,7 @@ int main() { // Spielschleife for (int i = 0; i < 9; i++) { int in; - printf("Player %c, enter your move (1-9): ", (player == 10) ? 'X' : 'O'); + messages(4, (player == 10) ? 'X' : 'O'); scanf("%d", &in); printf("\n"); @@ -53,20 +58,15 @@ int main() { } else if (pos9 == in) { pos9 = player; } else { - printf("Try again. "); + messages(0, (player == 10) ? 'X' : 'O'); i--; // wiederholen continue; } drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); if (checkForWin(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, player)) { - if(player == 10){ - printf("\nSpieler X hat gewonnen!\n\n"); - break; - }else{ - printf("\nSpieler O hat gewonnen!\n\n"); - break; - } + messages(player, '\0'); // Winner + break; } player = switchPlayer(player); @@ -189,4 +189,29 @@ int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int } else { return 0; } +} + +void messages(int message, char player) { + switch (message) { + case 0: + printf("\nInvalid input. Please try again.\n"); + break; + case 10: + printf("\nPlayer X has won!\n\n"); + break; + case 11: + printf("\nPlayer O has won!\n\n"); + break; + case 4: + printf("\nPlayer %c, enter your move (1-9): ", player); + break; + case 99: + printf("\n\nWelcome to the TIC TAC TOE game!\n\nINSTRUCTIONS:\n"); + printf("Players take turns entering a number from 1 to 9 to mark their position on the board.\n"); + printf("Player 'X' begins first, and player 'O' second.\n"); + printf("The board looks like this: Start!\n\n"); + break; + default: + break; + } } \ No newline at end of file