Game evaluation - Mastermind

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 4
Reputation: Rachmaninov is an unknown quantity at this point 
Solved Threads: 0
Rachmaninov Rachmaninov is offline Offline
Newbie Poster

Game evaluation - Mastermind

 
0
  #1
Oct 5th, 2008
I made the game Mastermind using C++ and would like anyone who wants to test it and let me know what they think. It comes with instructions, but since I wrote them and I know how to play the game, they might not be clear.

Note: If this post is not supposed to go here feel free to delete or whatever. If there is a better place for stuff like this to go, please let me know.

  1. #include <iostream>
  2. #include <ctime> // For time()
  3. #include <cstdlib> // For srand() and rand()
  4.  
  5. using namespace std;
  6.  
  7. void displayIntro();
  8. void prompt(char guess[], int n);
  9. bool gaveOver(char guess[], char right[]);
  10. char convertToColor(int num);
  11.  
  12. // #######################################################
  13. int main(void)
  14. {
  15. char symbol;
  16. char guess[4];
  17. char right[4];
  18. bool playAgain = true;
  19. bool won;
  20. int numGuesses;
  21.  
  22. displayIntro();
  23.  
  24. while (playAgain) {
  25. won = false;
  26. numGuesses = 10;
  27.  
  28. cout << endl;
  29. // generate the correct random sequence
  30. srand(time(0));
  31. for (int i = 0; i < 4; i++)
  32. {
  33. int temp = (rand() % 6) + 1;
  34. right[i] = convertToColor(temp);
  35. //cout << right[i] << " ";
  36. }
  37. cout << endl;
  38.  
  39. // loop until the user gets the right value
  40. while (!won && numGuesses > 0)
  41. {
  42. prompt(guess, numGuesses);
  43. won = gaveOver(guess, right);
  44. numGuesses--;
  45. }
  46.  
  47. if (won)
  48. {
  49. cout << "You win! The correct sequence was " << right[0] << " "
  50. << right[1] << " " << right[2] << " " << right[3] << endl;
  51. }
  52. else
  53. {
  54. cout << "You lost! The correct sequence was " << right[0] << " "
  55. << right[1] << " " << right[2] << " " << right[3] << endl;
  56. }
  57.  
  58. cout << "Would you like to play again (Y or N): ";
  59.  
  60. cin >> symbol;
  61.  
  62. playAgain = (symbol == 'Y' || symbol == 'y') ? true : false;
  63. }
  64.  
  65. return 0;
  66. } // end main
  67.  
  68. // #######################################################
  69. bool gaveOver(char guess[], char right[])
  70. {
  71. int totalRight = 0;
  72. int rightColor = 0;
  73. int grab;
  74. bool exclude[4];
  75. bool excludeColor[4];
  76. bool inList = false;
  77.  
  78. for (int i = 0; i < 4; i++)
  79. {
  80. exclude[i] = false;
  81. excludeColor[i] = false;
  82. }
  83.  
  84. // loops to determine which have both the correct color and position
  85. for (int i = 0; i < 4; i++)
  86. {
  87. if (guess[i] == right[i])
  88. {
  89. totalRight++;
  90. exclude[i] = true;
  91. }
  92. }
  93.  
  94. // loops to determine if any colors are correct, just in the wrong position
  95. for (int i = 0; i < 4; i++)
  96. {
  97. if (!exclude[i])
  98. {
  99. for (int j = 0; j < 4; j++)
  100. {
  101. if (!exclude[j] && i != j)
  102. {
  103. if ((guess[i] == right[j]) && !excludeColor[j])
  104. {
  105. inList = true;
  106. grab = j;
  107. }
  108. }
  109. }
  110.  
  111. if (inList)
  112. {
  113. rightColor++;
  114. inList = false;
  115. excludeColor[grab] = true;
  116. }
  117. }
  118.  
  119.  
  120.  
  121. }
  122.  
  123. if (totalRight == 4)
  124. return true;
  125. else
  126. {
  127. cout << totalRight << ", " << rightColor << endl;
  128. return false;
  129. }
  130.  
  131. } // end gameOver
  132.  
  133.  
  134. // #######################################################
  135. void prompt(char guess[], int n)
  136. {
  137. cout << "Enter your guess (" << n << "): ";
  138.  
  139. cin >> guess[0] >> guess[1] >> guess[2] >> guess[3];
  140.  
  141.  
  142. for (int i = 0; i < 4; i++)
  143. {
  144. if (guess[i] > 91) // lower case level
  145. guess[i] = guess[i] - 32;
  146. }
  147.  
  148. } // end prompt
  149.  
  150. // #######################################################
  151. char convertToColor(int num)
  152. {
  153. switch (num)
  154. {
  155. case 1:
  156. return static_cast<char>(66);
  157. case 2:
  158. return static_cast<char>(71);
  159. case 3:
  160. return static_cast<char>(79);
  161. case 4:
  162. return static_cast<char>(80);
  163. case 5:
  164. return static_cast<char>(82);
  165. case 6:
  166. return static_cast<char>(89);
  167. }
  168. } // end convertToColor
  169.  
  170. // #######################################################
  171. void displayIntro()
  172. {
  173. cout << "======================= Welcome to Mastermind =======================" << endl;
  174. cout << "Mastermind is a game of logic." << endl;
  175. cout << "The goal is to use logic to guess the correct four color combination" << endl;
  176. cout << "by using your previous guesses as clues." << endl;
  177. cout << "Everytime you guess, you will be told how close to the goal you are." << endl;
  178. cout << "The first number says how many were in the right place. " << endl;
  179. cout << "The second number says how many of the right colors you have in the wrong place.";
  180. cout << "To guess, enter the first letter of each color." << endl;
  181. cout << "Available colors are Blue, Green, Orange, Purple, Red and Yellow." << endl;
  182. cout << "An sample guess would be look like this: R R G B" << endl;
  183. cout << "=====================================================================";
  184. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Game evaluation - Mastermind

 
0
  #2
Oct 5th, 2008
Game works great! I tested it and ended up playing it for far longer than I intended. It seems to work fine. The only thing I'd change, if you wanted to improve it, is to have some data validation, as in if users enter in illegal characters or not exactly four characters, the game tells them what they did wrong and prompts them to guess again. Also, you only need to seed the random number generator once. Right now you do it every game in your while loop. Just seed it once at the top. Other than that, perfect.
Last edited by VernonDozier; Oct 5th, 2008 at 2:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: Rachmaninov is an unknown quantity at this point 
Solved Threads: 0
Rachmaninov Rachmaninov is offline Offline
Newbie Poster

Re: Game evaluation - Mastermind

 
0
  #3
Oct 5th, 2008
Well, I already added support for upper and lower case. Do you have any ideas for fixing if the user doesn't insert four characters? I haven't used C++ for very long, and as of now the program will keep on asking for characters until there are four, even if the user presses enter after every time they enter a character.

Thanks for the suggestion about seeding random numbers, I'll change that too.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Game evaluation - Mastermind

 
0
  #4
Oct 6th, 2008
Originally Posted by Rachmaninov View Post
Well, I already added support for upper and lower case. Do you have any ideas for fixing if the user doesn't insert four characters? I haven't used C++ for very long, and as of now the program will keep on asking for characters until there are four, even if the user presses enter after every time they enter a character.

Thanks for the suggestion about seeding random numbers, I'll change that too.
If the user enters MORE than 4 characters, there is a definite problem because those characters are stored in the buffer. Try entering "bbbbbbbb" and you'll notice that the computer doesn't wait for your answer on the next question because it takes the last four b's as the answer, even though you typed them in response to an earlier question.

If you want to check for valid input, I would read it in as a string. First check, make sure it's exactly four characters using the length function. If not, give an error message. You can also use the toupper function to convert from lower to upper case. Right now you are subtracting 32, which works too. Then you can go through the 4 characters and make sure they are each one of the six legal characters, and if any of those tests fail, display an error message and prompt the user to try again. If they all work, convert the string to a char array and return from your prompt function.

It's extra work and you'll have to decide whether it is worth it, but it might make it a better game since if the user accidentally types in 5 characters or something and doesn't notice, it could be very frustrating since he could type in the correct answer on a line, but the program would say it was wrong because of the leftover characters. Another way to solve that particular problem would be to flush the buffer after each guess.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC