Advertisement
icyflamez96

Untitled

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