Advertisement
icyflamez96

Untitled

Oct 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <iomanip>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. //passes in the appropriate values to set the board for the difficulty
  11. //selected by the user.
  12. const string begnner(4, '.'),
  13. easy(6, '.'),
  14. interm(7, '.'),
  15. hard(8, '.');
  16.  
  17. //Function prototypes
  18. string setBoard(string user, string &choices);
  19. string checkInput(int, bool&);
  20. void printInstructions();
  21.  
  22. int main()
  23. {
  24. string guess; //the player's guess will be stored
  25. string spot(4,'.'); //where letters will be randomly assigned
  26. string chkSpot; //checks how accurate the player's guess is
  27. string again="y"; //variable that user input will determine if he plays again
  28. string choices; //Where list of valid choices will be stored for the difficulty
  29. int numOf; //number of valid choices
  30. bool win; //Keeps game in a loop until character wins.
  31. bool quit;
  32. string dif; //variable where user input to determine difficulty will be stored
  33. string accura=(4, " ");
  34. int oCount=0;
  35. int oOCount=0;
  36. string accuraO = (4, "");
  37.  
  38. RESTART:
  39. do{
  40. //sets win = false so that the game will keep looping until player
  41. //wins and/or decides to quit the program.
  42. win=false;
  43. quit=false;
  44.  
  45. //Prompts user to select difficulty or leave program.
  46. cout<<"Welcome to MASTERMIND!"<<endl;
  47. cout<<"What difficulty do you want to select?"<<endl;
  48. cout<<"1 = Beginner"<<endl;
  49. cout<<"2 = Easy"<<endl;
  50. cout<<"3 = Intermediate"<<endl;
  51. cout<<"4 = Hard"<<endl;
  52. cout<<"9 = INSTRUCTIONS"<<endl;
  53. cout<<""<<endl;
  54. cout<<"Input anything else to quit program."<<endl;
  55. getline(cin,dif);
  56.  
  57. //Passes in strings of a length relevant to the difficulty selected.
  58. switch (dif[0])
  59. {
  60. case '1': {spot=setBoard(begnner, choices);
  61. numOf=4;
  62. break;}
  63. case '2': {spot=setBoard(easy, choices);
  64. numOf=6;
  65. break;}
  66. case '3': {spot=setBoard(interm, choices);
  67. numOf=7;
  68. break;}
  69. case '4': {spot=setBoard(hard, choices);
  70. numOf=8;
  71. break;}
  72. case '9': {printInstructions();
  73. goto RESTART;
  74. break;}
  75. default: {
  76. win=true;
  77. again="n";
  78. }
  79. }
  80.  
  81. do{
  82. //this if will skip the whole block that checks the user's input for the
  83. //right letters if user decides to quit.
  84. if (again!="n"){
  85. guess=checkInput(numOf, quit);
  86. if(quit==true)
  87. {
  88. break;
  89. }
  90. //for loop checks each spot to see how it corresponds with the player's
  91. //guess, and then will assign O or X to a variable that will output
  92. //during the game so that the player can know how accurate his guess was
  93. for (int j=0; j<4; j++)
  94. {
  95. (guess[j]==spot[j]) ? (chkSpot[j]='O') : (chkSpot[j]='X');
  96.  
  97. if(guess[j]==spot[j])
  98. {
  99. accura+="O";
  100. oOCount++;
  101. }
  102. else if ((spot.find(guess[j])!= string::npos)&&guess[j]!=spot[j])
  103. {
  104. accura+="o";
  105. oCount++;
  106. }
  107. else if (spot.find(guess[j])==string::npos)
  108. {
  109. accura+=" ";
  110. }
  111. }
  112.  
  113. for(int i=0; i<oCount; i++)
  114. {
  115. accuraO+="o";
  116. }
  117.  
  118. for(int i=0; i<oOCount; i++)
  119. {
  120. accuraO+="O";
  121. }
  122.  
  123. //if statement checks if user guessed the letters assigned to each
  124. //spot on the board correctly. If the player is successful, tell em.
  125. //if not, tell em.
  126. if (chkSpot[0]=='O'&&chkSpot[1]=='O'&&chkSpot[2]=='O'&&chkSpot[3]=='O')
  127. {
  128. cout<<"YOU WIN"<<endl;
  129.  
  130. cout<<"|| [OOOO] || ";
  131. cout<<"== [OOOO]"<<endl;
  132. cout<<endl;
  133. win=true;
  134. cout<<"Would you like to play again on a new board? Enter 'y' for yes, ";
  135. cout<<"or anything else to quit."<<endl;
  136. getline(cin,again);
  137. }
  138. else
  139. {
  140. //Outputs the board to show what the user got right and the
  141. //list of their choices
  142. cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
  143. if(numOf!=4){
  144. cout<<"== ["<<accuraO<<"]";
  145. }
  146. else
  147. {
  148.  
  149. cout<<"== ["<<chkSpot[0]<<chkSpot[1]<<chkSpot[2]<<chkSpot[3]<<"]";
  150. }
  151. cout<<" -- CHOICES: "<<choices<<" -- Enter '1' to return to the menu."<<endl;
  152. }
  153. oCount=0;
  154. oOCount=0;
  155. accuraO = (4, "");
  156. accura = (4, "");
  157. }
  158. }while(win==false); //loop will keep players guessing until they win.
  159. }while(again=="y"||again=="Y"); //loop will keep players in menu until they-
  160. //decide to quit
  161. return 0;
  162. }//end of main
  163.  
  164. string setBoard(string user, string &choices){
  165. string spot(user.size(),'.'); //where letters will be randomly assigned
  166. string chkSpot; //checks how accurate the player's guess is
  167. int boardSet; //where random number assignment (1-4) will be stored
  168. int repeat[user.size()]={}; //tests for number repetition
  169. bool exists=false;
  170. srand(time(NULL));
  171.  
  172. //Will assign list of valid choices depending on what difficulty the user
  173. //chooses
  174. switch(user.size())
  175. {
  176. case 4: {choices="Q W A S"; break;}
  177. case 6: {choices="Q W A S E D"; break;}
  178. case 7: {choices="Q W A S E D R"; break;}
  179. case 8: {choices="Q W A S E D R F"; break;}
  180. }
  181.  
  182. //for loop for randomizing the board
  183. for (int i=0; i<4; i++)
  184. {
  185. //sets exist to true at the start of each loop.
  186. exists=true;
  187.  
  188. //gets a random number in-between 1 and the number denoting
  189. //the length of the string passed in by the user selected difficulty
  190. boardSet=(rand() % user.size()) + 1;
  191.  
  192. //Tests a user character to see if it's already been used
  193. //(AKA, if it's already been stored in repeat[])
  194. for(int j=0; j<4; j++)
  195. {
  196. if(boardSet==repeat[j])
  197. {
  198. //if there is a repeat variable, set exists to false
  199. exists=false;
  200. }
  201. }
  202.  
  203. //first if test will test if the random number is identical to any previous random number.
  204. //if the random number IS equal to any previous ones, it will decrement 'i'
  205. if (exists==false)
  206. {
  207. //(bgrList.find(guess[i])== string::npos) (PREVIOUS VERSION ^)
  208.  
  209. //decrements i so that the for loop will run an extra loop so that it can
  210. //assign a number that hasn't already been taken
  211. i--;
  212. }
  213. //if the random number is unique, it assigns it to a spot on the board
  214. else{
  215. //each number 1-4 corresponds to a letter that will be assigned to a spot
  216. //on the board
  217. switch (boardSet)
  218. {
  219. case 1: {
  220. spot[i]='Q'; //Assigns Q to a spot
  221. chkSpot[i]=spot[i]; //Assigns Q to check for if the user guessed correctly.
  222. repeat[i]=boardSet; //assigns number 1 in repeat variable so it can test for future repeats
  223. break;
  224. }
  225. case 2: {
  226. spot[i]='W'; //Assigns W to a spot
  227. chkSpot[i]=spot[i]; //Assigns W to check for if the user guessed correctly.
  228. repeat[i]=boardSet; //assigns number 2 in repeat variable so it can test for future repeats
  229. break;
  230. }
  231. case 3: {
  232. spot[i]='A'; //Assigns A to a spot
  233. chkSpot[i]=spot[i]; //Assigns A to check for if the user guessed correctly.
  234. repeat[i]=boardSet; //assigns number 3 in repeat variable so it can test for future repeats
  235. break;
  236. }
  237. case 4: {
  238. spot[i]='S'; //Assigns S to a spot
  239. chkSpot[i]=spot[i]; //Assigns S to check for if the user guessed correctly.
  240. repeat[i]=boardSet; //assigns number 4 in repeat variable so it can test for future repeats
  241. break;
  242. }
  243. case 5: {
  244. spot[i]='E'; //Assigns E to a spot
  245. chkSpot[i]=spot[i]; //Assigns E to check for if the user guessed correctly.
  246. repeat[i]=boardSet; //assigns number 5 in repeat variable so it can test for future repeats
  247. break;
  248. }
  249. case 6: {
  250. spot[i]='D'; //Assigns D to a spot
  251. chkSpot[i]=spot[i]; //Assigns D to check for if the user guessed correctly.
  252. repeat[i]=boardSet; //assigns number 6 in repeat variable so it can test for future repeats
  253. break;
  254. }
  255. case 7: {
  256. spot[i]='R'; //Assigns R to a spot
  257. chkSpot[i]=spot[i]; //Assigns R to check for if the user guessed correctly.
  258. repeat[i]=boardSet; //assigns number 7 in repeat variable so it can test for future repeats
  259. break;
  260. }
  261. case 8: {
  262. spot[i]='F'; //Assigns F to a spot
  263. chkSpot[i]=spot[i]; //Assigns green to check for if the user guessed correctly.
  264. repeat[i]=boardSet; //assigns number 8 in repeat variable so it can test for future repeats
  265. break;
  266. }
  267. }//ends switch
  268. }//ends else statement
  269.  
  270. }//ends for loop
  271. //outputs the board
  272. cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
  273. cout<<"== [????]";
  274. cout<<" -- CHOICES: "<<choices<<" -- Enter '1' to return to the menu."<<endl;
  275.  
  276. return spot; //returns the string which holds the positions of the letters
  277. //on the board.
  278. }
  279.  
  280. string checkInput(int num, bool &quit)
  281. {
  282. bool check; //variable to check if input is valid
  283. string guess; //user's guess
  284. string vldList; //where valid list of characters will be stored.
  285. string repeat=(4, "");
  286. //assigns the string of valid characters to a variable that will be searched
  287. //when it comes to checking if the user input is valid
  288. switch(num)
  289. {
  290. case 4: {vldList="QWASqwas1"; break;}
  291. case 6: {vldList="QWASEDqwased1"; break;}
  292. case 7: {vldList="QWASEDRqwasedr1"; break;}
  293. case 8: {vldList="QWASEDRFqwasedrf1"; break;}
  294. }
  295.  
  296. do{
  297. //if nothing is invalid about the user input, this bool allows
  298. //the program to pass through the loop
  299. check=true;
  300.  
  301. //Prompts user to enter guess
  302. cout<<"Enter: ";
  303. getline(cin,guess);
  304.  
  305. if(guess=="1")
  306. {
  307. quit=true;
  308. }
  309.  
  310. //Checks for valid input
  311. while((guess.size()!=4)&&(guess!="1"))
  312. {
  313. cout<<"Invalid input. Please type in the four valid letters only. NO REPEATS"<<endl;
  314. getline(cin,guess);
  315. }
  316.  
  317. //converts lowercase inputs to uppercase
  318. for(int u=0; u<4; u++){
  319. switch(guess[u])
  320. {
  321. case 'q': {guess[u]='Q'; break;}
  322. case 'w': {guess[u]='W'; break;}
  323. case 'a': {guess[u]='A'; break;}
  324. case 's': {guess[u]='S'; break;}
  325. case 'e': {guess[u]='E'; break;}
  326. case 'd': {guess[u]='D'; break;}
  327. case 'r': {guess[u]='R'; break;}
  328. case 'f': {guess[u]='F'; break;}
  329. }
  330. }
  331.  
  332. //Goes through the user inputted characters one by one, searches
  333. //to see if any character doesn't match up with the valid list of
  334. //characters.
  335. for(unsigned int i=0; i<guess.size(); i++)
  336. {
  337. if (vldList.find(guess[i])== string::npos)
  338. {
  339. check=false;
  340. }
  341. }
  342.  
  343.  
  344. for (unsigned i=0; i<guess.size()-1; i++)
  345. { if (guess.find(guess[i], i+1) != string::npos)
  346. check=false;
  347. }
  348.  
  349.  
  350.  
  351. //if any erroneous character was found in previous for loop, this while
  352. //will run giving the player an invalid input message
  353. if(check==false)
  354. {
  355. cout<<"Invalid input. Please type in the four valid letters only. NO REPEATS"<<endl;
  356. }
  357.  
  358.  
  359. }while(check==false); //will loop if the input was invalid
  360. return guess; //returns the user's BACKGROUND CHECKED guess
  361. }
  362.  
  363. void printInstructions(){
  364. string cont, contents;
  365. ifstream textFile;
  366.  
  367. textFile.open("instructions.txt");
  368. if (textFile) { //If file opened successfully, continue with what you wanna do
  369. while (textFile >> contents) { //While there is MORE input to read from the file, do whatever
  370. cout << contents <<" "; //display 'contents'
  371. }
  372. } else cout << "Error opening file."; //If it doesn't open, display error
  373.  
  374. //I put this after it's done looping with the files. You close and clear after evertime it reads a word, which isn't good
  375. textFile.close();
  376. textFile.clear(); //Lehr said how the book doesn't mention clear, but always do this
  377.  
  378. cout<<endl;
  379. cout<<endl;
  380. cout<<"Enter anything to continue: ";
  381. getline(cin,cont);
  382.  
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement