From 58e6b3018bd42428bc3fdd8f8991c9e4e562fe9e Mon Sep 17 00:00:00 2001 From: fdai7727 Date: Thu, 25 Jan 2024 18:47:09 +0100 Subject: [PATCH] Implementation der Berechnung des Gewinners --- src/main/c/Tim/SchereSteinPapier.c | 51 +++++++++++++++++++++++++++++- src/main/c/Tim/SchereSteinPapier.h | 1 + 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 72d76fc..6862522 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -17,13 +17,24 @@ void schereSteinPapier() scanf("%d", &user); if(user == 0) { + printf("Vielen Dank fürs Spielen! Tschau!\n"); break; } else if(user == 1 || user == 2 || user == 3) { srand(time(NULL)); 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 { @@ -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; + } + } +} \ No newline at end of file diff --git a/src/main/c/Tim/SchereSteinPapier.h b/src/main/c/Tim/SchereSteinPapier.h index 35c884b..a96c9f9 100644 --- a/src/main/c/Tim/SchereSteinPapier.h +++ b/src/main/c/Tim/SchereSteinPapier.h @@ -2,5 +2,6 @@ #define SCHERESTEINPAPIER_H void schereSteinPapier(); +int calculateWinner(int, int); #endif