Advertisement
icyflamez96

Untitled

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