Browse Source

Create Stone Paper Scissor game by C language

pull/670/head
Jatin Ranpariya GitHub 5 years ago
parent
commit
c6eab7534c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 113 additions and 0 deletions
  1. +113
    -0
      Stone Paper Scissor game by C language

+ 113
- 0
Stone Paper Scissor game by C language View File

@@ -0,0 +1,113 @@
#### for creating this game in C programming , You have to write code as shown below

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Random(int n)
{
srand(time(NULL)); //srand takes seed as an input and is defined inside <stdlib.h>
return rand() % n;
}
// Create Rock, paper & Scissors Game
// player 1 : rock
// player 2 (computer) : scissors--> player 1 gets 1 point

// stone(0) vs scissors(2) - stone(0) wins
// paper(1) vs stone(0) - paper(1) wins
// scissors(2) vs paper(1) - scissors(2) wins

// write a C program to allow user to play this game three times with computer. Log the scores of computer and the player.Display the name of the winner at the end.

// Notes : You have to display name of the player during the game , Take users name as an input from the user.

int main()
{
// printf("The random number between 0 to 2 is %d\n", Random(3));
printf("Welcome !!! Enter your name : ");
char name[50];
gets(name);
int i = 0;
int p = 0, c = 0;

printf("You're welcome %s , in Stone-paper-scissors game.\n", name);
printf("Hey,\npress 0 for stone.\npress 1 for paper.\npress 2 for scissors.\n\n");
while (i < 3)
{

printf("%s's turn : ", name);
int n;
scanf("%d", &n);
printf("Computer's Turn : %d\n\n", Random(3));
if (n == 0)
{
if (Random(3) == 0)
{
goto end;
}
else if (Random(3) == 1)
{
c++;
}
else
{
p++;
}
}

if (n == 1)
{
if (Random(3) == 0)
{
p++;
}
else if (Random(3) == 1)
{
goto end;
}
else
{
c++;
}
}

if (n == 2)
{
if (Random(3) == 0)
{
c++;
}
else if (Random(3) == 1)
{
p++;
}
else
{
goto end;
}
}

end:
i++;
}
printf("\n");
printf("~~~~~~~~~~score~~~~~~~~~~ \n");
printf("%s : computer = %d : %d\n", name, p, c);
if (p == c)
{
printf("The match is tied.\n");
}
else if (p >> c)
{
printf("congratulations,%s\nyou won the game against computer.\n", name);
}
else
{
printf("You lose...\nPLEASE...!!! TRY AGAIN\n");
}
printf("~~~~~~~~~~score~~~~~~~~~~ \n");

return 0;
}

#### BEST OF LUCK

Loading…
Cancel
Save