Guest User

Untitled

a guest
Dec 12th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.29 KB | None | 0 0
  1. #include <iostream> //input/output objects
  2. #include <string> //enables string data type
  3. #include <cstdlib> //stadndard library
  4. #include <ctime> //enables rand
  5. #include <iomanip> //enables stream manipulation
  6. #include <fstream> //enables file io
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. //Global Constants - Array Size Initialization
  12. const int ROW=11,
  13. COL=2;
  14.  
  15. //Function prototypes
  16. string setBoard(string user, string&);
  17. string checkInput(int, bool&, string&);
  18. void printInstructions();
  19. void saveScore(float, string);
  20. void printScore(float, string);
  21. void sortScores(string[][COL], int);
  22.  
  23. //execution begins here
  24. int main()
  25. {
  26. //passes in the appropriate values to set the board for the difficulty
  27. //selected by the user.
  28. string begnner(4, '.'),
  29. easy(6, '.'),
  30. interm(7, '.'),
  31. hard(8, '.');
  32. string guess, //the player's guess will be stored
  33. spot(4,'.'), //where letters will be randomly assigned
  34. chkSpot, //checks how accurate the player's guess is
  35. again="y", //variable that user input will determine if he plays again
  36. choices, //Where list of valid choices will be stored for the difficulty
  37. accuraO = (4,""), //where Os and os will be sorted
  38. accura=(4,""), //Where Os and os will be stored
  39. dif, //variable where user input to determine difficulty will be stored
  40. cont,
  41. name; //User will enter their name to save their score.
  42. bool win, //Keeps game in a loop until character wins.
  43. quit; //keeps menu in loop until user chooses to quit
  44. int numOf, //number of valid choices
  45. oCount=0, //where number of os will be stored
  46. oOCount=0; //where number of Os will be stored
  47. float score=0; //where score will be kept
  48. ifstream file; //Where high-score data will be read from.
  49.  
  50. RESTART: //will take back to beginning of menu loop
  51. do{
  52. //sets win = false so that the game will keep looping until player
  53. //wins and/or decides to quit the program.
  54. win=false;
  55.  
  56. //sets quit to false so menu will keep looping until player decides to
  57. //leave
  58. quit=false;
  59.  
  60. //Prompts user to select difficulty or leave program.
  61. cout<<"Welcome to MASTERMIND!"<<endl;
  62. cout<<"What difficulty do you want to select?"<<endl;
  63. cout<<"1 = Beginner"<<endl;
  64. cout<<"2 = Easy"<<endl;
  65. cout<<"3 = Intermediate"<<endl;
  66. cout<<"4 = Hard"<<endl;
  67. cout<<""<<endl;
  68. cout<<"5 = VIEW HIGH-SCORES!"<<endl;
  69. cout<<"9 = INSTRUCTIONS"<<endl;
  70. cout<<"- = DELETE HIGH-SCORES!"<<endl;
  71. cout<<"+ = Cheat! (Only works when you input it during a game)"<<endl;
  72. cout<<""<<endl;
  73. cout<<"Input anything else to quit program: ";
  74. getline(cin,dif);
  75.  
  76. //Passes in strings of a length relevant to the difficulty selected.
  77. //Also sets score based on difficulty.
  78. switch (dif[0])
  79. {
  80. case '1': {spot=setBoard(begnner, choices);
  81. numOf=4;
  82. score=100;
  83. break;}
  84. case '2': {spot=setBoard(easy, choices);
  85. numOf=6;
  86. score=300;
  87. break;}
  88. case '3': {spot=setBoard(interm, choices);
  89. numOf=7;
  90. score=375;
  91. break;}
  92. case '4': {spot=setBoard(hard, choices);
  93. numOf=8;
  94. score=480;
  95. break;}
  96. case '5': {printScore(0,"\0");
  97. goto RESTART;
  98. break;}
  99. case '-': {
  100. ofstream outfile("names.txt");
  101. outfile.close();
  102.  
  103. ofstream outfile1("scores.txt");
  104. outfile1.close();
  105.  
  106. cout<<"The scoreboard is now CLEARED"<<endl;
  107. cout<<"Enter anything to continue: ";
  108. getline(cin,cont);
  109. goto RESTART;
  110. break;}
  111. case '9': {printInstructions(); //calls function to print instructions
  112. goto RESTART; //goes to beginning of menu loop
  113. break;}
  114. default: {
  115. win=true; //sets win to true to break out of loop
  116. again="n"; //sets again to n to break out of loop
  117. }
  118. }
  119.  
  120.  
  121. do{
  122. //this if will skip the whole block that checks the user's input for the
  123. //right letters if user decides to quit.
  124.  
  125. if (again!="n"){
  126. guess=checkInput(numOf, quit, spot);
  127. if(quit==true)
  128. {
  129. break;
  130. }
  131.  
  132. //for loop checks each spot to see how it corresponds with the player's
  133. //guess, and then will assign O or X to a variable that will output
  134. //during the game so that the player can know how accurate his guess was
  135. for (int j=0; j<4; j++)
  136. {
  137. (guess[j]==spot[j]) ? (chkSpot[j]='O') : (chkSpot[j]='X');
  138.  
  139. if(guess[j]==spot[j])
  140. {
  141. accura+="O";
  142. oOCount++; //increases value per every O assigned
  143. }
  144. else if ((spot.find(guess[j])!= string::npos)&&guess[j]!=spot[j])
  145. {
  146. accura+="o";
  147. oCount++; //increases value per every o assigned
  148. }
  149. else if (spot.find(guess[j])==string::npos)
  150. {
  151. accura+=" "; //puts in a blank when neither an O or o isn't assigned
  152. }
  153. }
  154.  
  155. //puts an amount of os corresponding to the oCount in the front of string
  156. for(int i=0; i<oCount; i++)
  157. {
  158. accuraO+="o";
  159. }
  160.  
  161. //after the os are put in, this does the same thing as above, only with
  162. //Os. The Os will always be after the os
  163. for(int i=0; i<oOCount; i++)
  164. {
  165. accuraO+="O";
  166. }
  167.  
  168. //if statement checks if user guessed the letters assigned to each
  169. //spot on the board correctly. If the player is successful, tell em.
  170. //if not, tell em.
  171. if (chkSpot[0]=='O'&&chkSpot[1]=='O'&&chkSpot[2]=='O'&&chkSpot[3]=='O')
  172. {
  173. cout<<"YOU WIN"<<endl;
  174.  
  175. cout<<"|| [OOOO] || ";
  176. cout<<"== [OOOO]"<<endl;
  177. cout<<endl;
  178.  
  179. //Outputs final score
  180. cout<<"Your final score is "<<setprecision(4)<<score<<"!"<<endl;
  181.  
  182. //Prompts user to type in the name that will be associated with
  183. //the score.
  184. cout<<"Enter your name, you mastermind: ";
  185.  
  186. getline(cin,name);
  187. while(name.size()>14)
  188. {
  189. cout<<"Input up to 14 LETTERS only"<<endl;
  190. cout<<"Enter your name: ";
  191. getline(cin,name);
  192.  
  193. }
  194. cout<<endl;
  195.  
  196. //Calls saveScore function to save score.
  197. saveScore(score, name);
  198.  
  199.  
  200. //Sets win = true so that game loop will break
  201. win=true;
  202. cout<<"Would you like to play again on a new board? Enter 'y' for yes, ";
  203. cout<<"or anything else to quit: ";
  204.  
  205. getline(cin,again);
  206.  
  207. }
  208. else
  209. {
  210. //Outputs the board to show what the user got right and the
  211. //list of their choices
  212. cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
  213. if(numOf!=4){
  214. cout<<"== ["<<accuraO<<"]";
  215. }
  216. else
  217. {
  218.  
  219. cout<<"== ["<<chkSpot[0]<<chkSpot[1]<<chkSpot[2]<<chkSpot[3]<<"]";
  220. }
  221. cout<<" -- CHOICES: "<<choices<<" -- Enter '1' to return to the menu."<<endl;
  222. }
  223. //resets score, oCount, oOcount, accura & accuraO
  224. score-=score*0.07;
  225. oCount=0;
  226. oOCount=0;
  227. accuraO = (4, "");
  228. accura = (4, "");
  229. }
  230. }while(win==false); //loop will keep players guessing until they win.
  231. }while(again=="y"||again=="Y"); //loop will keep players in menu until they-
  232. //decide to quit
  233. return 0;
  234. }//end of main
  235.  
  236. string setBoard(string user, string &choices){
  237. string spot(user.size(),'.'), //where letters will be randomly assigned
  238. chkSpot; //checks how accurate the player's guess is
  239. int boardSet, //where random number assignment (1-4) will be stored
  240. repeat[user.size()]={}; //tests for number repetition
  241. bool exists=false;
  242. srand(time(NULL));
  243.  
  244. //Will assign list of valid choices depending on what difficulty the user
  245. //chooses
  246. switch(user.size())
  247. {
  248. case 4: {choices="Q W A S"; break;}
  249. case 6: {choices="Q W A S E D"; break;}
  250. case 7: {choices="Q W A S E D R"; break;}
  251. case 8: {choices="Q W A S E D R F"; break;}
  252. }
  253.  
  254. //for loop for randomizing the board
  255. for (int i=0; i<4; i++)
  256. {
  257. //sets exist to true at the start of each loop.
  258. exists=true;
  259.  
  260. //gets a random number in-between 1 and the number denoting
  261. //the length of the string passed in by the user selected difficulty
  262. boardSet=(rand() % user.size()) + 1;
  263.  
  264. //Tests a user character to see if it's already been used
  265. //(AKA, if it's already been stored in repeat[])
  266. for(int j=0; j<4; j++)
  267. {
  268. if(boardSet==repeat[j])
  269. {
  270. //if there is a repeat variable, set exists to false
  271. exists=false;
  272. }
  273. }
  274.  
  275. //first if test will test if the random number is identical to any previous random number.
  276. //if the random number IS equal to any previous ones, it will decrement 'i'
  277. if (exists==false)
  278. {
  279. //(bgrList.find(guess[i])== string::npos) (PREVIOUS VERSION ^)
  280.  
  281. //decrements i so that the for loop will run an extra loop so that it can
  282. //assign a number that hasn't already been taken
  283. i--;
  284. }
  285. //if the random number is unique, it assigns it to a spot on the board
  286. else{
  287. //each number 1-4 corresponds to a letter that will be assigned to a spot
  288. //on the board
  289. switch (boardSet)
  290. {
  291. case 1: {
  292. spot[i]='Q'; //Assigns Q to a spot
  293. break;
  294. }
  295. case 2: {
  296. spot[i]='W'; //Assigns W to a spot
  297. break;
  298. }
  299. case 3: {
  300. spot[i]='A'; //Assigns A to a spot
  301. break;
  302. }
  303. case 4: {
  304. spot[i]='S'; //Assigns S to a spot
  305. break;
  306. }
  307. case 5: {
  308. spot[i]='E'; //Assigns E to a spot
  309. break;
  310. }
  311. case 6: {
  312. spot[i]='D'; //Assigns D to a spot
  313. break;
  314. }
  315. case 7: {
  316. spot[i]='R'; //Assigns R to a spot
  317. break;
  318. }
  319. case 8: {
  320. spot[i]='F'; //Assigns F to a spot
  321. break;
  322. }
  323. }//ends switch
  324. chkSpot[i]=spot[i]; //Assigns letter to check for if the user guessed correctly.
  325. repeat[i]=boardSet; //assigns number in repeat variable so it can test for future repeats
  326. }//ends else statement
  327.  
  328. }//ends for loop
  329. //outputs the board
  330. cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
  331. cout<<"== [????]";
  332. cout<<" -- CHOICES: "<<choices<<" -- Enter '1' to return to the menu."<<endl;
  333.  
  334. return spot; //returns the string which holds the positions of the letters
  335. //on the board.
  336. }
  337.  
  338. string checkInput(int num, bool &quit, string &spot)
  339. {
  340. bool check; //variable to check if input is valid
  341. string guess, //user's guess
  342. vldList, //where valid list of characters will be stored.
  343. repeat=(4, "");
  344. //assigns the string of valid characters to a variable that will be searched
  345. //when it comes to checking if the user input is valid
  346. switch(num)
  347. {
  348. case 4: {vldList="QWASqwas1+"; break;}
  349. case 6: {vldList="QWASEDqwased1+"; break;}
  350. case 7: {vldList="QWASEDRqwasedr1+"; break;}
  351. case 8: {vldList="QWASEDRFqwasedrf1+"; break;}
  352. }
  353.  
  354. do{
  355. //if nothing is invalid about the user input, this bool allows
  356. //the program to pass through the loop
  357. check=true;
  358.  
  359. //Prompts user to enter guess
  360. cout<<"Enter: ";
  361. getline(cin,guess);
  362.  
  363. if(guess=="1")
  364. {
  365. quit=true;
  366. }
  367. else if(guess=="+")
  368. {
  369. cout<<"THE ANSWER IS: "<<spot<<endl;
  370. }
  371.  
  372. //Checks for valid input
  373. while((guess.size()!=4)&&(guess!="1"))
  374. {
  375. cout<<"Invalid input. Please type in the four valid letters only. NO REPEATS"<<endl;
  376. getline(cin,guess);
  377. }
  378.  
  379. //converts lowercase inputs to uppercase
  380. for(int u=0; u<4; u++){
  381. switch(guess[u])
  382. {
  383. case 'q': {guess[u]='Q'; break;}
  384. case 'w': {guess[u]='W'; break;}
  385. case 'a': {guess[u]='A'; break;}
  386. case 's': {guess[u]='S'; break;}
  387. case 'e': {guess[u]='E'; break;}
  388. case 'd': {guess[u]='D'; break;}
  389. case 'r': {guess[u]='R'; break;}
  390. case 'f': {guess[u]='F'; break;}
  391. }
  392. }
  393.  
  394. //Goes through the user inputted characters one by one, searches
  395. //to see if any character doesn't match up with the valid list of
  396. //characters.
  397. for(unsigned int i=0; i<guess.size(); i++)
  398. {
  399. if (vldList.find(guess[i])== string::npos)
  400. {
  401. check=false;
  402. }
  403. }
  404.  
  405. //check = false if user types in 2 of the same chars
  406. for (unsigned i=0; i<guess.size()-1; i++)
  407. { if (guess.find(guess[i], i+1) != string::npos)
  408. check=false;
  409. }
  410.  
  411. //if any erroneous character was found in previous for loop, this while
  412. //will run giving the player an invalid input message
  413. if(check==false)
  414. {
  415. cout<<"Invalid input. Please type in four valid letters only. NO REPEATS"<<endl;
  416. }
  417.  
  418. }while(check==false); //will loop if the input was invalid
  419. return guess; //returns the user's BACKGROUND CHECKED guess
  420. }
  421.  
  422. void printInstructions(){
  423. string cont, //pauses after instructions print until user inputs anything
  424. contents; //where file data will be stored
  425. ifstream textFile;
  426.  
  427. //opens file
  428. textFile.open("instructions.txt");
  429. if (textFile) { //If file opened successfully, continue with what you wanna do
  430. while (textFile >> contents) { //While there is MORE input to read from the file, do whatever
  431. cout << contents <<" "; //display data from file
  432. }
  433. } else cout << "Error opening file."; //If it doesn't open, display error
  434.  
  435. //close and clear file
  436. textFile.close();
  437. textFile.clear();
  438. cout<<endl;
  439. cout<<endl;
  440. //waits for user input to continue back to menu
  441. cout<<"Enter anything to continue: ";
  442. getline(cin,cont);
  443.  
  444. return;
  445. }
  446.  
  447. void saveScore(float score, string name){
  448. ofstream file;
  449.  
  450. file.open("scores.txt", fstream::app);
  451. file<<score<<" \n";
  452. file.close();
  453.  
  454. file.open("names.txt", fstream::app);
  455. file<<name<<" \n";
  456. file.close();
  457.  
  458. return;
  459. }
  460.  
  461. void printScore(float score, string name){
  462. string hiScore[ROW][COL];
  463. string cont;
  464. int num=0;
  465. ifstream scores,
  466. names;
  467. ofstream outfile, outfile1;
  468.  
  469.  
  470. //Opens files
  471. scores.open("scores.txt");
  472. names.open("names.txt");
  473.  
  474. cout<<"TEST PRINT SCORES before storing file values into array"<<endl;
  475. for(int i=num-1; i>=0; i--)
  476. {
  477. cout<<setprecision(4)<<atoi(hiScore[i][0].c_str())<<endl;
  478. }
  479.  
  480. cout<<endl;
  481.  
  482.  
  483. //stores values from file into array
  484. for(int i=0; i<ROW; i++)
  485. {
  486. scores>>hiScore[i][0];
  487. names>>hiScore[i][1];
  488.  
  489. if(static_cast<int>(hiScore[i][1][0])>65&&static_cast<int>(hiScore[i][1][0])<122)
  490. {
  491. num++;
  492. }
  493. }
  494. cout<<"TEST PRINT SCORES after storing file values into array"<<endl;
  495. for(int i=num-1; i>=0; i--)
  496. {
  497. cout<<setprecision(4)<<atoi(hiScore[i][0].c_str())<<endl;
  498. }
  499.  
  500. cout<<endl;
  501. if(score!=0){
  502. if(score>atoi(hiScore[9][0].c_str())/**&&static_cast<int>(hiScore[9][0][0])>48&&static_cast<int>(hiScore[9][0][0])<58**/)
  503. {
  504. ostringstream ss;
  505. ss<<score;
  506. hiScore[9][0]=ss.str();
  507. hiScore[9][1]=name;
  508. }
  509. }
  510. cout<<"TEST PRINT SCORES after swap"<<endl;
  511. for(int i=num-1; i>=0; i--)
  512. {
  513. cout<<setprecision(4)<<atoi(hiScore[i][0].c_str())<<endl;
  514. }
  515.  
  516. cout<<endl;
  517.  
  518. //Calls function to sort scores
  519. sortScores(hiScore, num);
  520.  
  521. //Closes files
  522. scores.close();
  523. names.close();
  524.  
  525.  
  526. if(score!=0){
  527. //Clears files
  528. ofstream file("names.txt");
  529. file.close();
  530. ofstream file1("scores.txt");
  531. file1.close();
  532. cout<<endl;
  533. //cout<<"CHECK TO SEE IF FILES WERE CLEARED: ";
  534. //getline(cin,cont);
  535. }
  536.  
  537. if(score==0){
  538. //Prints scoreboard
  539. cout<<" SCOREBOARD"<<endl;
  540. for(int i=num-1; i>0; i--)
  541. {
  542. cout<<setw(15)<<hiScore[i][1]<<" ";
  543. cout<<setprecision(4)<<atoi(hiScore[i][0].c_str())<<endl;
  544. }
  545.  
  546. cout<<endl;
  547. }
  548. cout<<endl;
  549.  
  550. outfile.open("scores.txt");
  551. outfile1.open("names.txt");
  552.  
  553. //Puts new sorted array into file.
  554. for(int i=num-1; i>0; i--){
  555.  
  556. outfile<<hiScore[i][0]<<" \n";
  557. outfile1<<hiScore[i][1]<<" \n";
  558. }
  559.  
  560. outfile1.close();
  561. outfile.close();
  562.  
  563. cout<<endl;
  564.  
  565. cout<<"Enter anything to continue: ";
  566. getline(cin,cont);
  567. cout<<endl;
  568. cout<<endl;
  569.  
  570. return;
  571. }
  572.  
  573. void sortScores(string scores[][COL], int num){
  574.  
  575.  
  576. for(int i=0; i<(num-1); i++)
  577. {
  578. for(int j=i+1; j<num; j++)
  579. {
  580. string temp, temp1;
  581.  
  582. if(atoi(scores[i][0].c_str())>atoi(scores[j][0].c_str()))
  583. {
  584. temp=scores[i][0];
  585. scores[i][0]=scores[j][0];
  586. scores[j][0]=temp;
  587.  
  588. temp1=scores[i][1];
  589. scores[i][1]=scores[j][1];
  590. scores[j][1]=temp1;
  591. }
  592. }
  593. }
  594.  
  595. cout<<"TEST PRINT SCORES after sort (within function)"<<endl;
  596. for(int i=num-1; i>=0; i--)
  597. {
  598. cout<<setprecision(4)<<atoi(scores[i][0].c_str())<<endl;
  599. }
  600.  
  601. cout<<endl;
  602.  
  603. return;
  604. }
Advertisement
Add Comment
Please, Sign In to add comment