-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
397 lines (315 loc) · 9.29 KB
/
main.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// main.cpp
// Problem Set 1b Chris Canal
// Team cansym [email protected]
// Sarada Symonds
//
// Main program file for homework 1b.
#include "Code.h"
#include "Response.h"
#include "Mastermind.h"
#include <iostream>
#include <vector>
#include <time.h> //for making rand() more random
using namespace std;
//------------Global Operator Overloading (MUST GO FIRST)---------------
ostream& operator << (ostream & ostr, Response responseObj)
{
ostr << "(" << responseObj.getCorrect() << ", ";
ostr << responseObj.getIncorrect() << ")";
return ostr;
}
//---------------------Code Class Functions-----------------
Code::Code()
//default constructor
{
n = 0;
m = 0;
}
Code::Code(int newN, int newM)
//constructor for range and length of the code
{
set(newN, newM);
}
int Code::getN()
//returns number of digits in the code
{
return n;
}
void Code::randomInit()
//initializes a random string of digits to be used as the code
{
for(int i = 0; i < n; i++)
//for each digit n, will add a random digit
{
//inserts an additional random digit to codeDigits
//that is in the range 0 to m-1
codeDigits.push_back(rand() % m);
}
}
int Code::checkCorrect( Code& guessDigits) const
//returns the number of correct digits in the correct location
{
int numberCorrect = 0;
for(int i = 0; i < guessDigits.getCode().size(); i++)
//compare each digit in the guess to the digits in the secret code
{
if (guessDigits.getCode()[i] == getCode()[i])
{
numberCorrect += 1;
}
}
return numberCorrect;
//end of checkCorrect function
}
int Code::checkIncorrect( Code& guessDigits) const
//returns the number of correct digits in the incorrect location
{
vector<int> guessVector = guessDigits.getCode();
vector<int> codeVector = getCode();
int incorrectLocationDigits = 0;
for(int i = 0; i < codeVector.size(); i++)
//for each digit in codeDigits
{
for(int j = 0; j < guessVector.size(); j++)
//for each digit in guessDigits
{
if(codeVector[i] == guessVector[j])
//increments incorrectDigits if digit is in code
//but not right place. Sets matched vector numbers
//to -1 so that they will not match with other ints
{
if (i == j)
//if digits in codeVector and guessVector are in same position
{
codeVector[i] = -1;
guessVector[j] = -1;
j = guessVector.size();
}
else
//if digits are not in same position
{
bool isPosSameI = codeVector.at(i) != guessVector.at(i);
bool isPosSameJ = codeVector.at(j) != guessVector.at(j);
if ( isPosSameI && isPosSameJ)
{
codeVector[i] = -1;
guessVector[j] = -1;
incorrectLocationDigits++;
j = guessVector.size();
}
//end of else statement for digit positions
}
//end of if statement to determine if digits match
}
//end of for loop to go through each guess digit
}
//end of for loop to go through each code digit
}
return incorrectLocationDigits;
//end of checkIncorrect function
}
void Code::set(int newN, int newM)
//sets the range and length of the code
{
n = newN;
m = newM;
}
void Code::setCodeDigits(const string &guess)
//adds an integer to codeDigits
{
for(int i = 0; i < guess.length(); i++)
//adds each number in guess to codeDigits
{
//Subtract 48 to convert from ASCII to Integer
codeDigits.push_back(guess[i] - 48);
}
}
vector<int> Code::getCode() const
//returns the code vector
{
return codeDigits;
}
void Code::checkGuess( Code& guessCode) const
//prints out the results of checkCorrent and checkIncorrect
{
cout << "Result: (" << checkCorrect(guessCode);
cout << ", " << checkIncorrect(guessCode);
cout << ")" << endl;
}
//---------------------Response Class Functions-----------------
Response::Response()
//default constructor
{
correctDigits = 0;
incorrectDigits = 0;
}
Response::Response(int newCorrectDigits, int newIncorrectDigits)
//constructor for two given values
{
correctDigits = newCorrectDigits;
incorrectDigits = newIncorrectDigits;
}
void Response::set(int newCorrectDigits, int newIncorrectDigits)
//set correct and incorrect digit amounts
{
correctDigits = newCorrectDigits;
incorrectDigits = newIncorrectDigits;
}
void Response::setCorrect(int newCorrectDigits)
//set only correct digit amount
{
correctDigits = newCorrectDigits;
}
void Response::setIncorrect(int newIncorrectDigits)
//set only incorrect digit amount
{
incorrectDigits = newIncorrectDigits;
}
int Response::getCorrect()
//get correct digit
{
return correctDigits;
}
int Response::getIncorrect()
//get incorrect digit
{
return incorrectDigits;
}
//operator overload to compare two Response objects
bool operator == ( Response & response1, Response & response2)
{
bool getCorBool = response1.getCorrect() == response2.getCorrect();
bool getIncorBool = response1.getIncorrect() == response2.getIncorrect();
return (getCorBool && getIncorBool);
}
//---------------------Mastermind Class Functions-----------------
Mastermind::Mastermind()
//default constructor
{
secretCode.set(5, 10);
}
Mastermind::Mastermind(int newN, int newM)
//constructor for the mastermind class
{
secretCode.set(newN, newM);
}
void Mastermind::printSecretCode()
//prints the secret code from the mastermind object
{
cout << "Secret Code: (";
for(int i = 0; i < secretCode.getCode().size(); ++i)
//prints out secret code
{
cout << secretCode.getCode()[i];
if (i != secretCode.getCode().size() - 1)
{
cout << ", ";
}
}
cout << ")" << endl;
//end of Mastermind member function printSecretCode
}
Code Mastermind::humanGuess()
//reads a guess from the keyboard and returns guess as Code object
{
Code guessCode;
string guessString = "EMPTY";
while(guessString == "EMPTY")
{
cout << "\nPlease enter your guess, numbers only please:" << endl;
cin >> guessString;
if (guessString.length() != secretCode.getCode().size())
{
cout << "\nWrong size! Please enter a sequence of numbers of length ";
cout << secretCode.getCode().size();
guessString = "EMPTY";
}
// end of while string - while string is "EMPTY"
}
guessCode.setCodeDigits(guessString);
cout << "Guess Code: (";
for(int i = 0; i < guessCode.getCode().size(); ++i)
{
cout << guessCode.getCode()[i];
if (i != guessCode.getCode().size() - 1)
{
cout << ", ";
}
}
cout << ")" << endl;
return guessCode;
//end of Mastermind member function humanGuess
}
Response Mastermind::getResponse(Code & guessCode, Code & secretCode)
//Compares two codes and gets response
{
Response codeResponse;
codeResponse.setCorrect(secretCode.checkCorrect(guessCode));
codeResponse.setIncorrect(secretCode.checkIncorrect(guessCode));
return codeResponse;
}
bool Mastermind::isSolved(Response responseObj)
// Checks the Response obj to see if it the code is solved
{
return responseObj.getCorrect() == secretCode.getN();
}
int Mastermind::playGame()
// initiates a game of mastermind
{
secretCode.randomInit();
printSecretCode();
Code userGuessCode;
Response userResponse;
for (int i = 0; i < 10; i++)
//allows user to input 10 guesses
{
userGuessCode = humanGuess();
userResponse = getResponse(userGuessCode, secretCode);
cout << userResponse << endl;
int secretCodeSize = secretCode.getCode().size();
int guessCodeSize = userGuessCode.getCode().size();
bool isCodeSize = guessCodeSize == secretCodeSize;
if (isSolved(userResponse) && isCodeSize)
//checks if user guess was correct and ends for loop if it is
{
cout << "\nCongratulations! You have guessed correctly!\n";
cout << "EXITING GAME..." << endl;
i = 10;
}
else
{
cout << "\nPlease guess again...";
}
if (i == 9)
//checks tht this is the last round and prints message
{
cout << "Wait. Actually...\nThat was your last guess.\n";
cout << "Sorry\nYOU LOSE\n";
}
//end of for loop for 10 guesses
}
return 0;
}
//--------------------- Free Functions ----------------------
void printCodeDigits(Code viewingCode)
//prints the code vector in a readable format
{
cout << "Code: (";
for(int i = 0; i < viewingCode.getCode().size(); ++i)
{
cout << viewingCode.getCode()[i];
if (i != viewingCode.getCode().size() - 1)
{
cout << ", ";
}
}
cout << ")" << endl;
//end of printCodeDigits
}
int main()
{
srand (time(NULL)); //Uses time to make rand more random
Mastermind theGame;
return theGame.playGame();
}