forked from ash638/code-for-hactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockPaperScissors.cpp
More file actions
55 lines (55 loc) · 1.93 KB
/
RockPaperScissors.cpp
File metadata and controls
55 lines (55 loc) · 1.93 KB
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int getRan();
int userCh();
void winner(int, int);
int main()
{
int userChoice = userCh();
int compChoice = getRan();
if(userChoice == compChoice)
{
cout << "Both players have entered the same choice\nNo winner was decided" << endl;
}
else
winner(compChoice, userChoice);
}
//Function to input computer's choice
int getRan()
{
srand(time(0));
int rn = rand() % 3 + 1;
if (rn == 1) cout << "Computer has chosen Rock" << endl;
if (rn == 2) cout << "Computer has chosen Paper" << endl;
if (rn == 3) cout << "Computer has chosen Scissors" << endl;
return rn;
}
//Function to input user's choice
int userCh()
{
int c;
cout << "Enter 1 for Rock, 2 for Paper, 3 for Scissors:" << endl;
cin >> c;
while(!(c>=1&&c<=3))
{
cout<<"Invalid choice..."<<endl<<"Enter again:"<<endl;
cin>>c;
}
if (c == 1) cout << "You have chosen Rock" << endl;
if (c == 2) cout << "You have chosen Paper" << endl;
if (c == 3) cout << "You have chosen Scissors" << endl;
return c;
}
//Function to determine the winner
void winner(int c, int u)
{
cout << endl;
if (c == 1 && u == 3) cout << "Computer is the winner" << endl; //Computer's choice is Rock & user's choice is Scissors
if (c == 3 && u == 2) cout << "You are the winner" << endl; //Computer's choice is Scissors & user's choice is Paper
if (c == 2 && u == 1) cout << "Computer is the winner" << endl; //Computer's choice is Paper & user's choice is Rock
if (c == 3 && u == 1) cout << "You are the winner" << endl; //Computer's choice is Scissors & user's choice is Rock
if (c == 2 && u == 3) cout << "Computer is the winner" << endl; //Computer's choice is Paper & user's choice is Scissors
if (c == 1 && u == 2) cout << "You are the winner" << endl; //Computer's choice is Rock & user's choice is Paper
}