Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. using namespace std;
  4.  
  5. void userInputCode()
  6. {};
  7. void userCreateCode(int code[], int num_digits, int int_code)
  8. {
  9. for (int i = num_digits - 1; i >= 0; i--) //Create code and store in array
  10. {
  11. code[i] = int_code % 10;
  12. int_code /= 10;
  13. }
  14. }
  15.  
  16. void randomCode(int code[], int random_pool[], int num_digits, int size)
  17. {
  18. for (int i = 0; i < num_digits; i++)
  19. {
  20. int index = rand() % size;
  21. code[i] = random_pool[index];
  22. random_pool[index] = random_pool[size - 1];
  23. size--;
  24. }
  25. }
  26.  
  27. void validateInput(int &cheat_code, int &num_digits) {
  28.  
  29. bool keep_going = true;
  30. while (keep_going)
  31. {
  32. cout << "Enter number of digits in code (3, 4, or 5): ";
  33. cin >> num_digits;
  34. switch (num_digits)
  35. {
  36. case 3:
  37. case 4:
  38. case 5:
  39. keep_going = false;
  40. break;
  41. case 0:
  42. cheat_code = 1;
  43. keep_going = false;
  44. break;
  45.  
  46. default:
  47. cout << "Please enter a valid input. " << endl;
  48. cout << endl;
  49. break;
  50. }
  51.  
  52. }
  53. }
  54.  
  55. int main(){
  56. srand(std::chrono::duration_cast<std::chrono::milliseconds>
  57. (std::chrono::system_clock::now().time_since_epoch()).count() % 2000000000);
  58.  
  59. const int CODE_SIZE = 5;
  60. const int NUMBER_POOL = 10;
  61. int cheat_code = 0;
  62. int size = 10;
  63. int int_code;
  64. int num_digits = -1;
  65. int code[CODE_SIZE];
  66. int random_pool[NUMBER_POOL] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  67. //get the number of digits for the game
  68. validateInput(cheat_code, num_digits);
  69. if (cheat_code == 1)
  70. {
  71. cout << "Enter code: ";
  72. cin >> int_code;
  73. cout << "Number of digits in code: ";
  74. cin >> num_digits; //Get inputs from cheater
  75. userCreateCode(code, num_digits, int_code);
  76. }
  77. else
  78. {
  79. //Generate code of distinct digits
  80. randomCode(code, random_pool, num_digits, size);
  81. }
  82.  
  83. //Output the correct code to guess with correct formatting
  84. cout << "Number to guess: ";
  85. for (int i = 0; i < num_digits; i++)
  86. {
  87. cout << code[i];
  88. if (i != num_digits - 1)
  89. cout << '-';
  90. }
  91. cout << endl;
  92.  
  93. int number;
  94. int user_code[5];
  95. int user_digits[5];
  96. int index = 0;
  97. bool keepGoing = true;
  98. bool found = false;
  99. while (keepGoing)
  100. {
  101. if (found)
  102. {
  103. cout << "ERROR: The digits of the coded must be distinct." << endl;
  104. }
  105. found = false;
  106. cout << "Enter a code to guess: ";
  107. cin >> number;
  108. cout << number << endl;
  109. while (number != 0)
  110. {
  111. int temp = number;
  112. if (temp / (pow(10, num_digits - 1)) >= 10)
  113. {
  114. cout << "ERROR: Code must not exceed the number of digits.";
  115. cout << endl;
  116. break;
  117. }
  118. else
  119. {
  120. user_digits[index] = number % 10;
  121. number /= 10;
  122. index++;
  123.  
  124. }
  125.  
  126. }
  127. int x = 0;
  128. int y = 0;
  129. for (x = 0; x < num_digits; x++)
  130. {
  131. for (y = x + 1; y < num_digits; y++)
  132. {
  133. cout << "y: " << user_digits[x] << "\tx: " << user_digits[y] << endl;
  134. if (user_digits[x] == user_digits[y])
  135. {
  136. cout << "DUPLICATE";
  137. cout << endl;
  138. found = true;
  139. }
  140. }
  141. }
  142. if (!found)
  143. keepGoing = false;
  144. }
  145.  
  146. //Output with zero at beginning
  147. int i = 0;
  148. for (int j = num_digits - 1; j >= 0; j--)
  149. {
  150. if (user_digits[num_digits - 1] == 0)
  151. {
  152. user_code[0] = 0;
  153. user_code[i + 1] = user_digits[j];
  154. }
  155. else
  156. user_code[i] = user_digits[j];
  157. i++;
  158. }
  159.  
  160.  
  161. int bull = 0;
  162. int cow = 0;
  163. for (int i = 0; i < num_digits; i++)
  164. {
  165. for (int j = 0; j < num_digits; j++)
  166. {
  167. if (user_code[i] == code[i])
  168. {
  169. bull++;
  170. i++;
  171. }
  172. else if (user_code[i] == code[j] && i != j)
  173. cow++;
  174. }
  175. }
  176. cout << '(' << bull << ") Bulls, and (" << cow << ") Cows";
  177.  
  178.  
  179.  
  180.  
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement