DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Hangman Game Help

  1. #1
    Join Date
    Mar 2009
    Posts
    24

    Hangman Game Help

    I have to create a Hangman game where the user is asked how they want to populate the list of words. They could do this either by a text file or by entering the words on the screen. Once the list of words are populated they will be asked if they want to play a game of hangman. If no then a message is displayed along with a message that tells them they have won and lost 0 games. If they choose to play a game a game is displayed. After playing the game they are asked if they want to play again. If yes another game is displayed. If no then a message is displayed along with a message that tells them how many wins and losses they have.

    My problem is that the wins and losses are not displaying correctly.

    If I play only one game and lose the game I get Wins = 2009252579 and Losses = 1 displayed.

    If I play only one game and win the game I get Wins = 2009252580 and Losses = 0 displayed.

    If I play 2 games and lose I get Wins = 2009252579 and Losses = 2 displayed.

    If I play 2 games and win I get Wins = 2009252581 and Losses = 0 displayed.

    Why is the wins being displayed like this?

    I've attached a zip of the files.
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    i have not looked at the zip file, but it seems that you have omitted to initialize the variable that holds the number of wins/losses.

    if the variable holding the number of wins is a free variable with automatic (or dynamic) storage duration, make sure that it is initialized with zero.

    if it is a non-static member of a class, make sure that every constructor for the class initializes its value with zero.

  3. #3
    Join Date
    Mar 2009
    Posts
    24
    Thanks. I did that and that seems to have worked as far as setting total wins and losses to 0 at start of play. When I play more than one game and lets say I win 2 and lose 1 it displays the total wins as 0 and total losses as 1. How do I keep a total win and a total loss until the user stops playing? Why was it that total wins was initialized to a huge number and total losses initialized to 0?
    Last edited by justinsbabe5000; 04-05-2009 at 08:22 PM.

  4. #4
    Join Date
    Dec 2007
    Posts
    401
    > How do I keep a total win and a total loss until the user stops playing?

    for this, i need to look at your code:

    in main, this is the loop that playes one game.
    Code:
    	while(toupper(answer) == 'Y'){
    
    		CurrentGame c(d.getWord());
    
    
                   // elided .....
    
    		cin>>answer;
    
    		
    
    		if(toupper(answer) == 'N'){
    
    			cout<<"Wins = "<<c.getWins()<<" and Losses = "<<c.getLosses()<<endl;
    
    	        cout<<"Thank you for playing.";
    
    		} // end if
    the variable c (of type CurrentGame) is of an automatic storage duration; a new object is created each time the loop is executed (and destroyed at the end of the loop). to keep track of totals, you need variables that have a longer life time than c:

    Code:
            int total_wins = 0 ;
            int total_losses = 0 ;
    
    	while(toupper(answer) == 'Y'){
    
    		CurrentGame c(d.getWord());
    
                   // elided .....
    
                   total_wins += c.getWins() ;
                   total_losses += c.getLosses() ;
    
    		if(toupper(answer) != 'Y' ){
    
    			cout << "Wins = " << total_wins << " and Losses = "
                                    << total_losses <<  "Thank you for playing.\n" ;
    
    		} // end if
    > Why was it that total wins was initialized to a huge number and total losses initialized to 0?

    both were uninitialized. the values depend on the (unpredictable) contents of the memory which was used to store these variables when they were created.

  5. #5
    Join Date
    Mar 2009
    Posts
    24
    Thanks. This worked perfectly.

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2007, 05:26 PM
  2. Replies: 3
    Last Post: 06-23-2006, 05:44 AM
  3. Replies: 2
    Last Post: 10-29-2005, 11:47 AM
  4. csharp Hangman game
    By Moni in forum .NET
    Replies: 1
    Last Post: 07-02-2002, 03:54 AM
  5. Framework of a complex game...
    By AGuy in forum Java
    Replies: 0
    Last Post: 07-31-2000, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center