-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPS.cpp
118 lines (101 loc) · 2.41 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
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
/* Implementation of Rock-Paper-Scissors
Keeps tracks of wins, losses, and draws.
Lets user choose what to throw, then reports
results and updates record.
*/
// uses includes from stdafx
#include "stdafx.h"
#define ROCK 0
#define PAPER 1
#define SCISSORS 2
using namespace std;
void printChoice(int i);
int main()
{
int playChoice = 3, compChoice = 0;
string playChoiceChar;
cout << "Welcome to Rock-Paper-Scissors! " << endl;
/* Decision matrix for player vs. computer
Row = Player Choice
Column = Computer Choice
2 = Tie
1 = Player Win
0 = Computer Win */
int decision[3][3] = { 2,0,1,1,2,0,0,1,2 };
int wins = 0, loss = 0, draw = 0, result;
do
{
// generate computer choice
srand((unsigned int)time(NULL));
compChoice = rand() % 3;
/* Display options for user*/
cout << endl << "Please enter your choice: " << endl;
cout << "Rock - R" << endl;
cout << "Paper - P" << endl;
cout << "Scissors - S" << endl;
cout << "Quit - Q" << endl << endl << "Choice: ";
/* Read in user choice*/
cin >> playChoiceChar;
/* Assign appropriate choice, only reads first char*/
switch (toupper(playChoiceChar[0])) {
case 'R':
playChoice = ROCK;
break;
case 'P':
playChoice = PAPER;
break;
case 'S':
playChoice = SCISSORS;
break;
case 'Q':
return 0;
break;
default:
playChoice = 3; // 3 -> User did not choose correct input
cout << endl << "Please choose one of the options." << endl;
break;
}
if (playChoice != 3)
{
/* Print results */
cout << endl << "You chose: ";
printChoice(playChoice);
cout << "Computer chose: ";
printChoice(compChoice);
cout << endl << "Result: ";
result = decision[playChoice][compChoice];
if (result == 0) {
cout << "COMPUTER WIN!" << endl;
loss++;
}
else if (result == 1){
cout << "PLAYER WIN!" << endl;
wins++;
}
else {
cout << "DRAW!" << endl;
draw++;
}
cout << endl << "Current Record: " << wins << "W-" << loss << "L-" << draw << "D" << endl;
cout << endl << "------------------------------";
}
} while (1);
}
//Function to add
void printChoice(int i)
{
switch (i)
{
case 0:
cout << "Rock" << endl;
break;
case 1:
cout << "Paper" << endl;
break;
case 2:
cout << "Scissors" << endl;
break;
default:
break;
}
}