The C language implements the simplest example of a rock paper scissors game
- 2020-05-27 06:48:36
- OfStack
This article describes the C language implementation of the most simple rock paper scissors game. I will share it with you for your reference as follows:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*************\
* scissors stone cloth *
* The simplest little game *
\*************/
int main(void){
char gesture[3][10] = {"scissor","stone","cloth"};
int man, computer, result, ret;
/* Random number initializer function */
srand(time(NULL));
while(1){
computer = rand()%3;
printf("\nInput your gesture 0-scissor 1-stone 2-cloth:\n");
ret = scanf("%d", &man);
if(ret !=1 || man<0 || man>2){
printf("Invalid input!\n");
return 1;
}
printf("Your gesture:%s\tComputer's gesture: %s\n",
gesture[man], gesture[computer]
);
result = (man - computer + 4) %3 -1;
if(result > 0)
printf("YOU WIN!\n");
else if(result == 0)
printf("Draw!\n");
else
printf("You lose!\n");
}
return 0;
}
PS: the game USES ctrl+c to exit the program.
I hope this article has been helpful to you in the programming of C language.