Browse Source

refactoring neue Funktion int checkForWin(pos1, ...)

remotes/origin/tictactoedev
Sabina Grisi 11 months ago
parent
commit
769ff78b6d
  1. 85
      src/TicTacToe.c

85
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;
@ -221,3 +167,26 @@ int switchPlayer(int player) {
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;
}
}
Loading…
Cancel
Save