4 Replies - 1870 Views - Last Post: 21 March 2013 - 10:57 AM Rate Topic: -----

#1 saki55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 13

Word Jumble

Posted 15 March 2013 - 07:53 AM

Hi, I would like to ask some help about my code.
This is a word jumble game, in a sense only the words are 'prearranged' in the code and the hint is readily available.
I'm trying to solve the issue of giving more freedom to the user since my code only accepts 'lowercase' letters while I want the user to be able to answer in which case he prefers.

I'm also trying to sort it by difficulty but I kind of gave it up.
I'm also having problem with the score. Let's say you play it and got a score of 500 then you played it again and gets a 600, the 'highscore' displays
P1 - 600
P2(up to P10) - 500


and sorry, the code is kind of messed up right now(I'm in a bit of a hurry so I haven't made it 'pretty' yet. I'm juggling my options to use 'switch' rather than 'if...else' at the moment. )
 
# include <iostream>
# include <string>
# include <iomanip>
# include <cmath>
# include <cstdlib>
using namespace std;
struct words{
string description;
string puzzle;
string answer;

int points;
};
struct player{
string name;
int score;
};
const int wordCount = 5;
int score = 0;
words easyWords[wordCount];
words mediumWords[wordCount];
words hardWords[wordCount];

player highscorers[10];

void initialize(){

easyWords[0].description ="clue: open it to enter you home";
easyWords[0].puzzle = "ORDO";
easyWords[0].answer = "door";
easyWords[0].points = 1000;

easyWords[1].description = "clue: made from trees ";
easyWords[1].puzzle = "PPERA";
easyWords[1].answer = "paper";
easyWords[1].points = 1000;

easyWords[2].description = "clue: i can see you wearing this";
easyWords[2].puzzle = "HSRIT";
easyWords[2].answer = "shirt";
easyWords[2].points = 1000;

easyWords[3].description = "clue:i see you not laughing, you lack this sense i guess";
easyWords[3].puzzle = "MUOHR";
easyWords[3].answer = "humor";
easyWords[3].points = 1000;

easyWords[4].description = "we need this for us to be able to smell";
easyWords[4].puzzle = "ONSE";
easyWords[4].answer = "nose";
easyWords[4].points = 1000;

mediumWords[0].description = "clue: money demanded for releasing captive";
mediumWords[0].puzzle = "NRMSOA";
mediumWords[0].answer = "ransom";
mediumWords[0].points = 3000;

mediumWords[1].description = "clue: synonym of the  word for envious";
mediumWords[1].puzzle = "SOUVCTOE";
mediumWords[1].answer = "covetous";
mediumWords[1].points = 3000;

mediumWords[2].description = "clue: a group needs this";
mediumWords[2].puzzle = "EATOKRMW";
mediumWords[2].answer = "teamwork";
mediumWords[2].points = 3000;

mediumWords[3].description = "clue: the state of your heart after you've lost something";
mediumWords[3].puzzle = "ORENKB";
mediumWords[3].answer = "broken";
mediumWords[3].points = 3000;

mediumWords[4].description = "clue: use as an output device for computer.";
mediumWords[4].puzzle = "ITORNOM";
mediumWords[4].answer = "monitor";
mediumWords[4].points = 3000;

hardWords[0].description ="clue: it makes a person happy";
hardWords[0].puzzle = "MCOPLENTIM";
hardWords[0].answer = "compliment";
hardWords[0].points = 5000;

hardWords[1].description ="clue: subject about solutions";
hardWords[1].puzzle = "EMCIHTRSY";
hardWords[1].answer = "chemistry";
hardWords[1].points = 5000;

hardWords[2].description ="clue: I know you want to be this";
hardWords[2].puzzle = "LLIMONREIAI";
hardWords[2].answer = "millionaire";
hardWords[2].points = 5000;

hardWords[3].description ="clue: i see you're ......... with C++ functions and operators";
hardWords[3].puzzle = "VREOADLEDO";
hardWords[3].answer = "overloaded";
hardWords[3].points = 5000;

hardWords[4].description ="clue: Many students cram because of this";
hardWords[4].puzzle = "RCPOASNATTIINO";
hardWords[4].answer = "procastination";
hardWords[4].points = 5000;
}

void play(string level, int points, words wordsArr[]){
string ans;

for(int i=0; i < wordCount; i++){
cout<<"\n\n\n\n\n"<<"\t\t\t\t";
cout<<level<<" LEVEL"<<"\n\n";
cout<<"\t\t"<<"Note : You must answer in small letters."<<"\n\n";
cout<<"\t\t\t"<<"Player can only answer once."<<"\n\n\n";

cout<<"\t\t"<< wordsArr[i].description <<"\n\n";
cout<<"\t\t\t"<< wordsArr[i].puzzle <<"\n\n";
cout<<"\t\t\tAnswer: ";
cin>>ans;

if(ans == wordsArr[i].answer){
score += wordsArr[i].points;
cout<<"\n\n"<<"Press enter to continue"<<endl;
cin.ignore();
system("cls");
}else{
cout<<"\t\t\t"<<"Incorrect!"<<endl;
system("PAUSE");
system("cls");
}

}
}


void start(){
int select;

mymenu:
cout<<"\n\n\n\n\n\n";
cout<<"\t\t\t WORD TWISTER"<<"\n\n\n";
cout<<"\t\t\t1 New Game"<<"\n\n\n";
cout<<"\t\t\t2 Instructions"<<"\n\n\n";
cout<<"\t\t\t3 High scores"<<"\n\n";
cin>>select;
cin.ignore();
system("cls");

if (select == 1){
string name;
play("EASY", easyWords[0].points, easyWords);
play("MEDIUM", mediumWords[0].points, mediumWords);
play("HARD", hardWords[0].points, hardWords);

cout<<"Enter your name: ";
cin>>name;

for(int i = 0; i < 10; i++){
if(highscorers[i].score < score){

for(int j = i; j < 9; j++ ){
highscorers[j+1] = highscorers[j];
}
highscorers[i].score = score;
highscorers[i].name = name;

break;
}
}

score = 0;
cin.ignore();
system("cls"); 
goto mymenu;

}
else if (select == 2){
cout<<"\t\t\tMECHANICS OF GAME"<<"\n\n\n";
cout<<"1. There are three rounds for this game. (Easy, Medium and Hard)."<<endl;
cout<<"2. The player must be able to arrange the letters correctly to earn points."<<endl;
cout<<"3. After each round the points gets higher.\n"
	<<"   And lastly, The player should write his name after the game for the score"<<endl;
cout<<"\nEASY ROUND: Consist of FOUR TO FIVE LETTERS."<<endl;
cout<<"\nMEDIUM ROUND: Consist of SIX TO EIGHT LETTERS."<<endl;
cout<<"\nHARD ROUND: Consist of NINE TO FOURTEEN LETTERS."<<endl;

cin.ignore();
system("cls"); 
goto mymenu;
}
else if (select == 3){
cout<<"\n\n\n";
cout<<"\tRank"<<"\t\tName"<<"\t\t\tScore\n\n"<<endl;
for (int i = 0; i<10; i++){
cout<<"\t"<<i+1<<"\t\t"<<highscorers[i].name<<"\t\t\t"<<highscorers[i].score<<"pts."<<endl;
}
cin.ignore();
system("cls");
goto mymenu;
}
}

int main() 
{

initialize();
start();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Word Jumble

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Word Jumble

Posted 15 March 2013 - 09:28 AM

The solution to the lowercase problem is to immediately change whatever the user enters, to lowercase, as far as the internals of your program go. You can display the upper or mixed case on the console, but internally, use only lowercase.

The first person that good indentation will help -- is you. As you continue to code with a good style, you are also "teaching" your brain to recognize indented code. After a while, your brain will be trained well enough to pick out common errors in syntax and logic, with just a quick glance.

But that brain training won't happen nearly as well, without a good and consistent indentation style, and time spent working with it, of course.

So pretty isn't just pretty - it's pretty practical.
Was This Post Helpful? 0
  • +
  • -

#3 saki55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 13

Re: Word Jumble

Posted 15 March 2013 - 04:55 PM

I fixed the indentation already and it runs without errors. I researched on the case problem and I found 'toupper' but I don't really know where to put it in my code. (I am but a humble student that is forced to take a programming course even if I'm a bio major. I'm a newbie. )


So far, what I'm trying to deal with is to make the end of the program display the "Highscores" after you enter your name and gives the score but I'm failing at it.

# include <iostream>
# include <string>
# include <iomanip>
# include <cmath>
# include <cstdlib>
using namespace std;

struct words
{
	string description;
	string puzzle;
	string answer;

	int points;
};

struct player
{
	string name;
	int score;
};

const int wordCount = 5;
int score = 0;
words easyWords[wordCount];
words mediumWords[wordCount];
words hardWords[wordCount];

player highscorers[10];

void initialize()
{
	//easy level
	easyWords[0].description ="clue: open it to enter you home";
	easyWords[0].puzzle = "ORDO";
	easyWords[0].answer = "door";
	easyWords[0].points = 1000;

	easyWords[1].description = "clue: made from trees ";
	easyWords[1].puzzle = "PPERA";
	easyWords[1].answer = "paper";
	easyWords[1].points = 1000;

	easyWords[2].description = "clue: i can see you wearing this";
	easyWords[2].puzzle = "HSRIT";
	easyWords[2].answer = "shirt";
	easyWords[2].points = 1000;

	easyWords[3].description = "clue:i see you not laughing, you lack this sense i guess";
	easyWords[3].puzzle = "MUOHR";
	easyWords[3].answer = "humor";
	easyWords[3].points = 1000;

	easyWords[4].description = "we need this for us to be able to smell";
	easyWords[4].puzzle = "ONSE";
	easyWords[4].answer = "nose";
	easyWords[4].points = 1000;

	//medium level
	mediumWords[0].description = "clue: money demanded for releasing captive";
	mediumWords[0].puzzle = "NRMSOA";
	mediumWords[0].answer = "ransom";
	mediumWords[0].points = 3000;

	mediumWords[1].description = "clue: synonym of the  word for envious";
	mediumWords[1].puzzle = "SOUVCTOE";
	mediumWords[1].answer = "covetous";
	mediumWords[1].points = 3000;

	mediumWords[2].description = "clue: a group needs this";
	mediumWords[2].puzzle = "EATOKRMW";
	mediumWords[2].answer = "teamwork";
	mediumWords[2].points = 3000;

	mediumWords[3].description = "clue: the state of your heart after you've lost something";
	mediumWords[3].puzzle = "ORENKB";
	mediumWords[3].answer = "broken";
	mediumWords[3].points = 3000;

	mediumWords[4].description = "clue: use as an output device for computer.";
	mediumWords[4].puzzle = "ITORNOM";
	mediumWords[4].answer = "monitor";
	mediumWords[4].points = 3000;

	//hard level
	hardWords[0].description ="clue: it makes a person happy";
	hardWords[0].puzzle = "MCOPLENTIM";
	hardWords[0].answer = "compliment";
	hardWords[0].points = 5000;

	hardWords[1].description ="clue: subject about solutions";
	hardWords[1].puzzle = "EMCIHTRSY";
	hardWords[1].answer = "chemistry";
	hardWords[1].points = 5000;

	hardWords[2].description ="clue: I know you want to be this";
	hardWords[2].puzzle = "LLIMONREIAI";
	hardWords[2].answer = "millionaire";
	hardWords[2].points = 5000;

	hardWords[3].description ="clue: i see you're ......... with C++ functions and operators";
	hardWords[3].puzzle = "VREOADLEDO";
	hardWords[3].answer = "overloaded";
	hardWords[3].points = 5000;

	hardWords[4].description ="clue: Many students cram because of this";
	hardWords[4].puzzle = "RCPOASNATTIINO";
	hardWords[4].answer = "procastination";
	hardWords[4].points = 5000;
}

void play(string level, int points, words wordsArr[])
{
	string ans;

	for(int i=0; i < wordCount; i++)
	{
		cout<<"\n\n\n\n\n"<<"\t\t\t\t";
		cout<<level<<" LEVEL"<<"\n\n";
		cout<<"\t\t"<<"Note : You must answer in small letters."<<"\n\n";
		cout<<"\t\t\t"<<"Player can only answer once."<<"\n\n\n";

		cout<<"\t\t"<< wordsArr[i].description <<"\n\n";
		cout<<"\t\t\t"<< wordsArr[i].puzzle <<"\n\n";
		cout<<"\t\t\tAnswer: ";
		cin>>ans;

		if(ans == wordsArr[i].answer)
		{
			score += wordsArr[i].points;
			cout<<"\n\n"<<"Press enter to continue"<<endl;
			cin.ignore();
			system("cls");
		}
		
		else
		{
			cout<<"\t\t\t"<<"Incorrect!"<<endl;
			system("PAUSE");
			system("cls");
		}

	}
}


void start()
{
	int select;

	mymenu:
	cout<<"\n\n\n\n\n\n";
	cout<<"\t\t\t WORD TWISTER"<<"\n\n\n";
	cout<<"\t\t\t1 New Game"<<"\n\n\n";
	cout<<"\t\t\t2 Instructions"<<"\n\n\n";
	cout<<"\t\t\t3 High scores"<<"\n\n";
	cin>>select;
	cin.ignore();
	system("cls");

	if (select == 1)
	{
		string name;
		play("EASY", easyWords[0].points, easyWords);
		play("MEDIUM", mediumWords[0].points, mediumWords);
		play("HARD", hardWords[0].points, hardWords);

		cout<<"Enter your name: ";
		cin>>name;
		cout<<"YOUR SCORE IS "<<score;

		for(int i = 0; i < 10; i++)
		{
			if(highscorers[i].score < score)
			{
				for(int j = i; j < 9; j++ )
				{
					highscorers[j+1] = highscorers[j];
				}
				highscorers[i].score = score;
				highscorers[i].name = name;

				system("pause");
				system("cls");
				goto highscore;
			}
		}
//score
score = 0;
cin.ignore();
system("cls"); 
goto mymenu;

}

	else if (select == 2)
	{
		cout<<"\t\t\tMECHANICS OF GAME"<<"\n\n\n";
		cout<<"1. There are three rounds for this game. (Easy, Medium and Hard)."<<endl;
		cout<<"2. The player must be able to arrange the letters correctly to earn points."<<endl;
		cout<<"3. After each round the points gets higher.\n"
			<<"   And lastly, The player should write his name after the game for the score"<<endl;
		cout<<"\nEASY ROUND: Consist of FOUR TO FIVE LETTERS."<<endl;
		cout<<"\nMEDIUM ROUND: Consist of SIX TO EIGHT LETTERS."<<endl;
		cout<<"\nHARD ROUND: Consist of NINE TO FOURTEEN LETTERS."<<endl;

		cin.ignore();
		system("cls"); 
		goto mymenu;
	}

highscore:
	if (select == 3)
	{
		cout<<"\n\n\n";
		cout<<"\tRank"<<"\t\tName"<<"\t\t\tScore\n\n"<<endl;
		
		for (int i = 0; i<10; i++)
		{
			cout<<"\t"<<i+1<<"\t\t"<<highscorers[i].name<<"\t\t\t"<<highscorers[i].score<<"pts."<<endl;
		}
		cin.ignore();
		system("cls");
		goto mymenu;
	}
}

int main() 
{

	initialize();
	start();
}


Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Word Jumble

Posted 18 March 2013 - 05:42 PM

Just wondering if you've made some progress?

You may want to tackle your problem in two big steps ...

1st step ... get your word game working well

2nd step ... add top 10 users after 1st step working the way you want it to work.

For the 1st step ...

I would use a file to hold all the struct data

Then ... read that file into a C++ vector of that struct

struct Puzzle
{
	int points;
	string scrambled;
	string word;
	string clue;
} ;




You file data could be saved like this ... to facilitate reading it back into ONE vector
/*
1000 ORDO door Open it to enter your home.
1000 PPERA paper Made from trees.
1000 HSRIT shirt I can see you wearing this.
1000 MUOHR humor You have this if you can laugh when appropriate.
1000 ONSE nose This allows one to sense an aroma.
3000 NRMSOA ransom Money demanded for release of captive.
3000 SOUVCTOE covetous Wanting things that others have.
3000 EATOKRMW teamwork What a group desires.
3000 ORENKB broken When something is damaged, it is ...
3000 ITORNOM monitor To watch is to ...
5000 MCOPLENTIM compliment Saying a kind thing about someone is a ...
5000 EMCIHTRSY chemistry The study of reaction rates is done in  ...
5000 LLIMONREIAI millionaire To be called a ... these days is more common.
5000 VREOADLEDO overloaded More than one can carry is to be ...
5000 NTNTSRCRPOAIAIO procrastination Putting things off till sometimes later is called ...

*/



Since you are going to ask all 15 questions ... the data might as well be in one file and then one vector container

You can then just loop through that vector with questions and getting answers and seeing if correct answer or not

You could use (a) function(s) like this to facilitate comparison

string& strToUpper( string& s )
{
	int len = s.size();
	while( len )
	{
		--len;
		s[len] = toupper(s[len]);
	}
	return s;
}
string& strToLower( string& s )
{
	int len = s.size();
	while( len )
	{
		--len;
		s[len] = tolower(s[len]);
	}
	return s;
}

This post has been edited by David W: 18 March 2013 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 saki55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 13

Re: Word Jumble

Posted 21 March 2013 - 10:57 AM

^I'm trying to study 'vector' now. It hasn't been thought to us(we don't have a lesson about it).

I had solved the problem of displaying the "highscore" at the end of the program. I'm already working on the highscore array problem.

I haven't solved the upper/lowercase problem yet.

So far, the program seems to be okay now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1