-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
67 lines (51 loc) · 2.03 KB
/
Copy pathMain.cpp
File metadata and controls
67 lines (51 loc) · 2.03 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
/*
Project 1A
This file contains the main program to play the game designed in the two previous files
This file prompt the user to enter the code length and range of digits
It creates the secret code by calling the initialize randomly method
It then prints the secret code for grading purposes
It then prompts the user to play at least 10 guesses to get the secret code
If the user gets the secret code, it congradulates the user
If not, it ends the game and again prints the secret code to the user
*/
#include "Code.h"
#include <vector>
#include <iostream>
using namespace std;
int main() {
srand(time(NULL)); // Random Time Seed
// Obtaining Length (n) and Range (m)
int n, m;
cout << "Enter the code length: ";
cin >> n;
cout << "Enter the range of digits: ";
cin >> m;
// Instance of class Code called secret_code
Code secret_code(n, m);
secret_code.initialize_randomly();
secret_code.print_secret_code();
cout << "You have 10 attempts to guess the code." << endl;
// Loop to store the guess of length (n) and tracking the attempts
for (int attempt = 1; attempt <= 10; ++attempt) {
cout << "Attempt " << attempt << ": Enter your guess of length " << n << ": ";
vector<int> guess(n);
for (int i = 0; i < n; ++i) {
cin >> guess[i];
}
// Returns the # of correct answers
int correct = secret_code.check_correct(guess);
// Returns the # of incorrect answers
int incorrect = secret_code.check_incorrect(guess);
cout << "Correct: " << correct << " Incorrect: " << incorrect << endl;
// If the # of correct matches the Length of the SecretCode
// Game Won
if (correct == n) {
cout << "Congratulations! You guessed the code correctly!" << endl;
return 0;
}
}
// After running out of attemps Game Over
cout << "Sorry, you did not guess the code within 10 attempts. The correct code was: ";
secret_code.print_secret_code();
return 0;
}