Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /*--------------------------------------------------------
  2. * Program 2: Mastermind Game
  3. *
  4. * Class: CS 141, Summer 2019
  5. * System: Windows 10, Codio.com
  6. * Author: Travis Lee
  7. *
  8. * --------------------------------------------------------
  9. */
  10.  
  11. #include <iostream>
  12. #include <stdlib.h>
  13. #include <iomanip>
  14.  
  15. using namespace std;
  16.  
  17.  
  18. int main()
  19. {
  20. //Declare Variables
  21. int origValue[3];
  22. int userInput;
  23. int inpArray[3];
  24. int turnCap = 11;
  25. int correctCount = 0;
  26. int wrongCount = 0;
  27. int zeroCount = 0;
  28. int nineCount = 0;
  29.  
  30. //Print out ID information (class, author)
  31. cout << "Class: CS 141, Summer 2019" << endl;
  32. cout << "Author: Travis Lee" << endl;
  33. cout << endl;
  34.  
  35. // Print out the program instructions and name of program
  36. cout << "Program 2: Mastermind" << endl;
  37. cout << "The program selects 3 distinct random digits 0..9." << endl;
  38. cout << "On each turn you guess 3 digits. The program indicates" << endl;
  39. cout << "how many are correct. You have 10 moves to guess the number." << endl;
  40. cout << "Good luck!" << endl;
  41. cout << endl;
  42. cout << "Input of 000 displays the hidden digits. Input of 999 exits the program" << endl;
  43.  
  44. //choose 3 digits from 0..9 at random and store them
  45. for(int i = 0; i < 3; ++i)
  46. {
  47. origValue[i] = rand() % 10;
  48. }
  49.  
  50.  
  51. //Display top of "Board"
  52. cout << "\t\t\tIn place Out of place" << endl;
  53. cout << "\t\t\t-------- --------" << endl;
  54.  
  55. //Main loop to run through the 10 turns
  56. for(int i = 1; i < turnCap; ++i)
  57. {
  58. cout << i << ". Your guess: ";
  59.  
  60. //Take in the user's input
  61. cin >> userInput;
  62.  
  63. //splits up the input and puts them into an array
  64. inpArray[0] = (userInput / 100) % 10;
  65. inpArray[1] = (userInput / 10) % 10;
  66. inpArray[2] = userInput % 10;
  67.  
  68. cout << "\tYou entered: ";
  69.  
  70. //Print user's input and check compares original value
  71. //with input value increments Count variables based off of result
  72. for(int c = 0; c < 3; ++c)
  73. {
  74. cout << inpArray[c];
  75. if(inpArray[c] == 9)
  76. {
  77. nineCount = nineCount + 1;
  78. }
  79. if(inpArray[c] == 0)
  80. {
  81. zeroCount = zeroCount + 1;
  82. }
  83. if(inpArray[c] == origValue[c])
  84. {
  85. correctCount = correctCount + 1;
  86. }
  87. else if(inpArray[0] == origValue[c])
  88. {
  89. wrongCount = wrongCount +1;
  90. }
  91. else if(inpArray[1] == origValue[c])
  92. {
  93. wrongCount = wrongCount +1;
  94. }
  95. else if(inpArray[2] == origValue[c])
  96. {
  97. wrongCount = wrongCount +1;
  98. }
  99.  
  100. }
  101.  
  102. //Exits program if user enters 999
  103. if(nineCount == 3)
  104. {
  105. cout << endl;
  106. cout << "Exiting program..." << endl;
  107. exit(0);
  108. }
  109.  
  110. //Prints the in place and out of place spots
  111. cout << " ";
  112. cout << correctCount << "\t\t";
  113. cout << wrongCount << endl;
  114.  
  115. //Exit the program and print out exiting statements if user guessed correctly
  116. if(correctCount == 3)
  117. {
  118. cout << endl;
  119. cout << "*** Congratulations! ***" << endl;
  120. cout << "Exiting program..." << endl;
  121. exit(0);
  122. }
  123.  
  124. //Resets correctCount and wrongCount
  125. if(zeroCount == 3)
  126. {
  127. cout << "\t\t\t Hidden digits: ";
  128. for(int i = 0; i < 3; ++i)
  129. {
  130. cout << origValue[i];
  131. }
  132. cout << endl;
  133. }
  134. correctCount = 0;
  135. wrongCount = 0;
  136.  
  137. //Resets nineCount so the program only ends when user
  138. //types 3 nines at the same time
  139. nineCount = 0;
  140.  
  141. //Resets zeroCount so the program only prints hidden digits
  142. //when user types three zeros at the same time
  143. zeroCount = 0;
  144.  
  145. }
  146.  
  147. //Prints program exit after the 10 turns from the loop above
  148. cout << " Better luck next time." << endl;
  149. cout << endl;
  150. cout << "Exiting program..." << endl;
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement