Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-01-2011
Member
 
Join Date: Dec 2010
Posts: 5
Traditional hangman game not working in C

I have wrote a program a little game of hangman to learn a little history to handle the strings. I think it's a fairly trivial exercise, and I wondered if people here had to give me advice for program improvement and syntax, because I did it with my little knowledge. I am sending the code that I used for my source program :
Code:
Stdio.h 
/ * 
Compares two strings and returns 1 if they are identical, otherwise 0 
Takes 2 strings as input 
Compare the strings, letters by Letters 
* / 
Compare HI int (char str1 [], char str2 []) 
{Int i = 0, test = 1; 
while (str1 [i] & & str2 [i] & & test) 
if (str1 [i + +]! = ch2 [i + +]) test = 0; 
return test;} 
/ * 
Returns 1 if the letter is contained in the word, otherwise returns 0 
Takes as input a string and a letter 
Compare the word letter by letter with the letter 
* / 
LetterTest int (char word [], char letter []) 
{Int i = 0, test = 0; 
while (word [i]) 
if (word [i + +] == letter [0]) test = 1; 
return test;} 
/ * 
Displaying the incomplete word, returns 1 if the word is complete, otherwise 0 
It sends word to find, and the string containing the characters 
already found. 
Compare the word letter by letter with the string containing the letters 
found, displays the character if it is in this chain, displays 
and put an underscore variable WIN 0 otherwise 
* / 
int display (char word [], char find []) 
{Int i =- 1, win = 1; 
while (word [i + +]) 
{If (LetterTest (find, & word [i]) == 1) printf ("% c", word [i]); 
else {printf ("_") win = 0;}} 
return win; 
} 
/ * 
Main loop: 
Request a word which must then guess 
As the chain entry! = Not quit and Display 
not return 1: 
Request a character or string 'quit' 
/ "\ No protection to limit other 
channels possible bugs has predict / "\ 
If the letter belongs to the word, add in the chain 
letters found 
Increment the number of trials 
Show Score when the main loop is finished 
* / 
main () 
{ 
char word [30], letter [2] = "", found [30] = ""; 
int trials = 0, n = 0; 
printf ("\ n \ nEnter a word found \ n \ n") scanf ("% s", word); printf ("\ n \ n"); 
while ((Compare HI (letter, "quit")! = 1) & & (Display (word find)! = 1)) 
{ 
printf ("\ n \ n \ nEnter a letter or 'quit' to quit \ n"); 
scanf ("% s", letter); printf ("\ n \ n"); 
if (LetterTest (word, letter)) found [n + +] = letter [0]; 
trials + +; 
} 
printf ("\ n \ n% d tests \ n", tests); 
return 0; 
}
Reply With Quote
  #2  
Old 03-01-2011
Member
 
Join Date: Feb 2010
Posts: 137
Re: Traditional hangman game not working in C

It seems pretty good. I have just one or two comments:
  • LetterTest in the public, your second argument should be a character (I would find it more readable, but it also works well as you did)
  • LetterTest still function, you could stop you to browse the channel as soon as you know your test is false. For example:
Code:
LetterTest int (char word [], char letter) 
{ 
int i = 0; 
while (word [i]) 
if (word [i + +] == Letter) return 1; 
return 0; 
}
  • the prototype of your main function should be "int main ()" or "int main (int, char **)" (rather the first solution in your case)
  • in the main function: why your channels (particularly 'find') are initialized with a space?
  • your chain 'letter' is not large enough to contain the word 'quit' -> segfault
  • like your string 'find' is sent first argument LetterTest, it is imperative that you assure it is properly terminated by a 0. As thou hast statically allocated, I think in most cases your table is initialized with 0 and it works, but I'm not sure that is specified in the standard (can anyone confirm / reverse?) So, I'd rather see a main function in the genre:
Code:
int main () 
{ 
char word [30], letter [5 ]="", Found [30 ]=""; 
int trials = 0, n = 0; 
printf ("\ n \ nEnter a word found \ n \ n") scanf ("% s", word); printf ("\ n \ n"); 
while (1) 
{ 
printf ("\ n \ n \ nEnter a letter or 'quit' to quit \ n"); 
scanf ("% s", letter); printf ("\ n \ n"); 
if (Compare HI (letter, "quit") == 1) / * the user wants to exit * / 
break; / * is exited while loop * / 
if (LetterTest (word, letter [0])) 
{ 
Found [n + +] = letter [0]; 
Found [n] = 0; / * set end of string with a 0 * / 
} 
trials + +; 
if (display (word find) == 1) / * won! * / 
break; 
} 
printf ("\ n \ n% d tests \ n", tests); 
return 0; 
}
  • In general, your data entry with scanf are quite fragile, and you can not ensure that data entered by the user will not exceed. You should run a little cleaner this type of problems.
Reply With Quote
  #3  
Old 03-01-2011
Member
 
Join Date: Mar 2008
Posts: 192
Re: Traditional hangman game not working in C

Hello, a few remarks about the code:
Code:
Stdio.h 
/ * 
Compares two strings and returns 1 if they are identical, otherwise 0 
Takes 2 strings as input 
Compare the strings, letters by Letters 
* / 
Compare HI int (char str1 [], char str2 []) 
{Int i = 0, test = 1; 
while (str1 [i] & & str2 [i] & & test) 
if (str1 [i + +]! = ch2 [i + +]) test = 0; 
return test;}
you wrote ch1 [i + +]! = ch2 [i + +], which amounts to
ch1 [0]! = ch [1] and
c. [2]! = ch [3 ],...
which does not really suit, maybe you wanted to write ch1 [i]! = ch2 [i + +] instead? (Must still be on the compile evaluates the expression from left front).
Code:
/ * 
Returns 1 if the letter is contained in the word, otherwise returns 0 
Takes as input a string and a letter 
Compare the word letter by letter with the letter 
* / 
LetterTest int (char word [], char letter []) 
{Int i = 0, test = 0; 
while (word [i]) 
if (word [i + +] == letter [0]) test = 1; 
return test;}
Is this while the word [i] ends? The minimum (for me) would be a while (word [i]! = EOF), but the strings and I did 2 AC, especially c.
Reply With Quote
  #4  
Old 04-01-2011
Member
 
Join Date: Dec 2010
Posts: 5
Re: Traditional hangman game not working in C

For the first remark, I take it that the return command terminates a function to return to the one that called? Otherwise I have not really figured out how to describe the roles ... in court, one writes main () in the book that I use is not called evil functions by int () function like you, but what does it shows? If I initialize my array with a space, because I understand better now that it was doing rather than leaving them empty, especially if for example "find" or "letter" that variable argument is used before being defined.

while ((Compare HI (letter, "quit")! = 1) & & (Display (word find)! = 1)) in my function. I also found its weird if I define a string to a character (a letter, so ...) I can get a longer word without error ... I can do a scanf ("% c", letter) but then I can no longer use the word 'quit' (that I will eventually put a figure). I have my own idea of algorithm to display the ASCII hanged, but I'll take care of her when I get time.
Reply With Quote
  #5  
Old 04-01-2011
Member
 
Join Date: Mar 2008
Posts: 227
Re: Traditional hangman game not working in C

Quote:
if I initialize my array with a space, because I understand better now that it was doing rather than leaving them empty, especially if for example "find" or "letter" that variable argument is used before being defined
Caution, do not confuse "empty string" with "string uninitialized". Keep this in mind. You can totally initialize a string to "" (which means that you put a zero in the first cell of the table) it should not pose any problems.

Quote:
I also found its weird if I define a string to a character (a letter, so ...) I can get a longer word without error ...
The problem with segfaults is that they do not fire all the time. In fact, your system does not generate a segmentation fault when you try to access a piece of memory that is not yours. Basically, there may well be that you overtake the terminals of your paintings and corrupting memory chips, as you stay "with you", you do not segfault and you do not realize that you are trying to corrupt data / pieces of code in your program.
Reply With Quote
  #6  
Old 04-01-2011
Member
 
Join Date: Apr 2008
Posts: 193
Re: Traditional hangman game not working in C

I have wrote the code for the same game. I am posting, so that you can get some help from that. The game begins with a player enters a word to guess. The second player will place a letter or a complete word and the system will indicate whether the letter is present and if so, whether the word is correct. To simplify the input, the string of 0 characters (only accessible by pressing enter) will show in output to only a summary of the letters and words which have been made. Strings with a number of characters equal to the number of characters in the string choice, will be interpreted as an attempt to give the complete solution. The strings of 1 or more characters that do not fit in the case described above, will be truncated to first character and will continue to the usual search for the letter in the word initial. The player has a total of 7 attempts:

Code:
// SO = 0 -> UNIX // SO = 1 -> # ifndef WINDOWS # define SO 1 # endif # include # include <iostream> 
<string> # if OS == 0 # endif # include <unistd.h> <cstdlib> 
# include # include using namespace std <vector> / *!  
\ Brief Creates a string with dashes \ param n number of dashes \ n dashes return string * / 
string fill (int n) 
{string retv; for (int i = 0; i <n i + +) retv.push_back (' _ '); retv return;} / *!  
\ Brief Print the string, with each letter separated by spaces \ param string of letters letters * / 

void printLetters (string letters) 
{for (int i = 0; i <letters.size (); i + +) 
{court <<letters. at (i) if (i <letters.size () - 1) court <<"";} court <<endl;} 
/ *!  \ Brief Check if the letter is contained in the string to guess \ param word word to guess \ param letters string of letters / dashes \ param letter letter guessed * / 
bool guessLetter (const string & word, string & Letters, char letter) 
{bool ok = false; for (int i = 0; i <word.size (); i + +) if (word.at (i) == letter) {ok = true; letters.at (i) = letter;} return ok;} 
/ *!  \ Brief Check if the word choice is the right \ param init_word word to guess \ param word word proved \ retval 0 The words do not match \ retval 1 word guessed * / 
bool guessWord (const string & init_word, const string & word) 
{return init_word = = word;} 
/ *!  \ Brief Check if the chosen letter has already been attempted \ param lettersTried letters attempt \ param letter letter proves \ retval 0 point not yet attempted \ retval 1 letter attempted * / 
bool alreadyTried (vector <char> lettersTried, char letter) 
{for ( int i = 0; i <lettersTried.size (); i + +) if (lettersTried.at (i) == letter) return true; return false;} 
/ *!  \ Brief Check whether the word choice has been tried \ param wordTried words already tried \ param word word attempted \ retval 0 no word yet attempted \ retval already attempted a word * / 
bool alreadyTried (vector <string> wordTried, string word) 
{for (int i = 0; i <wordTried.size (); i + +) if (wordTried.at (i) == word) return true; return false;} 
/ *!  \ Brief Print, with rep.  set theory, a container \ container set of param elem.  is printed * / 
template void <class T> prittify (T container) {court <<"{" for (int i = 0; i <container.size (); i + +) 
{court <<container.at (i) ; if (i <container.size () -1) court <<"";} court <<"}";} 
int main () 
{const int MAX_ERRORS = 7; # if OS == 0 string word (getpass ("Secret Word:")); SO # elif 1 == string word; court <<"Secret Word:" getline (cin, word); system ("cls") # endif string letters = fill (word. size ()); 
<char> lettersTried vector, vector 
<string> wordTried; int errors = 0; do {string guess; printLetters (letters) 
court <<endl; court <<"[" <<errors <<"errors ] <<"getline (cin, guess, '\ n') 
court <<endl; if (guess.size () == 0) {court <<" Letters = try; prittify 
(lettersTried) court < <endl <<"try words ="; prittify (wordTried) court <<endl <<endl; continue;} if (guess.size ()! word.size = ()) {if (! alreadyTried (lettersTried, guess . at (0))) 
{lettersTried.push_back (guess.at (0)); if (! guessLetter (word, letters, guess.at (0))) {court <<guess.at (0) <<" not present "<<endl <<endl; errors + +;}} else {court <<" Attempting already 'done "<<endl <<endl;}} else {if (! alreadyTried (wordTried, guess)) {wordTried.push_back (guess) 
if (! guessWord (word, guess)) {court <<guess <<"is not 'the right word" <<endl <<endl; errors + +;} 
else if (word == guess) {letters = guess;}} else {court <<"Attempting already" <<endl <<endl;}} if (word == letters) break;} 
while (errors <MAX_ERRORS) printLetters (letters) court <<endl; if (errors <MAX_ERRORS) court <<"guessing" <<errors <<"error" <<endl <<endl; 
else if (errors == MAX_ERRORS) court <<"Sorry" <<endl;}
The input of the passphrase using the function getpass. If you are using a Windows system, change
# Define SW 1
in
# Define SO 0
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Traditional hangman game not working in C"
Thread Thread Starter Forum Replies Last Post
Defraggler 2.07 detects the traditional laptop'sHD as SSD KanthaBEN Windows Software 7 01-03-2012 10:51 AM
How to connect traditional speaker to Ipod Mini Jack tHeDa! Portable Devices 5 17-02-2012 07:03 PM
Cannot input Traditional Chinese language on Windows 7 Barraq Customize Desktop 2 12-03-2011 07:54 AM
Traditional Indian sport games on Sony PlayStation 2 Jeffrey Video Games 3 10-12-2009 03:48 PM
Join SQL Traditional or ANSI Deep23 Software Development 2 23-04-2009 12:07 AM


All times are GMT +5.5. The time now is 08:24 AM.