From b4495a97b963f6f7de2199f7f8b9426945cfce7b Mon Sep 17 00:00:00 2001 From: aakash-kr-gorai Date: Sat, 9 Oct 2021 11:19:06 +0530 Subject: [PATCH] Random choice generator game via C --- C/SnakeWaterGame.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 C/SnakeWaterGame.c diff --git a/C/SnakeWaterGame.c b/C/SnakeWaterGame.c new file mode 100644 index 00000000..d17baf52 --- /dev/null +++ b/C/SnakeWaterGame.c @@ -0,0 +1,77 @@ +#include +#include +#include + +int snakeWaterGun(char you, char comp){ + // returns 1 if you win, -1 if you lose and 0 if draw + // Condition for draw + // Cases covered: + // ss + // gg + // ww + if(you == comp){ + return 0; + } + + // Non-draw conditions + // Cases covered: + // sg + // gs + // sw + // ws + // gw + // wg + + + if(you=='s' && comp=='g'){ + return -1; + } + else if(you=='g' && comp=='s'){ + return 1; + } + + if(you=='s' && comp=='w'){ + return 1; + } + else if(you=='w' && comp=='s'){ + return -1; + } + + if(you=='g' && comp=='w'){ + return -1; + } + else if(you=='w' && comp=='g'){ + return 1; + } + +} +int main(){ + char you, comp; + srand(time(0)); + int number = rand()%100 + 1; + + if(number<33){ + comp = 's'; + } + else if(number>33 && number<66){ + comp='w'; + } + else{ + comp='g'; + } + + printf("Enter 's' for snake, 'w' for water and 'g' for gun\n"); + scanf("%c", &you); + int result = snakeWaterGun(you, comp); + if(result ==0){ + printf("Game draw!\n"); + } + else if(result==1){ + printf("You win!\n"); + } + else{ + printf("You Lose!\n"); + } + printf("You chose %c and computer chose %c. ", you, comp); + return 0; +} \ No newline at end of file