Help with Hangman( different version)_

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 101
Reputation: Se7Olutionyg has a little shameless behaviour in the past 
Solved Threads: 0
Se7Olutionyg Se7Olutionyg is offline Offline
Junior Poster

Help with Hangman( different version)_

 
0
  #1
Nov 24th, 2008
  1. // Description: This is a computer version of the game hangman, the user
  2. // can play against a 2nd player or the computer, and he can enter how many
  3. // guesses he gets,
  4. //
  5.  
  6. // Start of program
  7.  
  8. // Header file definitions
  9.  
  10. #include <iostream> // Standard input/output
  11. #include <string> // String manipulation
  12. #include <cctype> // Character manipulation and testing
  13. #include <fstream> // File stream
  14. #include <cstdlib> // Used for random function
  15.  
  16. #include "DRAW.h" // Draws hangman
  17.  
  18. using namespace std;
  19.  
  20. // Function declerations
  21. void DrawGallows(int State);
  22. void instruction(int& choice); // Gives instructions and gets choice
  23. void usergame(int i); // Plays 2nd user game
  24. void compgame(int i); // Plays against computer
  25. void test(string word, char letter, int& numwrong, string& temp, int i); // Tests current letter and replaces starred word
  26. void lchosen(char letter, string& letterchosen, int& check, int& chosencounter, int i); // Checks current letter and adds it to letters chosen output if not entered already
  27. void rnd(string& word, int i); // Gets random word from file
  28.  
  29. inline istream& Flush(istream& stream); // Flushes cin stream
  30.  
  31. // Start of main
  32.  
  33. int main()
  34. {
  35. int i = 0; // Counter variable for loops
  36. int exit = 0; // Main loop exit variable
  37. int choice; // Users inputed choice for type of game or to exit
  38.  
  39. // Main control loop
  40.  
  41. do { // while exit != 1
  42. system("cls"); // Clear the screen
  43. instruction(choice); // Give instructions
  44.  
  45.  
  46. switch(choice)
  47. {
  48. case 1:
  49. usergame(i); // Calls user game
  50. break;
  51. case 2:
  52. compgame(i); // Calls computer game
  53. break;
  54. case 3:
  55. cout << "Goodbye" << endl; // Exits
  56. exit = 1;
  57. break;
  58. default:
  59. cerr << "Invalid choice - try again" << endl; // Invalid choice erroe
  60. }
  61. } while (exit != 1);
  62.  
  63. // End main loop
  64.  
  65. system("pause");
  66. return 0;
  67. }
  68. // End main
  69.  
  70. // Subprograms
  71. // ---------------------------------------------------------------------
  72.  
  73. // Instructions subprogram
  74.  
  75. void instruction(int& choice)
  76. {
  77. cout << " -Hangman-" << endl << endl;
  78. cout << " Created by Kyle Burmark" << endl << endl;
  79. cout << "*****************************************" << endl;
  80. cout << endl;
  81. cout << " Enter -1- to play against user" << endl;
  82. cout << " Enter -2- to play against computer" << endl;
  83. cout << " Enter -3- to quit" << endl;
  84. cout << endl;
  85. cout << "*****************************************" << endl << endl;
  86.  
  87. cout << "Choice: "; // Get user choice
  88. cin >> choice;
  89.  
  90. // Bad data type check
  91.  
  92. while (!cin)
  93. {
  94. cerr << "Invalid character" << endl
  95. << "Enter again - choice: ";
  96. Flush(cin);
  97. cin >> choice;
  98. }
  99.  
  100. // End check
  101.  
  102. system("cls");
  103. }
  104.  
  105. // End instruction
  106.  
  107. // User game subprogram
  108.  
  109. void usergame(int i)
  110. {
  111. int numguess = 0; // Number of guesses player gets
  112. int numwrong = 0; // Number of wrong guesses player has so far
  113. int check; // A variable to test whether to check the letter entered in the test function
  114. int wordcheck; // A variable to check whether the user has entered an invalid word
  115. int end = 0; // A variable to test what to output to the user and to exit the usergame loop
  116. int chosencounter = 0; // A counter to tell the replace function where to put the letter chosen in the letterchosen string
  117. char letter; // User inputed letter
  118. string word; // Word user is trying to guess
  119. string temp; // Updated output word user sees
  120. string letterchosen = " "; // Defines length of letterchosen string
  121.  
  122. do { // while user puts in guesses outside of allowed range
  123. cout << "How many chances does the person have (4 - 10): ";
  124. cin >> numguess;
  125. } while (numguess < 4 || numguess > 10);
  126.  
  127. cout << "Enter word 2nd user: ";
  128. cin >> word;
  129.  
  130.  
  131. do { // while user inputs bad word
  132. wordcheck = 0;
  133. for (int i = 0; i < word.length(); i++)
  134. {
  135. if (!isalpha(word.at(i)))
  136. {
  137. wordcheck = 1;
  138. }
  139. }
  140. if (wordcheck == 1)
  141. {
  142. cout << "Invalid - Enter word again: ";
  143. cin >> word;
  144. }
  145. } while (wordcheck == 1);
  146.  
  147. temp = word; // Sets temp string length to word string length
  148.  
  149. // Replace temp with stars loop
  150.  
  151. for (i = 0; i < word.length(); i++)
  152. {
  153. temp.replace(i, 1, 1,'*');
  154. }
  155.  
  156. // End loop
  157.  
  158. system("cls");
  159.  
  160. // Main game loop
  161.  
  162. do {
  163.  
  164.  
  165. // Tests if user guessed word
  166.  
  167. if (word == temp)
  168. {
  169. cout << endl << endl;
  170. cout << "You guessed it [ " << word << " ]" << endl << endl;
  171. system("pause");
  172. end = 1;
  173. }
  174.  
  175. // Tests if user failed to guess word
  176.  
  177. if (numwrong == numguess)
  178. {
  179. cout << endl << endl;
  180. cout << "You failed" << endl << endl;
  181. system("pause");
  182. end = 2;
  183. }
  184.  
  185. // Play while above conditions aren't true
  186.  
  187. if (end == 0)
  188. {
  189. cout << endl << endl << endl;
  190. cout << "Letters chosen: " << letterchosen << endl;
  191. cout << endl << endl << endl;
  192. cout << "Guesses left: " << numguess - numwrong << endl << endl;
  193. cout << " " << temp << endl << endl;
  194. cout << "Letter: ";
  195. cin >> letter;
  196.  
  197. // Test for bad letter
  198.  
  199. while (!isalpha(letter))
  200. {
  201. Flush(cin);
  202. cout << "Not a letter - enter letter: ";
  203. cin >> letter;
  204. }
  205.  
  206. // End test
  207.  
  208. lchosen(letter, letterchosen, check, chosencounter, i);
  209.  
  210. // Test whether to check letter against correct word string
  211.  
  212. if (check == 0)
  213. {
  214. test(word, letter, numwrong, temp, i);
  215. }
  216. else
  217. {
  218. ;
  219. }
  220.  
  221. // End test
  222.  
  223. system("cls");
  224. }
  225.  
  226. // End game
  227.  
  228. system("cls");
  229. } while(end != 1 && end != 2);
  230.  
  231. // End main game loop
  232.  
  233. // Tests end variable to see what to display
  234.  
  235. if (end == 2)
  236. {
  237. cout << "Correct word was [ " << word << " ]" << endl << endl;
  238. system("pause");
  239. }
  240. if (end == 1)
  241. {
  242. cout << " ";
  243. }
  244.  
  245. system("cls");
  246.  
  247. }
  248.  
  249. // End user game
  250.  
  251. // Start computer game
  252. // See user game for variable and loop information
  253.  
  254. void compgame(int i)
  255. {
  256. int numguess = 0;
  257. int numwrong = 0;
  258. int check;
  259. int end = 0;
  260. int chosencounter = 0;
  261. char letter;
  262. string word;
  263. string temp;
  264. string letterchosen = " ";
  265.  
  266. do {
  267. cout << "How many chances do you want (4 - 10): ";
  268. cin >> numguess;
  269. } while (numguess < 4 || numguess > 10);
  270.  
  271. rnd(word, i); // Gets random word
  272. temp = word;
  273.  
  274. for (i = 0; i < word.length(); i++)
  275. {
  276. temp.replace(i, 1, 1, '*');
  277. }
  278.  
  279. system("cls");
  280.  
  281. do {
  282. int State = 1;
  283. void DrawGallows(int State);
  284. if (word == temp)
  285. {
  286. cout << endl << endl;
  287. cout << "You guessed it [ " << word << " ]" << endl << endl;
  288. system("pause");
  289. end = 1;
  290. }
  291. if (numwrong == numguess)
  292. {
  293. cout << endl << endl;
  294. cout << "You failed" << endl << endl;
  295. system("pause");
  296. end = 2;
  297. }
  298. if (end == 0)
  299. {
  300. cout << endl << endl << endl;
  301. cout << "Letters chosen: " << letterchosen << endl;
  302. cout << endl << endl << endl;
  303. cout << "Guesses left: " << numguess - numwrong << endl << endl;
  304. cout << " " << temp << endl << endl;
  305. cout << "Letter: ";
  306. cin >> letter;
  307.  
  308. while (!isalpha(letter))
  309. {
  310. Flush(cin);
  311. cout << "Not a letter - enter letter: ";
  312. cin >> letter;
  313. }
  314.  
  315. lchosen(letter, letterchosen, check, chosencounter, i);
  316.  
  317. if (check == 0)
  318. {
  319. test(word, letter, numwrong, temp, i);
  320. }
  321. else
  322. {
  323. ;
  324. }
  325.  
  326. system("cls");
  327. }
  328.  
  329. system("cls");
  330. } while(end != 1 && end != 2);
  331.  
  332. if (end == 2)
  333. cout << "Correct word was [ " << word << " ]" << endl << endl;
  334. system("pause");
  335. if (end == 1)
  336. cout << endl;
  337.  
  338. system("cls");
  339.  
  340.  
  341. }
  342.  
  343. // End computer game
  344.  
  345. // Checks current letter chosen by user
  346.  
  347. void lchosen(char letter, string& letterchosen, int& check, int& chosencounter, int i)
  348. {
  349. check = 0;
  350.  
  351. // Check if letter is equal to any letter in letterchosen string loop
  352.  
  353. for (i = 0; i < letterchosen.length(); i++)
  354. {
  355. if (letter == letterchosen.at(i))
  356. {
  357. check = 1;
  358. }
  359. }
  360.  
  361. // End loop
  362.  
  363. // Test if letter is already chosen
  364.  
  365. if (check == 1)
  366. {
  367. cout << endl;
  368. cout << "Letter already chosen" << endl;
  369. system("pause");
  370. }
  371.  
  372. // Puts letter in string if not chosen
  373.  
  374. else
  375. {
  376. letterchosen.replace(chosencounter, 1, 1, letter);
  377. chosencounter++;
  378. }
  379.  
  380. }
  381.  
  382. // End lchosen function
  383.  
  384. // Tests whether letter is in word string
  385.  
  386. void test(string word, char letter, int& numwrong, string& temp, int i)
  387. {
  388. int check2 = 0; // Checks whether letter is in word string, = 1 if it is
  389.  
  390. // Check for letter match loop
  391.  
  392. for (i = 0; i < word.length(); i++)
  393. {
  394. if (letter == word.at(i))
  395. {
  396. temp.replace(i, 1, 1, letter);
  397. check2 = 1;
  398. }
  399. }
  400.  
  401. // End loop
  402.  
  403. // Displays "wrong letter" if user inputed bad letter
  404.  
  405. if (check2 == 0)
  406. {
  407. cout << endl;
  408. cout << "Wrong letter" << endl;
  409. system("pause");
  410. numwrong++;
  411.  
  412. }
  413. }
  414.  
  415. // End test function
  416.  
  417. // Gets random word
  418.  
  419. void rnd(string& word, int i)
  420. {
  421.  
  422. int x; // Random number to pass to word file loop
  423. ifstream ins; // File stream
  424.  
  425. srand(time(NULL)); // Gets better random number based on time
  426. x = rand()%100; // Gets number 0 - 99
  427. ins.open("words.txt"); // Opens random word file
  428.  
  429. // Tests for bad file and gets word if not a bad file
  430.  
  431. if (ins.fail())
  432. {
  433. cerr << "Words.txt is not in same folder as hangman.exe, " << endl
  434. << "put in correct file and run again and make sure it's " << endl
  435. << "called words.txt" << endl;
  436. system("pause");
  437.  
  438. main();
  439. }
  440. else
  441. {
  442. for (i = 0; i < (x + 1); i++)
  443. {
  444. getline(ins, word);
  445. }
  446.  
  447. }
  448.  
  449. // End test
  450.  
  451. ins.close(); // Close file
  452. }
  453.  
  454. // End random function
  455.  
  456. // This will Draw the gallows according to the state
  457. void DrawGallows(int State)
  458. {
  459. if(State==6)
  460. {
  461. // The \\ will translate as '\' because it is a special char
  462. cout<<endl<<endl
  463. <<" +----+ "<<endl
  464. <<" | | "<<endl
  465. <<" | O "<<endl
  466. <<" | /|\\ "<<endl
  467. <<" | / \\ "<<endl
  468. <<" |Your Dead "<<endl
  469. <<" ============"<<endl<<endl;
  470. }
  471. else if(State==5)
  472. {
  473. cout<<endl<<endl
  474. <<" +----+ "<<endl
  475. <<" | | "<<endl
  476. <<" | O "<<endl
  477. <<" | /|\\ "<<endl
  478. <<" | \\ "<<endl
  479. <<" | "<<endl
  480. <<" ============"<<endl<<endl;
  481. }
  482. else if(State==4)
  483. {
  484. cout<<endl<<endl
  485. <<" +----+ "<<endl
  486. <<" | | "<<endl
  487. <<" | O "<<endl
  488. <<" | /|\\ "<<endl
  489. <<" | "<<endl
  490. <<" | "<<endl
  491. <<" ============="<<endl<<endl;
  492. }
  493. else if(State==3)
  494. {
  495. cout<<endl<<endl
  496. <<" +----+ "<<endl
  497. <<" | | "<<endl
  498. <<" | O "<<endl
  499. <<" | /| "<<endl
  500. <<" | "<<endl
  501. <<" | "<<endl
  502. <<" ============="<<endl<<endl;
  503. }
  504. else if(State==2)
  505. {
  506. cout<<endl<<endl
  507. <<" +----+ "<<endl
  508. <<" | | "<<endl
  509. <<" | O "<<endl
  510. <<" | | "<<endl
  511. <<" | "<<endl
  512. <<" | "<<endl
  513. <<" ============="<<endl<<endl;
  514. }
  515. else if(State==1)
  516. {
  517. cout<<endl<<endl
  518. <<" +----+ "<<endl
  519. <<" | | "<<endl
  520. <<" | "<<endl
  521. <<" | "<<endl
  522. <<" | "<<endl
  523. <<" | "<<endl
  524. <<" ============="<<endl<<endl;
  525. }
  526.  
  527. }
  528. inline istream& Flush(istream& stream)
  529. {
  530. stream.clear();
  531. int chars_to_skip = stream.rdbuf()->in_avail();
  532. return stream.ignore(chars_to_skip);
  533. }
  534.  
  535. // End flush function
  536.  
  537. // End subprogams
  538.  
  539. // End program

my problem is I was trying to top down design this program by solving each function seperately. However, I am consfued how to link the function draw to the main function. please help me. thank you
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help with Hangman( different version)_

 
0
  #2
Nov 24th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC