Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 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. 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. //keybool[k]
  133. std::cout << "counter incremented!" << " " << countIncorrect << " . " << std::endl;
  134. break;
  135. }
  136.  
  137. }
  138. }
  139. std::cout << "counter before modifying " << " " << countIncorrect << " . " << std::endl;
  140.  
  141.  
  142. /*for (int i = 0; i < keyBool.size(); i++)
  143. {
  144. if (keyBool[i] == guessBool[i])
  145. {
  146. countIncorrect--;
  147. std::cout << "decreased counter " << " " << countIncorrect << " . " << std::endl;
  148.  
  149. }
  150. }*/
  151. return countIncorrect;
  152.  
  153. /*if (numCorrect != 0)
  154. {
  155. countIncorrect = countIncorrect - (numCorrect % 2);
  156. }
  157.  
  158. if (countIncorrect < 0)
  159. {
  160. return 0;
  161. }
  162. else
  163. {
  164. return countIncorrect;
  165. } */
  166. }
  167.  
  168. //Returns code object length
  169. int Code::getLength()const
  170. {
  171. return length;
  172. }
  173.  
  174. //Returns code object key
  175. std::vector<int> Code::getKey() const
  176. {
  177. return key;
  178. }
  179.  
  180. //Prints out contained code
  181. void Code::printCode() const
  182. {
  183. std::string printString = "";
  184.  
  185. for (int i = 0; i < length; i++)
  186. {
  187. printString.append(std::to_string(key[i]));
  188. }
  189.  
  190. std::cout << "The secret code of length, " << length << ", and up to digit, " << maxDigit + 1<< ", is: " << printString << std::endl;
  191. }
  192.  
  193.  
  194.  
  195. int main()
  196. {
  197. int n, m, guess;
  198. std::cout << "Welcome to mastermind!" << std::endl;
  199. std::cout << "Please enter the code length" << std::endl;
  200. std::cin >> n;
  201. std::cout << "Please enter the code range" << std::endl;
  202. std::cin >> m;
  203.  
  204. Code keyCode = Code(n, m);
  205.  
  206. std::cout << "Please enter your guess" << std::endl;
  207. std::cin >> guess;
  208.  
  209.  
  210. std::stack<int> s;
  211. std::vector<int> guessVector;
  212. //Gets all digits of input and stores in stack lowest place first
  213. while (guess > 0)
  214. {
  215. s.push(guess % 10);
  216. guess = guess / 10;
  217. }
  218.  
  219. //Gets all digits in stack and builds guessVector from highest place first (top)
  220. while (!s.empty())
  221. {
  222. guessVector.push_back(s.top());
  223. s.pop();
  224. }
  225.  
  226. //Instantiate new Code object using second Code constructor
  227. Code guessCode = Code(guessVector);
  228.  
  229. //Print out how many correct digits are in the correct place
  230.  
  231. std::cout << "Number of correct digits in correct positions" << std::endl;
  232. int p = keyCode.checkCorrect(guessCode);
  233. std::cout << p << std::endl;
  234.  
  235. std::cout << "Number of correct digits in incorrect positions" << std::endl;
  236. int q = keyCode.checkIncorrect(guessCode, p);
  237. std::cout << q << std::endl;
  238.  
  239. std::cout << "Hit any key to escape" << std::endl;
  240. std::cin >> q;
  241.  
  242. //Print out how many correct digits are in the wrong place, no repeats
  243. //keyCode.checkIncorrect(guessCode);
  244.  
  245. //End of main
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement