-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps.cpp
67 lines (62 loc) · 1.75 KB
/
rps.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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
char displayMenu();
int outcome(char, int);
int main() {
string r;
int c, counter=0;
srand(time(NULL));
string rps[]={"rock","paper","scissors"};
while(++counter){
c=rand()%3;
switch(outcome(displayMenu(), c)){
case -1: cout << "NPC chose " << rps[c] << ", player one wins.";
break;
case 0: cout << "NPC chose " << rps[c] << ", player two wins..";
break;
default:cout << "NPC chose " << rps[c] << ", it's a draw.";
}
cout << "You have played " << counter << " times";
}
return 0;
}
char displayMenu() {
string player="";
while(1) {
cout << "You can choose either [r]ock, [p]aper, or [s]cissors";
cin >>player;
switch (player[0]) {
case 'r':cout << "You have chosen rock, are you sure? (y/n)";
break;
case 'p':cout << "You have chosen paper, are you sure? (y/n)";
break;
case 's':cout << "You have chosen scissors, are you sure? (y/n)";
break;
default: cout << "Please choose r for rock, p for paper, or s for scissors.";
continue;
break;
}
char n;
cin >> n;
if(n!='y')
continue;
else
break;
}
return player[0];
}
int outcome(char a, int c) {
int r;
switch (a) {
case 'r':r=0;
break;
case 'p':r=1;
break;
default:r=2;
}
int result=
(r==0)?(c==0)?1:(c==1)?0:-1:(r==1)?(c==0)?-1:(c==1)?(1):0:(c==0)?0:(c==1)?-1:1;
return result;
}