-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPS-Game-Simulator.c
69 lines (60 loc) · 2.1 KB
/
RPS-Game-Simulator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include<unistd.h>
// we define constants for ROCK, PAPER, SCISSORS to make our code easier to read
#define ROCK 1
#define PAPER 2
#define SCISSORS 3
int main()
{
// seed the random number generator with the current time to get different
// random numbers each time the program is run
srand( time(NULL) );
int player_throw = 0;
int ai_throw = 0;
bool draw = false;
do
{
// accept the player throw
printf("Select your throw.\n");
printf("1) Rock\n");
printf("2) Paper\n");
printf("3) Scissors\n");
printf("Selection: ");
scanf("%d", &player_throw);
// randomly generate the AI throw (where 1-3 represent rock/paper/scissors
// as defined above)
ai_throw = ((rand() * getpid()) % 3) + 1;
// output the AI's throw to the player
if (ai_throw == ROCK)
printf("\nAI throws ROCK.\n");
else if (ai_throw == PAPER)
printf("\nAI throws PAPER.\n");
else if (ai_throw == SCISSORS)
printf("\nAI throws SCISSORS.\n");
// determine and output the winner, if any
draw = false;
if (player_throw == ROCK && ai_throw == SCISSORS)
printf("\nROCK beats SCISSORS. YOU WIN.\n\n");
else if (player_throw == ROCK && ai_throw == PAPER)
printf("\nPAPER beats ROCK. YOU LOSE.\n\n");
else if (player_throw == SCISSORS && ai_throw == PAPER)
printf("\nSCISSORS beats PAPER. YOU WIN.\n\n");
else if (player_throw == SCISSORS && ai_throw == ROCK)
printf("\nROCK beats SCISSORS. YOU LOSE.\n\n");
else if (player_throw == PAPER && ai_throw == ROCK)
printf("\nPAPER beats ROCK. YOU WIN.\n\n");
else if (player_throw == PAPER && ai_throw == SCISSORS)
printf("\nSCISSORS beats PAPER. YOU LOSE.\n\n");
else
{
// if both player and AI throw the same, it's a draw
printf("\nDRAW! Play again.\n\n");
draw = true;
}
// in the case of a draw, the game is played again until there is a winner
} while (draw);
return 0;
}