forked from NBnfork/Group21RPS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPSGame.cpp
More file actions
207 lines (185 loc) · 5.72 KB
/
Copy pathRPSGame.cpp
File metadata and controls
207 lines (185 loc) · 5.72 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*********************************************************************
* Author: Noah Buchen, Alexandra Henley, Elliott Lapinel, Patrick
* Rice, and Samantha Tone
* Date: 10/25/2017
* Description:
*********************************************************************/
#include "RPSGame.hpp"
#include "validChar.hpp"
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <cstdlib>
using std::rand;
RPSGame::RPSGame()
{
rounds = 0;
hWins = 0;
cWins = 0;
ties = 0;
}
void RPSGame::playGame()
{
cout << "**************************************************" << endl;
cout << "Welcome to Rock, Paper, Scissors!" << endl;
cout << "Would you like to set different strengths for the tools? (y - yes, n - no)" << endl;
getline(cin, inputStr);
ynInput = validChar(inputStr, yn, 2);
//TODO *** THIS IS WHERE STRENGTH WILL NEED TO BE SET *** //
cout << "**************************************************" << endl;
// do while loop for the game to play as long as the user has not input 'e'
do{
rounds = rounds + 1;
cout << "**************************************************" << endl;
cout << "ROCK, PAPER, SCISSORS -- ROUND " << rounds << ":" << endl;
cout << "Choose your tool (r - rock, p - paper, s - scissor, e - exit)" << endl;
getline(cin, inputStr);
rpseInput = validChar(inputStr, rpse, 4);
if (rpseInput != 'e')
{
cout << "**************************************************" << endl;
assignToolComp();
cout << " VS." << endl;
userHistory.push_back(rpseInput); // must assign after comp AI done to avoid cheating
assignToolUser();
//TODO *** FIGHT FUNCTION WILL NEED TO GO HERE *** //
//TODO *** DELETE UP TO "END DELETE HERE" LINE ONCE FIGHT FUNCTION IS USED *** //
if (rpseInput == compChoice)
{gameWinner = 't';}
else if (rpseInput == 'r')
{
if(compChoice == 'p')
{gameWinner = 'c';}
else if(compChoice = 's')
{gameWinner = 'h';}
}
else if (rpseInput == 'p')
{
if(compChoice == 'r')
{gameWinner = 'h';}
else if(compChoice = 's')
{gameWinner = 'c';}
}
else if (rpseInput == 's')
{
if(compChoice == 'p')
{gameWinner = 'h';}
else if(compChoice = 'r')
{gameWinner = 'c';}
}
//TODO *** END DELETE HERE *** //
printResults();
}
} while(rpseInput != 'e');
cout << "**************************************************" << endl;
cout << "You have ended the game before round " << rounds << endl;
cout << "FINAL GAME TOTALS:" << endl;
cout << "Human: " << hWins << "\t Computer: " << cWins << "\t Ties: " << ties << endl;
cout << "**************************************************" << endl;
}
void RPSGame::assignToolUser()
{
//TODO *** WILL NEED TO ADD ASSIGNMENTS TO USER TOOLS WITHIN LOOPS *** //
if (rpseInput == 'r')
{
cout << "Human: ROCK" << endl;
}
if (rpseInput == 'p')
{
cout << "Human: PAPER" << endl;
}
if (rpseInput == 's')
{
cout << "Human: SCISSORS" << endl;
}
}
void RPSGame::assignToolComp() // making this void as the Tools are available throughout class
{
rCount = pCount = sCount = 0;
if (rounds > 3) // must have 3 saved rounds in order to have enough input
{
last = userHistory[rounds - 3]; // this is -3 because starts at zero & we look at 2 rounds back
current = userHistory[rounds - 2]; // this is -2 because starts at zero & we look at prev round
for (int i=1; i<rounds-1; i++)
{
if (userHistory[i] == current && userHistory[i-1] == last)
{
if (userHistory[i+1] == rock)
{rCount++;}
else if (userHistory[i+1] == paper)
{pCount++;}
else if (userHistory[i+1] == scissor)
{sCount++;}
}
}
// 7 possible outcomes below:
if(rCount == pCount && rCount == sCount) // all equally likely
{
randVal = rand()%3;
if (randVal == 0)
{compChoice = 'r';}
else if (randVal == 1)
{compChoice = 'p';}
else if (randVal == 2)
{compChoice = 's';}
}
else if (rCount > pCount && rCount > sCount) // rock most likely
{compChoice = 'p';} // choose paper to beat their rock
else if (pCount > rCount && pCount > sCount) // paper most likely
{compChoice = 's';} // choose scissors to beat their paper
else if (sCount > pCount && sCount > rCount) // scissors most likely
{compChoice = 'r';} // choose rock to beat their scissors
else if (rCount == pCount && rCount > sCount) // rock & paper most likely
{compChoice = 'p';} // p beats r & ties p but s loses to r & beats p
else if (rCount == sCount && rCount > pCount) // rock & scissors most likely
{compChoice = 'r';} // r beats s & ties r but p loses to s & beats r
else if (sCount == pCount && sCount > rCount) // scissors & paper most likely
{compChoice = 's';} // s beats p & ties s but r loses to p & beats s
}
else // randomly select if not enough data for AI
{
randVal = rand()%3;
if (randVal == 0)
{compChoice = 'r';}
else if (randVal == 1)
{compChoice = 'p';}
else if (randVal == 2)
{compChoice = 's';}
}
//TODO *** WILL NEED TO ADD ASSIGNMENTS TO USER TOOLS WITHIN LOOPS *** //
if (compChoice == 'r')
{
cout << "Computer: ROCK" << endl;
}
if (compChoice == 'p')
{
cout << "Computer: PAPER" << endl;
}
if (compChoice == 's')
{
cout << "Computer: SCISSORS" << endl;
}
}
void RPSGame::printResults()
{
cout << "**************************************************" << endl;
if (gameWinner == 't')
{
cout << "It's a tie!" << endl;
ties = ties + 1;
}
else if (gameWinner == 'h')
{
cout << "Human wins!" << endl;
hWins = hWins + 1;
}
else if (gameWinner == 'c')
{
cout << "Computer wins!" << endl;
cWins = cWins + 1;
}
cout << "RUNNING GAME TOTALS:" << endl;
cout << "Human: " << hWins << "\t Computer: " << cWins << "\t Ties: " << ties << endl;
cout << "**************************************************" << endl;
}