Jump to content
  • Advertisement
Sign in to follow this  
ToxicCROSSFIRE

Help with simple C++ Word Jumble Game

This topic is 4678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys I've been lurking here for awhile now and decided to start posting. I've been reading Michael Dawson's Beginning C++ Game Programming book. I'm at the end of chapter three and I've already hit a landmine. :( The book has exercises that you can do to review code at the end of each chapter. Anyways, one is to make a word jumble program (where you guess words and get a hint) with a scoring system. I must be missing something obvious with loops because this thing is acting like one messed up little puppy!I used Python for awhile before going to C++ and assumed the loops would work the same. I used to have it where it was stuck in an infinite loop, but I messed with the code a little. Now it won't go more than one round through the loop before closing the program. What I want it to do is keep going on until you enter 'quit' and I want it to give you a hint when you enter 'hint'. I also want it to deduct points when you ask for a hint. When it somewhat worked(before I messed with the code..) the program subtracted points for getting the wrong answer, which I didn't want it to do. It also gave points everytime you tried to quit(but now it quits even when you don't ask to quit). Maybe someone can help me out here since I don't want to give up on this thing (however small it may be.) Sorry it's not commented, hope it's not too confusing. The code:
//Word Jumble 2.0
//Because the book told me to.
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
   int points=0;
   int gameloop=1;
   enum fields{WORD,HINT,FIELDNUM};
   const int NUMWORDS=5;
   const string WORDS[NUMWORDS][FIELDNUM]=
   {
      {"wall","You can bang your head against this."},
      {"glasses","You might be able to read this with these."},
      {"tired","Sleepy? Had a hard day at work?"},
      {"persistent","Keep at it!"},
      {"jumble","It's what this game is all about!"}
   };
   cout<<"\t\t\tWelcome to the Word Scramble Game!!\n\n";
   cout<<"In this game you look at scrambled words and guess at what ";
   cout<<"they are!\n";
   cout<<"Enter 'hint' for a hint!\n";
   cout<<"Enter 'quit' to quit!\n\n";
   cout<<"You gain score by guessing the word correctly!\n";
   cout<<"If you ask for a hint you lose points equal to the word's length!";
   cout<<"If you get it wrong, you also lose points equal to the word's length.";
       
       
   while (gameloop==1)
      {
         srand(time(0));
         int choice=(rand()%NUMWORDS);
         string theword=WORDS[choice][WORD];
         string thehint=WORDS[choice][HINT];
         string jumble=theword;
         int length=jumble.size();
         for(int i=0; i<length; ++i)
         {
            int index1=(rand()%length);
            int index2=(rand()%length);
            char temp=jumble[index1];
            jumble[index1]=jumble[index2];
            jumble[index2]=temp;
         }
         cout<<"\n\nThe jumble word is: "<<jumble;
         string guess;
         cout<<"\nTake a guess!: ";
         cin>>guess;
         cin.ignore();
         while((guess!="quit")&&(guess!=theword))
         {
            if (guess=="hint")
               cout<<thehint;
               cout<<"\nThis cost you "<<theword.size()<<" points!\n\n";
               points-=theword.size();
            if((guess!="hint")&&(guess!=theword))
               cout<<"Sorry, that's not it!\n";
            cout<<"Try again!: ";
            cin>>guess;
            cin.ignore();
         }
         if(guess==theword)
            cout<<"You got the word!";
            cout<<"You gain "<<theword.size()*3<< "points!\n\n";
            points+=theword.size()*3;
            cout<<"You have "<<points<<" points!\n\n";
            
         if (guess=="quit")
            cout<<"Good day then!";
            cout<<"You quit with "<<points<<" points!\n\n";
            gameloop=0;
         continue;
         
      }          
   cin.ignore();
   return 0;      
}  

Thanks guys.

Share this post


Link to post
Share on other sites
Advertisement
well for one you need '{'s around your if blocks..
i.e.

if (a == 1)
{
do_this();
do_that();
}

your way will only do_this() and ALWAYS do_that()..

Share this post


Link to post
Share on other sites
What brad said :)

Since your if bodies aren't enclosed in brackets, it'll execute the first line if the condition is true, and the rest regardless.

Which is also why your program executes the loop once, as "gameloop=0" is always executed regardless of input.

On a side note, what dev environment are you using? Like Codewarrior, VC++, etc? Regardless of which you're using, I highly recommend you familiarize yourself with the debugger :) "Stepping" through code, executing one line at a time and "watching" variables to see what's going on will quickly bring to light a bunch of common errors :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!