Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <vector>
- #include <iostream>
- #include <stdlib.h>
- #include <stack>
- //using namespace std;
- class Code
- {
- private:
- //Can I give default values here?
- int length = 0;
- int maxDigit = 0;
- std::vector<int> generateCode(int n, int m);
- // int Code::checkCorrect(Code guess);
- public:
- std::vector<int> key;
- Code(int n, int m);
- Code(std::vector<int>);
- int getLength() const;
- std::vector<int> getKey() const;
- int checkCorrect(Code guess) const;
- int checkIncorrect(Code guess, const int& numCorrect) const;
- void printCode() const;
- };
- //First Code object Constructor
- Code::Code(int n, int m)
- {
- length = n;
- maxDigit = m - 1;
- key = generateCode(length, maxDigit);
- printCode();
- }
- //Second Code object Constructor
- //Takes a user's guess of unknown length and maxDigit
- //Initializes guessVector to be used in checkCorrect() and checkIncorrect()
- Code::Code(std::vector<int> guessVector)
- {
- //Find length of guess
- length = guessVector.size();
- //Read each digit in the guessVector and find max digit
- for (int i = 0; i < length; i++)
- {
- if (guessVector[i] > maxDigit)
- maxDigit = guessVector[i];
- }
- key = guessVector;
- }
- std::vector<int> Code::generateCode(int n, int m)
- {
- std::vector<int> answerVector;
- for (int i = 0; i < n; i++)
- {
- int toAdd = rand() % m;
- answerVector.push_back(toAdd);
- }
- return answerVector;
- }
- //Returns number of correct digits in correct location
- int Code::checkCorrect(Code guess) const
- {
- int countCorrect = 0;
- int guessLength = guess.getLength();
- std::vector<int> guessKey = guess.getKey();
- for (int i = 0; i < guessLength; i++)
- {
- if (guessKey[i] == key[i])
- countCorrect++;
- }
- return countCorrect;
- }
- //*****Needs clarification
- //Returns number of correct digits in incorrect location
- //Does not double count digits
- int Code::checkIncorrect(Code guess, const int& numCorrect) const
- {
- int countIncorrect = 0;
- std::vector<int> guessKey = guess.getKey();
- // create boolean vector, change bool to true if value matches but not position
- std::vector<bool> keyBool(key.size(), false);
- // std::vector<bool> guessBool(guessKey.size(), false);
- /*for (unsigned int y = 0; y < key.size(); y++)
- {
- if (guessKey[y] == key[y])
- {
- guessKey[y] = null;
- }
- }*/
- // populate the boolean check vector first
- for (int i = 0; i < key.size(); i++)
- {
- if (guessKey[i] == key[i])
- {
- keyBool[i] = true;
- }
- else
- {
- keyBool[i] = false;
- }
- std::cout << "keyBool value" << ", " << keyBool[i] << std::endl;
- }
- for (unsigned int i = 0; i < key.size(); i++) // loops through index of key vector
- {
- std::cout << "next run" << std::endl; // print out size of key
- std::cout << "i " << i << ", " << "key[i] value " << key[i] << std::endl; // print out index and value of key at index
- for (unsigned int k = 0; k < guessKey.size(); k++) // loop through all indices of guess vector for every index of key vector
- {
- std::cout << "k " << k << ", " << "guessKey[k] value " << guessKey[k] << std::endl;
- if ((k != i) && (key[i] == guessKey[k]) && (keyBool[i]!= true))
- {
- std::cout << "key and guess value" << ", " << key[i] << ", " << guessKey[k] << std::endl;
- std::cout << "key and guess index" << ", " << i << ", " << k << std::endl;
- std::cout << "keyBool value " << keyBool[k] << std::endl;
- countIncorrect = countIncorrect++;
- //keybool[k]
- std::cout << "counter incremented!" << " " << countIncorrect << " . " << std::endl;
- break;
- }
- }
- }
- std::cout << "counter before modifying " << " " << countIncorrect << " . " << std::endl;
- /*for (int i = 0; i < keyBool.size(); i++)
- {
- if (keyBool[i] == guessBool[i])
- {
- countIncorrect--;
- std::cout << "decreased counter " << " " << countIncorrect << " . " << std::endl;
- }
- }*/
- return countIncorrect;
- /*if (numCorrect != 0)
- {
- countIncorrect = countIncorrect - (numCorrect % 2);
- }
- if (countIncorrect < 0)
- {
- return 0;
- }
- else
- {
- return countIncorrect;
- } */
- }
- //Returns code object length
- int Code::getLength()const
- {
- return length;
- }
- //Returns code object key
- std::vector<int> Code::getKey() const
- {
- return key;
- }
- //Prints out contained code
- void Code::printCode() const
- {
- std::string printString = "";
- for (int i = 0; i < length; i++)
- {
- printString.append(std::to_string(key[i]));
- }
- std::cout << "The secret code of length, " << length << ", and up to digit, " << maxDigit + 1<< ", is: " << printString << std::endl;
- }
- int main()
- {
- int n, m, guess;
- std::cout << "Welcome to mastermind!" << std::endl;
- std::cout << "Please enter the code length" << std::endl;
- std::cin >> n;
- std::cout << "Please enter the code range" << std::endl;
- std::cin >> m;
- Code keyCode = Code(n, m);
- std::cout << "Please enter your guess" << std::endl;
- std::cin >> guess;
- std::stack<int> s;
- std::vector<int> guessVector;
- //Gets all digits of input and stores in stack lowest place first
- while (guess > 0)
- {
- s.push(guess % 10);
- guess = guess / 10;
- }
- //Gets all digits in stack and builds guessVector from highest place first (top)
- while (!s.empty())
- {
- guessVector.push_back(s.top());
- s.pop();
- }
- //Instantiate new Code object using second Code constructor
- Code guessCode = Code(guessVector);
- //Print out how many correct digits are in the correct place
- std::cout << "Number of correct digits in correct positions" << std::endl;
- int p = keyCode.checkCorrect(guessCode);
- std::cout << p << std::endl;
- std::cout << "Number of correct digits in incorrect positions" << std::endl;
- int q = keyCode.checkIncorrect(guessCode, p);
- std::cout << q << std::endl;
- std::cout << "Hit any key to escape" << std::endl;
- std::cin >> q;
- //Print out how many correct digits are in the wrong place, no repeats
- //keyCode.checkIncorrect(guessCode);
- //End of main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement