Browse Source

Implementation der Berechnung des Gewinners

remotes/origin/tim
fdai7727 11 months ago
parent
commit
58e6b3018b
  1. 51
      src/main/c/Tim/SchereSteinPapier.c
  2. 1
      src/main/c/Tim/SchereSteinPapier.h

51
src/main/c/Tim/SchereSteinPapier.c

@ -17,13 +17,24 @@ void schereSteinPapier()
scanf("%d", &user); scanf("%d", &user);
if(user == 0) if(user == 0)
{ {
printf("Vielen Dank fürs Spielen! Tschau!\n");
break; break;
} }
else if(user == 1 || user == 2 || user == 3) else if(user == 1 || user == 2 || user == 3)
{ {
srand(time(NULL)); srand(time(NULL));
com = rand() % 3 + 1; com = rand() % 3 + 1;
printf("%d\n",com);
int win = calculateWinner(user, com);
switch (win)
{
case(-1):printf("Der Computer hat %d gewaehlt, Du hast verloren!!!\n\n", com);
break;
case(0): printf("Der Computer hat %d gewaehlt, Es steht unentschieden!!!\n\n", com);
break;
case(1): printf("Der Computer hat %d gewaehlt, Du hast gewonnen!!!\n\n", com);
break;
default: printf("Error!");
}
} }
else else
{ {
@ -31,3 +42,41 @@ void schereSteinPapier()
} }
} }
} }
int calculateWinner(int x, int y)
{
switch (x)
{
case(1):
switch (y) {
case(1): return 0;
case(2): return -1;
case(3): return 1;
default: return 3;
}
case(2):
switch (y) {
case(1): return 1;
case(2): return 0;
case(3): return -1;
default: return 3;
}
case(3):
switch (y) {
case(1): return 1;
case(2): return -1;
case(3): return 0;
default: return 3;
}
}
}

1
src/main/c/Tim/SchereSteinPapier.h

@ -2,5 +2,6 @@
#define SCHERESTEINPAPIER_H #define SCHERESTEINPAPIER_H
void schereSteinPapier(); void schereSteinPapier();
int calculateWinner(int, int);
#endif #endif
Loading…
Cancel
Save