In: Computer Science
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you!
The assignment is:
Design an ADT for a one-person guessing game that chooses 4 random integers in the range from 1 to 9 and asks the user to guess them. The same integer might be chosen more than once. For example, the game might choose the following four integers that range from 1 to 9: 4, 6, 1, 6. The following interaction could occur between the user and the game:
Enter your guesses for the 4 integers in the range from 1 to 9 that have been selected:
User Input: 1 2 3 4
2 of your guesses are correct. Guess again.
User Input: 2 4 6 8
2 of your guesses are correct. Guess again.
User Input: 1 4 6 6
You are correct! Play again? No
Good-bye!
Submit header file guess.h, implementation file guess.cpp, main function file main.cpp You need to include a makefile, and Readme file (see Programming Guidelines).
My code is:
---------------------------------main.cpp----------------------------------------
#include <iostream> // needed for using cin/cou statements
#include <string> // needed for using string type
#include <fstream> // needed for using files
#include <ctime> // Enables use of time() function
#include "guess.h"
using namespace std;
int main() {
bool play = true;
string answer;
int randomNumsToGuess[3];
int playerGuesses[4];
srand(time(0));
int matches = 0;
bool match = false;
Guess g1;
do {
cout << "Begin" << endl; //just for following along
do {
cout << "Enter your first guess" << endl;
cin >> playerGuesses[0];
cin.ignore();
cout << "Enter your second guess" << endl;
cin >> playerGuesses[1];
cin.ignore();
cout << "Enter your third guess" << endl;
cin >> playerGuesses[2];
cin.ignore();
cout << "Enter your fourth guess" << endl;
cin >> playerGuesses[3];
cin.ignore();
Guess (playerGuesses[0], playerGuesses[1], playerGuesses[2], playerGuesses[3]);
cout << "Sitting just before loop" << endl;
for (int i = 0; i < 4; i++) {
cout << "Entered first loop" << endl;
for (int j = 0; j < 4; j++) {
cout << "Entered second loop" << endl;
//***** HERE IS WHERE I ERROR OUR ******
if (g1.getVecGuesses(i) == g1.getVecRandomCopy(i)) {
matches = matches + 1;
//cout << "Match at playerGuess " << playerGuesses[i] << "and randomNum" << vecRandomCopy[j] << endl; //used during testing
g1.setVecGuesses(i);
g1.setVecRandomCopy(j);
//vecGuesses[i] = -1;
//vecRandomCopy[j] = -2;
//gameOver = true;
cout << "Matches: " << matches << endl;
}
cout << "Number of correct matches: " << matches << endl;
}
}
if (matches == 3) {
cout << "Congratulations! You guessed all three random numbers!" << endl;
} else {
cout << "Try again" << endl;
}
} while (matches != 3);
matches = 0;
cout <<"Would you like to play again?" << endl;
cout << "Enter Y for yes or N for no." << endl;
cin >> answer;
cin.ignore();
if (answer == "Y") {
play = true;
}
else {
play = false;
cout << "Thanks for playing!" << endl;
}
} while (play == true);
return 0;
};
-------------------------------Guess.h-------------------------------------------
//
// Created by craig on 9/4/2019.
//
#ifndef DSPD_HW1_GUESS_H
#define DSPD_HW1_GUESS_H
#include <vector>
using namespace std;
class Guess{
private:
vector <int> vecRandom;
vector <int> vecRandomCopy;
vector <int> vecGuesses;
int randomNum;
int matches = 0;
public:
Guess();
Guess(int a, int b, int c, int d);
void Matches (vector<int> vecRandomCopy, vector<int> vecGuesses);
void showVecGuesses(vector<int>);
//might need to Guess::vecGuesses[x] = -1;
void setVecGuesses (int x) {
vecGuesses[x] = -1;}
//void setVecGuesses (vector<int> _vecGuesses(int x)) {vecGuesses[x] = _vecGuesses[x];}
void setVecRandomCopy (int y) {
vecRandomCopy[y] = -2;}
/*void displayVecContents (){
for (unsigned int i=0; i<vecGuesses.size(); i++ )
cout << "Guesses " << vecGuesses[i] << endl;
}
*/
int getVecGuesses (int x) {
cout << "TEST " << vecGuesses[x];
return vecGuesses[x];}
int getVecRandomCopy (int y) {
return vecRandomCopy[y];}
int Matches (vector<Guess> & ) {
cout << "Testing Matches" << endl;
}
};
#endif //DSPD_HW1_GUESS_H
--------------------------guess.cpp --------------------------------------
//
// Created by craig on 9/4/2019.
//
#include <iostream> // needed for using cin/cou statements
#include <string> // needed for using string type
#include <fstream> // needed for using files
#include <ctime> // Enables use of time() function
#include <vector>
#include "guess.h"
using namespace std;
Guess::Guess() {//default constructor, fills with random numbers
for (int i = 0; i < 4; i++) {
randomNum = (rand() % 9)+1;
vecRandom.push_back(randomNum);
cout << "Random number " << i + 1 << ": " << vecRandom[i] << endl;
}
for (int i=0; i < 4; i++) { //creates a copy of the random numbers so I can manipulate it without
//losing the original random numbers
vecRandomCopy = vecRandom;
cout << "Random number copy " << i + 1 << ": " << vecRandomCopy[i] << endl;
}
}
Guess::Guess (int a, int b, int c, int d){ //constructor for user guesses
vecGuesses.push_back(a);
vecGuesses.push_back(b);
vecGuesses.push_back(c);
vecGuesses.push_back(d);
cout << "You Guessed: ";
for (int i = 0; i < 4; i++) {
cout << " " << vecGuesses[i];
}
cout << endl;
}
void showVecGuesses(vector<int> _vecGuesses){
for (int i=0; i<4; i++){
cout << "TESTING SHOW VEC GUESSES" << endl;
cout << _vecGuesses[i] << endl;
}
}
void Matches (vector<int> _vecRandomCopy, vector<int> _vecGuesses){
cout << "TESTING: do we get into matches?" << endl;
}
cpp code:
main.cpp
#include <iostream> // needed for using cin/cou statements
#include <string> // needed for using string type
#include <fstream> // needed for using files
#include <ctime> // Enables use of time() function
#include <stdlib.h>
#include "guess.h"
using namespace std;
//main
int main() {
bool play = true;
string answer;
int playerGuesses[4];
srand(time(0));
int matches = 0;
//loop starts
do {
Guess g1; //object for guess class (invoke the constructor &
set the guess
do {
//get numbers from user
cout << "Enter your first guess" << endl;
cin >> playerGuesses[0];
// cin.ignore();
cout << "Enter your second guess" << endl;
cin >> playerGuesses[1];
// cin.ignore();
cout << "Enter your third guess" << endl;
cin >> playerGuesses[2];
//cin.ignore();
cout << "Enter your fourth guess" << endl;
cin >> playerGuesses[3];
// cin.ignore();
/*rather than passing the user guess as constructor with
parameter, set the values with function.
when you set two constructor the previous value will be deleted
*/
//set the user guess with function
g1.setUserGuess(playerGuesses[0], playerGuesses[1],
playerGuesses[2], playerGuesses[3]);
//check the match
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
//if guess equals
if (g1.getVecGuesses(i) == g1.getVecRandomCopy(j)) {
matches = matches + 1; //increase the match
g1.setVecRandomCopy(j); //set the random copy to -1
}
}
}
//print the matches
cout << "\n\nNumber of correct matches: " << matches
<< endl;
//if matches >3, print msg
if (matches > 3) {
cout << "\nCongratulations! You guessed all three random numbers!" << endl;
} else {
//if not, reset the random copy and matches to 0
cout << "\nTry again" << endl;
g1.resetCopy();
matches = 0;
}
} while (matches <4); //if matches not found repeat the loop
//ask user to play again
cout <<"\nWould you like to play again?" << endl;
cout << "Enter Y for yes or N for no." << endl;
cin >> answer;
cin.ignore();
if (answer == "Y") {
play = true;
}
else {
play = false;
cout << "\n\nThanks for playing!" << endl;
}
} while (play == true);
return 0;
};
Guess.h
#ifndef GUESS_H_INCLUDED
#define GUESS_H_INCLUDED
#include <vector>
using namespace std;
class Guess{
private:
vector <int> vecRandom;
vector <int> vecRandomCopy;
vector <int> vecGuesses;
int randomNum;
int matches = 0;
public:
Guess();
void setUserGuess(int a, int b, int c, int d);
void Matches (vector<int> vecRandomCopy, vector<int> vecGuesses);
void showVecGuesses(vector<int>);
//might need to Guess::vecGuesses[x] = -1;
void setVecGuesses (int x) {
vecGuesses[x] = -1;}
//void setVecGuesses (vector<int> _vecGuesses(int x)) {vecGuesses[x] = _vecGuesses[x];}
void setVecRandomCopy (int y) {
vecRandomCopy[y] = -2;}
int getVecGuesses (int x) {
// cout << "TEST " << vecGuesses[x];
return vecGuesses[x];
}
int getVecRandomCopy (int y) {
return vecRandomCopy[y];
}
void Matches (vector<Guess> & ) {
cout << "Testing Matches" << endl;
}
void resetCopy();
};
#endif // GUESS_H_INCLUDED
Guess.cpp
#include <iostream> // needed for using cin/cou statements
#include <string> // needed for using string type
#include <fstream> // needed for using files
#include <ctime> // Enables use of time() function
#include <vector>
#include <stdlib.h>
#include "guess.h"
using namespace std;
Guess::Guess()
{//default constructor, fills with random numbers
for (int i = 0; i < 4; i++) {
randomNum = (rand() % 9)+1;
vecRandom.push_back(randomNum);
cout << "Random number " << i + 1 << ": " << vecRandom[i] << endl;
}
for (int i=0; i < 4; i++) { //creates a copy of the random numbers so I can manipulate it without
//losing the original random numbers
vecRandomCopy = vecRandom;
cout << "Random number copy " << i + 1 << ": " << vecRandomCopy[i] << endl;
}
}
//reset the random copy with original guess
void Guess::resetCopy()
{
vecRandomCopy = vecRandom;
}
//function for set the user guesses
void Guess::setUserGuess(int a, int b, int c, int d){
vecGuesses.clear(); //clears the previous values
vecGuesses.push_back(a);
vecGuesses.push_back(b);
vecGuesses.push_back(c);
vecGuesses.push_back(d);
cout << "You Guessed: ";
for (int i = 0; i < 4; i++) {
cout << " " << vecGuesses[i];
}
cout << endl;
}
//no need this function (upto you to delete)
void showVecGuesses(vector<int> _vecGuesses){
for (int i=0; i<4; i++){
cout << "TESTING SHOW VEC GUESSES" << endl;
cout << _vecGuesses[i] << endl;
}
}
//no need this function (upto you to delete)
void Matches (vector<int> _vecRandomCopy, vector<int>
_vecGuesses){
cout << "TESTING: do we get into matches?" << endl;
}
output:
I have fixed your code and mentioned the error in the code. some of your functions are ambiguous , can delete it if it has no further use.
if you have any issues please do comments . if you found this solutions useful, please give me thumbs up.