Browse Source

refactoring neue Funktion void messages(int message, char player)

remotes/origin/tictactoedev
Sabina Grisi 11 months ago
parent
commit
58fdfd5165
  1. 41
      src/TicTacToe.c

41
src/TicTacToe.c

@ -11,8 +11,13 @@ int switchPlayer(int currentPlayer);
// Funktion, um zu prüfen, ob es einen Gewinner gibt // 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 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() { int main() {
messages(99, '\0');
int pos1 = 1; int pos1 = 1;
int pos2 = 2; int pos2 = 2;
int pos3 = 3; int pos3 = 3;
@ -29,7 +34,7 @@ int main() {
// Spielschleife // Spielschleife
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
int in; int in;
printf("Player %c, enter your move (1-9): ", (player == 10) ? 'X' : 'O');
messages(4, (player == 10) ? 'X' : 'O');
scanf("%d", &in); scanf("%d", &in);
printf("\n"); printf("\n");
@ -53,21 +58,16 @@ int main() {
} else if (pos9 == in) { } else if (pos9 == in) {
pos9 = player; pos9 = player;
} else { } else {
printf("Try again. ");
messages(0, (player == 10) ? 'X' : 'O');
i--; // wiederholen i--; // wiederholen
continue; continue;
} }
drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); drawBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9);
if (checkForWin(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, player)) { 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");
messages(player, '\0'); // Winner
break; break;
} }
}
player = switchPlayer(player); player = switchPlayer(player);
@ -190,3 +190,28 @@ int checkForWin(int pos1, int pos2, int pos3, int pos4, int pos5, int pos6, int
return 0; 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;
}
}
Loading…
Cancel
Save