Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <stack>
  6. //using namespace std;
  7.  
  8. class Code
  9. {
  10. private:
  11.     //Can I give default values here?
  12.     int length = 0;
  13.     int maxDigit = 0;
  14.     std::vector<int> generateCode(int n, int m);
  15.  
  16.     // int Code::checkCorrect(Code guess);
  17.  
  18. public:
  19.     std::vector<int> key;
  20.     Code(int n, int m);
  21.     Code(std::vector<int>);
  22.     int getLength() const;
  23.     std::vector<int> getKey() const;
  24.     int checkCorrect(Code guess) const;
  25.     int checkIncorrect(Code guess, const int& numCorrect) const;
  26.     void printCode() const;
  27. };
  28.  
  29. //First Code object Constructor
  30. Code::Code(int n, int m)
  31. {
  32.     length = n;
  33.     maxDigit = m - 1;
  34.     key = generateCode(length, maxDigit);
  35.     void printCode();
  36. }
  37.  
  38. //Second Code object Constructor
  39. //Takes a user's guess of unknown length and maxDigit
  40. //Initializes guessVector to be used in checkCorrect() and checkIncorrect()
  41. Code::Code(std::vector<int> guessVector)
  42. {
  43.     //Find length of guess
  44.     length = guessVector.size();
  45.  
  46.     //Read each digit in the guessVector and find max digit
  47.     for (int i = 0; i < length; i++)
  48.     {
  49.         if (guessVector[i] > maxDigit)
  50.             maxDigit = guessVector[i];
  51.     }
  52.     key = guessVector;
  53. }
  54.  
  55. std::vector<int> Code::generateCode(int n, int m)
  56. {
  57.     std::vector<int> answerVector;
  58.  
  59.     for (int i = 0; i < n; i++)
  60.     {
  61.         int toAdd = rand() % m;
  62.         answerVector.push_back(toAdd);
  63.     }
  64.  
  65.     return answerVector;
  66. }
  67.  
  68. //Returns number of correct digits in correct location
  69. int Code::checkCorrect(Code guess) const
  70. {
  71.     int countCorrect = 0;
  72.     int guessLength = guess.getLength();
  73.     std::vector<int> guessKey = guess.getKey();
  74.  
  75.     for (int i = 0; i < guessLength; i++)
  76.     {
  77.         if (guessKey[i] == key[i])
  78.             countCorrect++;
  79.     }
  80.  
  81.     return countCorrect;
  82. }
  83.  
  84. //*****Needs clarification
  85. //Returns number of correct digits in incorrect location
  86. //Does not double count digits
  87. int Code::checkIncorrect(Code guess, const int& numCorrect) const
  88. {
  89.     int countIncorrect = 0;
  90.     std::vector<int> guessKey = guess.getKey();
  91.     // create boolean vector, change bool to true if value matches but not position
  92.     std::vector<bool> keyBool(key.size(), false);
  93.     // std::vector<bool> guessBool(guessKey.size(), false);
  94.     /*for (unsigned int y = 0; y < key.size(); y++)
  95.     {
  96.         if (guessKey[y] == key[y])
  97.         {
  98.             guessKey[y] = null;
  99.         }
  100.     }*/
  101.  
  102.     // populate the boolean check vector first
  103.  
  104.     for (int i = 0; i < key.size(); i++)
  105.     {
  106.         if (guessKey[i] == key[i])
  107.         {
  108.             keyBool[i] = true;
  109.         }
  110.         else
  111.         {
  112.             keyBool[i] = false;
  113.         }
  114.         //std::cout << "keyBool value" << ", " << keyBool[i] << std::endl;
  115.     }
  116.  
  117.     for (unsigned int i = 0; i < key.size(); i++)   // loops through index of key vector
  118.     {
  119.         //std::cout << "next run" << std::endl;  // print out size of key
  120.         //std::cout << "i " << i << ", " <<  "key[i] value " << key[i]  << std::endl;   // print out index and value of key at index
  121.  
  122.         for (unsigned int k = 0; k < guessKey.size(); k++)  // loop through all indices of guess vector for every index of key vector
  123.         {
  124.             //std::cout << "k " << k << ", " << "guessKey[k] value " << guessKey[k] << std::endl;
  125.  
  126.             if ((k != i) && (key[i] == guessKey[k]) && (keyBool[i]!= true))
  127.             {
  128.                 //std::cout << "key and guess value" << ", " << key[i] << ", " << guessKey[k] << std::endl;
  129.                 //std::cout << "key and guess index" << ", " << i << ", " << k << std::endl;
  130.                 //std::cout << "keyBool value " << keyBool[k] << std::endl;
  131.                 countIncorrect = countIncorrect++;
  132.                 //std::cout << "counter incremented!" << " " << countIncorrect << " . " << std::endl;
  133.                 break;
  134.             }
  135.  
  136.         }
  137.     }
  138.     //std::cout << "counter before modifying " << " " << countIncorrect << " . " << std::endl;
  139.  
  140.  
  141.     /*for (int i = 0; i < keyBool.size(); i++)
  142.     {
  143.         if (keyBool[i] == guessBool[i])
  144.         {
  145.             countIncorrect--;
  146.             std::cout << "decreased counter " << " " << countIncorrect << " . " << std::endl;
  147.  
  148.         }
  149.     }*/
  150.     return countIncorrect;
  151.  
  152.     /*if (numCorrect != 0)
  153.     {
  154.         countIncorrect = countIncorrect - (numCorrect % 2);
  155.     }
  156.    
  157.     if (countIncorrect < 0)
  158.     {
  159.         return 0;
  160.     }
  161.     else
  162.     {
  163.         return countIncorrect;
  164.     } */
  165. }
  166.  
  167. //Returns code object length
  168. int Code::getLength()const
  169. {
  170.     return length;
  171. }
  172.  
  173. //Returns code object key
  174. std::vector<int> Code::getKey() const
  175. {
  176.     return key;
  177. }
  178.  
  179. //Prints out contained code
  180. void Code::printCode() const
  181. {
  182.     std::string printString = "";
  183.  
  184.     for (int i = 0; i < length; i++)
  185.     {
  186.         printString.append(std::to_string(key[i]));
  187.     }
  188.  
  189.     std::cout << "The secret code of length, " << length << ", and up to digit, " << maxDigit + 1<< ", is: " << printString << std::endl;
  190. }
  191.  
  192.  
  193.  
  194. int main()
  195. {
  196.     int n, m, guess;
  197.     std::cout << "Welcome to mastermind!" << std::endl;
  198.     std::cout << "Please enter the code length" << std::endl;
  199.     std::cin >> n;
  200.     std::cout << "Please enter the code range" << std::endl;
  201.     std::cin >> m;
  202.  
  203.     Code keyCode = Code(n, m);
  204.  
  205.     std::cout << "Please enter your guess" << std::endl;
  206.     std::cin >> guess;
  207.  
  208.  
  209.     std::stack<int> s;
  210.     std::vector<int> guessVector;
  211.     //Gets all digits of input and stores in stack lowest place first
  212.     while (guess > 0)
  213.     {
  214.         s.push(guess % 10);
  215.         guess = guess / 10;
  216.     }
  217.  
  218.     //Gets all digits in stack and builds guessVector from highest place first (top)
  219.     while (!s.empty())
  220.     {
  221.         guessVector.push_back(s.top());
  222.         s.pop();
  223.     }
  224.  
  225.     //Instantiate new Code object using second Code constructor
  226.     Code guessCode = Code(guessVector);
  227.  
  228.     //Print out how many correct digits are in the correct place
  229.  
  230.     std::cout << "Number of correct digits in correct positions" << std::endl;
  231.     int p = keyCode.checkCorrect(guessCode);
  232.     std::cout << p << std::endl;
  233.  
  234.     std::cout << "Number of correct digits in incorrect positions" << std::endl;
  235.     int q = keyCode.checkIncorrect(guessCode, p);
  236.     std::cout << q << std::endl;
  237.  
  238.     keyCode.printCode();
  239.  
  240.     std::cout << "Hit any key to escape" << std::endl;
  241.     std::cin >> q;
  242.  
  243.     //Print out how many correct digits are in the wrong place, no repeats
  244.     //keyCode.checkIncorrect(guessCode);
  245.  
  246.     //End of main
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement