2 Replies - 12742 Views - Last Post: 19 July 2007 - 08:41 AM Rate Topic: -----

#1 rainbow78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-July 07

word jumble c++

Post icon  Posted 18 July 2007 - 08:38 PM

I'm lost on a few things to do after this, can anyone give me some help or hints
here what I got left:



Restrict the user to 5 guesses only.


Add a scoring system that assigns a point value to each word.
Base the point value on the word length (hint: use string funtions).
Deduct a point every time the player asks for a hint.
When the player guesses the word, tell him how many points he earned.

Add a loop so the player can guess another word.
Keep a running total of the overall points.


what I got so far is:
// Word Jumble
// The classic word jumble game where the player can ask for a hint

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "Do you feel you're banging your head against something?"},
{"glasses", "These might help you see the answer."},
{"labored", "Going slowly, is it?"},
{"persistent", "Keep at it."},
{"jumble", "It's what the game is all about."}
};

srand(time(0));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; // word to guess
string theHint = WORDS[choice][HINT]; // hint for word

string jumble = theWord; // jumbled version of word
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 << "\t\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " << jumble;

string guess;
cout << "\n\nYour guess: ";
cin >> guess;

while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
cout << theHint;
else
cout << "Sorry, that's not it.";

cout <<"\n\nYour guess: ";
cin >> guess;
}

if (guess == theWord)
cout << "\nThat's it! You guessed it!\n";

cout << "\nThanks for playing.\n";

return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: word jumble c++

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: word jumble c++

Posted 19 July 2007 - 05:28 AM

Can you describe the problem you are having? any error messages?
Was This Post Helpful? 0
  • +
  • -

#3 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: word jumble c++

Posted 19 July 2007 - 08:41 AM

Neat program... to restrict the user to only 5 guesses you need to use a FOR loop and restric the max value ( which you can make a constant as I have done ) to 5. For allowing the user to guess again you need to put your code in a DO WHILE loop which will DO the code 1 time regardless and at the end you prompt for a repeat and DO the code again WHILE the repeat character is valid. For the point system, since you are using strings you can use the .size() operator so if the guess is valid then you say guess.size() to get the # of letters in the word and "+=" and essentially continuously add the total # of letters of the correct guess to the points! To decrease one you just use points--; to subtract 1 from the user.

WALL OF TEXT I KNOW! :)

You can clean up the code however you want but this does exactly what you want:

Note: you may want to add more error checking for the user choice at the end to play again and clean up the variable names and comment!

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	enum fields {WORD, HINT, NUM_FIELDS};
	const int NUM_WORDS = 5;
	const int MAX_GUESSES = 5;
	const string WORDS[NUM_WORDS][NUM_FIELDS] =
	{
	{"wall", "Do you feel you're banging your head against something?"},
	{"glasses", "These might help you see the answer."},
	{"labored", "Going slowly, is it?"},
	{"persistent", "Keep at it."},
	{"jumble", "It's what the game is all about."}
	};

	string guess;
	int points = 0;
	char another;


	do
	{
		system("cls");
		srand(time(0));
		int choice = (rand() % NUM_WORDS);
		string theWord = WORDS[choice][WORD]; // word to guess
		string theHint = WORDS[choice][HINT]; // hint for word

		string jumble = theWord; // jumbled version of word
		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 << "\t\t\tWelcome to Word Jumble!\n\n";
		cout << "Unscramble the letters to make a word.\n";
		cout << "Enter 'hint' for a hint.\n";
		cout << "Enter 'quit' to quit the game.\n\n";
		cout << "The jumble is: " << jumble;
	
		for(int nIndex = 0; nIndex < MAX_GUESSES; nIndex++)
		{
			cout << "\n\nGuess " << nIndex+1 << ": ";
			cin >> guess;

			if (guess == "hint")
			{
				cout << theHint;
				points--; //subtract 1 point 
			}//end if
			else if (guess == theWord)
			{
				cout << "\nThat's it! You guessed it!\n";
				points += guess.size(); //points = # of letters in word
				break;
			}//end else if
			else
			{
				cout << "Sorry, that's not it.";
			}//end else
		}//end for
		cout << "\n\nYour Total Points Are: " << points;
		cout << "\n\n\nWould You Like To Play Again? (y/n): ";
		cin  >> another;
	}while(another == 'y' || another == 'Y');

	system("cls");
	cout << "Thanks for playing!";

	return 0;
}//end main


This post has been edited by Topher84: 19 July 2007 - 08:42 AM

Was This Post Helpful? 1

Page 1 of 1