-
Notifications
You must be signed in to change notification settings - Fork 0
/
stonepaperscissor.cpp
138 lines (134 loc) · 3.08 KB
/
stonepaperscissor.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//Stone Paper Scissors game using C++ by Vishruth Codes
#include<iostream>
#include<stdio.h>
#include<time.h>
using namespace std;
int computerchooses()
{
//Function to randomly generate and return nos. between 1 and 3 based on which either stone(if 1's generated) or paper(if 2's generated) else scissor is selected by the computer
int res;
srand(time(0));
res=1+(rand()%3);
//cout<<"\nThe r no is :"<<res;
return res;
}
int results(int n, int cn)
{
//n - User's choice
//cn - Computer's choice
//0 - If player lost
//1 - If player won
//2 - The game is a tie
if(n==1 && cn==2)
{
return 0;
}
else if(n==1 && cn==3)
{
return 1;
}
else if(n==1 && cn==1)
{
return 2;
}
else if(n==2 && cn==3)
{
return 0;
}
else if(n==2 && cn==1)
{
return 1;
}
else if(n==2 && cn==2)
{
return 2;
}
else if(n==3 && cn==1)
{
return 0;
}
else if(n==3 && cn==2)
{
return 1;
}
else //if(n==3 && cn==3)
{
return 2;
}
}
//The manager
int main()
{
int n,i=0,cn,res;
int maxpts=0;
char name[15];
cout<<"\nEnter your Name: ";
cin>>name;
cout<<"\nYou are about to play Stone-Paper-Scissor against computer.";
cout<<"\nEnter the maximum points:- ";
cin>>maxpts;
int plrpts=0, cspts=0;
while(plrpts<=maxpts && cspts<=maxpts)
{
i++;
cout<<"\n\n|-----MATCH "<<i<<"-----|";
//Player: User
cout<<"\nEnter -\n1. for Stone\n2. for Paper\n3. for Scissors\n:- ";
//This is the basis for the choices of both computer and user.
cin>>n;
if(n==1)
{
cout<<"\nPlayer "<<name<<" chose Stone.";
}
else if(n==2)
{
cout<<"\nPlayer "<<name<<" chose Paper.";
}
else //if(n==3)
{
cout<<"\nPlayer "<<name<<" chose Scissor.";
}
//Player: Computer
cn=computerchooses();
if(cn==1)
{
cout<<"\nComputer chooses Stone.";
}
else if(cn==2)
{
cout<<"\nComputer chooses Paper.";
}
else //if(cn==3)
{
cout<<"\nComputer chooses Scissor.";
}
//Deciding the winner
res=results(n,cn);
if(res==0)
{
cout<<"\nComputer won!";
cspts=cspts+1;
cout<<"\n\nThe points earned by - \n"<<name<<" - "<<plrpts<<"\nComputer - "<<cspts;
}
else if(res==1)
{
cout<<"\nPlayer "<<name<<" won!";
plrpts=plrpts+1;
cout<<"\n\nThe points earned by - \n"<<name<<" - "<<plrpts<<"\nComputer - "<<cspts;
}
else //(res==2)
{
cout<<"\nThe match was a tie!";
cout<<"\n\nThe points earned by - \n"<<name<<" - "<<plrpts<<"\nComputer - "<<cspts;
}
}
//Final winner declaration
if(cspts>=plrpts)
{
cout<<"\nThe result is - COMPUTER WINS THE GAME. Better luck next time "<<name<<".";
}
else{
cout<<"\nThe result is - Congratulations! "<<name<<" WINS THE GAME";
}
return 0;
}