Hangman help help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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

Hangman help help

 
0
  #1
Nov 23rd, 2008
  1. #include <iostream>
  2. using namespace std;
  3. #include <string>
  4. #include <cctype>
  5. void DrawGallows(int );
  6. int main()
  7. {
  8. char solution[20]; //holds solution
  9. char blank[20]; //holds "*"'s for unsolved letters
  10. int counter = 0; //general-use counter
  11. int right = 0; //1 = right guess, 0 = wrong guess.
  12. char guess;
  13.  
  14. cout<<"Enter phrase 20 chars or less."<<endl;
  15. cin.getline(solution, 20);
  16. int puzzLength = strlen(solution); //finds lengtrh of puzzle, stores INT value to puzzlength
  17.  
  18.  
  19.  
  20.  
  21. //convert puzzle to full uppercase
  22. for (counter = 0; counter < puzzLength; counter++){
  23. solution[counter] = toupper(solution[counter]);
  24. }
  25. //done converting
  26.  
  27.  
  28. strcpy(blank, solution); //copy solution to the 'blank' array
  29.  
  30. for (counter = 0; counter < puzzLength; counter++) { //converts characters to *'s to represent blanks
  31.  
  32. if (isalnum(solution[counter])) blank[counter] = '*';
  33. else blank[counter] = solution[counter];
  34.  
  35. } //closes for loop
  36.  
  37. while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer
  38.  
  39.  
  40. cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl;
  41. cout<<"Enter a guess."<<endl;
  42. cin>>guess;
  43. guess = toupper(guess);
  44.  
  45.  
  46. //cbeck guess!
  47. int State =1;
  48. for (counter = 0; counter <= puzzLength; counter++) {
  49.  
  50. if (guess == solution[counter]) {
  51. blank[counter] = guess; //fill in the puzzle with the letter
  52.  
  53. }
  54. if (guess != solution[counter])
  55. {
  56. State++;
  57. DrawGallows(State);
  58. }
  59. } //close loop, done checking guess
  60.  
  61. } //game is over.
  62.  
  63. cout<<"Winner!";
  64. cin.get();
  65. return 0;
  66. }
  67. void DrawGallows(int State)
  68. {
  69. if(State==6)
  70. {
  71. // The \\ will translate as '\' because it is a special char
  72. cout<<endl<<endl
  73. <<" +----+ "<<endl
  74. <<" | | "<<endl
  75. <<" | O "<<endl
  76. <<" | /|\\ "<<endl
  77. <<" | / \\ "<<endl
  78. <<" |Your Dead "<<endl
  79. <<" ============"<<endl<<endl;
  80. }
  81. else if(State==5)
  82. {
  83. cout<<endl<<endl
  84. <<" +----+ "<<endl
  85. <<" | | "<<endl
  86. <<" | O "<<endl
  87. <<" | /|\\ "<<endl
  88. <<" | \\ "<<endl
  89. <<" | "<<endl
  90. <<" ============"<<endl<<endl;
  91. }
  92. else if(State==4)
  93. {
  94. cout<<endl<<endl
  95. <<" +----+ "<<endl
  96. <<" | | "<<endl
  97. <<" | O "<<endl
  98. <<" | /|\\ "<<endl
  99. <<" | "<<endl
  100. <<" | "<<endl
  101. <<" ============="<<endl<<endl;
  102. }
  103. else if(State==3)
  104. {
  105. cout<<endl<<endl
  106. <<" +----+ "<<endl
  107. <<" | | "<<endl
  108. <<" | O "<<endl
  109. <<" | /| "<<endl
  110. <<" | "<<endl
  111. <<" | "<<endl
  112. <<" ============="<<endl<<endl;
  113. }
  114. else if(State==2)
  115. {
  116. cout<<endl<<endl
  117. <<" +----+ "<<endl
  118. <<" | | "<<endl
  119. <<" | O "<<endl
  120. <<" | | "<<endl
  121. <<" | "<<endl
  122. <<" | "<<endl
  123. <<" ============="<<endl<<endl;
  124. }
  125. else if(State==1)
  126. {
  127. cout<<endl<<endl
  128. <<" +----+ "<<endl
  129. <<" | | "<<endl
  130. <<" | "<<endl
  131. <<" | "<<endl
  132. <<" | "<<endl
  133. <<" | "<<endl
  134. <<" ============="<<endl<<endl;
  135. }
  136. }
THIS IS MY CODE.. AND I HAVE PROBLEMS WITH THAT :
1) WHEN I INPUT THE WRONG CHARACTER, IT RUNS UNTIL STATE =5 ,NOT AS IT SHOULD RUNS AT STATE= 1+1 = 2... HOW CAN i FIX THAT
THIS IS MY SAMPLE RUN :
Enter phrase 20 chars or less.
DSADS
Enter a guess.
D


+----+
| |
| O
| |
|
|
=============



+----+
| |
| O
| /|
|
|
=============



+----+
| |
| O
| /|\
|
|
=============



+----+
| |
| O
| /|\
| \
|
============

Enter a guess.
2) I JUST WANNA TO CONFIRM, IF I WANNA TO LINK THIS FILE TO MY MAIN FILE .CPP . I SHOULD NAME THIS FILE .H AND DEFINE IT IN THE MAIN FILE, AND USE THE FUNCTION?? THE FUNCTIONS DRAWGALLOW : DO I HAVE TO DECLARE AND SET UP THINGS LIKE THAT IN THE MAIN FILE AND IN THE .H FILE SEPERATELY. OR I CAN COMBINE THEM. BUT I JUST CAN YOU THE FUNCTION FROM THE .H FILE TO THE MAIN FILE. NOT FROM THE MAIN FILE TO THE .H FILE RITE?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Hangman help help

 
0
  #2
Nov 23rd, 2008
This
  1. while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer
is not going to work. You cannot directly compare the strings this way. Look up use of strcmp( ) function.

Your guess checking block seems to be flawed
  1. //check guess!
  2. int State =1;
  3. for (counter = 0; counter <= puzzLength; counter++)
  4. {
  5. if (guess == solution[counter])
  6. {
  7. blank[counter] = guess; //fill in the puzzle with the letter
  8. }
  9.  
  10. if (guess != solution[counter])
  11. {
  12. State++;
  13. DrawGallows(State);
  14. }
  15. } //close loop, done checking guess

You are adding to the degree of hanging for every guess that does not match every position of the word. If the guess matches any of the blank spaces in the solution, player should not be further harmed.

Try using a boolean value that gets set inside the loop to a good state if the user got a character correct. Initialize it before the loop to indicate bad guess. If you enter that if block, set the value to indicate a successful guess. After the checking loop, examine that value to determine if the state variable (degree of hanging) needs to be updated.

Also, the state variable should be set to 1 before entering the main guessing loop, not inside it.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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