8 Replies - 7180 Views - Last Post: 04 December 2006 - 11:42 AM Rate Topic: -----

#1 KevSmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-December 06

word sramble game help!

Posted 03 December 2006 - 02:55 PM

i am trying to make a word scramble game having different difficulties with more word lengths,,,, i am needing help "were to put how many words are in the game" how to move on to the next level" just any help will be grateful thanks





#include <iostream.h>		
#include <cstdlib>
#include <stdlib.h>				


struct player {
	int numberoflives;
	#define wordlength 20;
	
};


int main() {
	char mywords[wordlength] = "race"; 

	player MyPlayer
	MyPlayer.numberoflives = 5;
	

	bool keepPlaying = true;

	do {

		
		system("cls");
		cout << "================================================================\n\n";
		cout << "				   WORD SCRAMBLE GAME\n\n";
		cout << "================================================================\n";
		cout << "The aim of the game is to unscramble the words\n";
		cout << "You have 5 attemts at each word\n";
		cout << "s to start\n";
		cout << "q to quit\n;
		
		//
		cout << "What you want to do? (enter s or q only)\n";
		char inputdata;
		cin >> inputdata;

		if (inputdata=='s') {
			//
			//
			
			
			
			
			if (inputdata=='r') {
				cout << "correct first letter is r\n";
				if  (inputdata=='a') {
					cout << "correct now you have ra\n";
					if (inputdata=='c') {
						cout << "correct now you have rac\n";
						if (inputdata=='e') {
							cout << "congratulations you unscrambled race\n";
						}
			else if (inputdata!'r')
				MyPlayer.numberoflives = MyPlayer.numberofliveslives-1;
				cout << "wrong try again\n";
					}
			else if (inputdata!'a')
				MyPlayer.numberoflives = MyPlayer.numberoflives-1;
				cout << "wrong try again\n";
				}
			else if (inputdata!'c')
				MyPlayer.numberoflives = MyPlayer.numberoflives-1;
				cout << "wrong try again\n";
			}
			else if (inputdata!'e')
				MyPlayer.numberoflives = MyPlayer.numberoflives-1;
				cout << "wrong try again\n";


		
				if (MyPlayer.numberoflives<=0) {
					cout << "gameover\n";
					keepPlaying = false;
				}
		}


	} while (keepPlaying);

}


edit: added [code] tags ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: word sramble game help!

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: word sramble game help!

Posted 03 December 2006 - 03:44 PM

Moved to C/C++
Was This Post Helpful? 0
  • +
  • -

#3 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: word sramble game help!

Posted 03 December 2006 - 04:41 PM

This is what i would do.

This is not the code, just an illustration of the setup that you could use.

#include <iostream>
#include <string>
#include <vector>

#define filename DATA.txt

class wg {
	vector<string> data;
	int lives;
	struct {
		string word;
		string rand;
	} currentword;
	public:
	void loadData(string); //put words into data vector
	string newWord(); //randomly pick word
							 //remove word from vector
							 //add word and rand letters to currentword
							 //return currentword.rand
	bool newGuess(char); //compare guess with currentword.word
									//if true output the letter
	bool keepGoing(); //false if dead, got word, etc.
	...
};
	
int main()
{
	string word;
	bool tf;
	char c;
	wg game;
				game.loadData(filename);
	do {
		word=game.newWord();
		do {
			cout << "guess: ";
			cin >> c;
			tf=game.newGuess(c);
			tf==true? cout<<"Right!" : cout<<"Wrong!";
		} (while game.keepGoing());
...


This post has been edited by okyup: 03 December 2006 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 KevSmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-December 06

Re: word sramble game help!

Posted 03 December 2006 - 05:19 PM

Thanks but how do i get it to chose a word from a list that i have wrote myself
Was This Post Helpful? 0
  • +
  • -

#5 KevSmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-December 06

Re: word sramble game help!

Posted 04 December 2006 - 09:56 AM

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

char gameSelect, diffSelect;

int main()
{

	do {
	// MAIN MENU
	// Tells the player the different modes of play and tells them what to do to
	// select one.
	cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
	cout << "		 V G  W O R D  S C R A M B L E !			  \n";
	cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
	cout << "Welcome to VG Word Scramble!  The game where you unscramble\n";
	cout << "words to find the Video-Game related word and get points doing it!\n";
	cout << "To play Word Scramble, press 'w'\n";
	cout << "To play the 2-player game Scramble Battle, press 's'\n";
	cout << "To play Point Attack, press 'a'\n";
	cout << "To play Time Attack, press 'd'\n";
	cout << "To quit press 'q'\n";
	cout << "***Please note that all selections are in lower case***\n";
	cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n";

	cout << "Please select a game; ";
	cin >> gameSelect;

	while (gameSelect=='w') {
	   // WORD SCRAMBLE MODE
	   // Describes the game of Word Scramble
	   cout << "\n\n\n\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
	   cout << "		 W O R D  S C R A M B L E					  \n";
	   cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
	   cout << "In this game, you must unscramble 10 words in order to\n";
	   cout << "complete the game.  However, you only have a limited\n";
	   cout << "number of guesses per word, which when depleted will\n";
	   cout << "result in you losing a life.  However, for every letter\n";
	   cout << "you unscramble, you gain 1 point, and for every 10\n";
	   cout << "points you score, you will gain an extra life.  But\n";
	   cout << "be aware; incorrectly guessing a letter will also\n";
	   cout << "cost you a point!  If you need help with a word,\n";
	   cout << "simply type 'hint', but this will cost a guess.";
	   cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n";
	   cout << "To quit during the game simply type 'quit'\n";
	   cout << "Difficulty levels;\nEasy - press e\nNormal - press n\nHard - press h\n";
	   cin >> diffSelect;

	   while (diffSelect=='e') {
		  enum fields {WORD, HINT, LETTER1, LETTER2, LETTER3, LETTER4, LETTER5, NUM_FIELDS};
		  const int NUM_WORDS = 15;
		  const string WORDS[NUM_WORDS][NUM_FIELDS] =
		  {
		  {"chaos", "Water God from Sonic Adventure.", "c", "h", "a", "o", "s"},
		  {"cloud", "Everyone's favourite spikey-haired hero!", "c", "l", "o", "u", "d"},
		  {"sonic", "He runs at the speed of light.", "s", "o", "n", "i", "c"},
		  {"mario", "The fat italian we all know and love", "m", "a", "r", "i", "o"},
		  {"zelda", "There's a big legend about her.", "z", "e", "l", "d", "a"},
		  {"samus", "The space-pirate's arch enemy and bounty hunter.", "s", "a", "m", "u", "s"},
		  {"fenix", "The XBox360's killer app's hero's second name.", "f", "e", "n", "i", "x"},
		  {"aeris", "She made grown men cry.", "a", "e", "r", "i", "s"},
		  {"kairi", "The red-head from the best Disney game ever.", "k", "a", "i", "r", "i"},
		  {"crash", "He's also a bandicoot", "c", "r", "a", "s", "h"},
		  ("snake", "He's solid.", "s", "n", "a", "k", "e"),
		  ("kirby", "The floating pink puffball", "k", "i", "r", "b", "y"),
		  ("guile", "The blond, chisel-jawed American from Street Fighter.", "g", "u", "i", "l", "e"),
		  ("solar", "The energy source you needed to power your gun in Boktai.", "s", "o", "l", "a", "r"),
		  ("psone", "Sony's small version of it's first console.", "p", "s", "o", "n", "e"),
		  };

		  int word=0;
		  int points=0;
		  int total=0;
		  int subtotal;
		  int lives=3;
		  int guesses=5;
		  int letter=0;
		  char playon = 'y';

		  srand((unsigned int)time(0));

		  while ((lives>0)&&(playon=='y'))
		  {
		  int choice = (rand() % NUM_WORDS);
		  string theWord = WORDS[choice][WORD];
		  string theHint = WORDS[choice][HINT];
		  string letter1 = WORDS[choice][LETTER1];
		  string letter2 = WORDS[choice][LETTER2];
		  string letter3 = WORDS[choice][LETTER3];
		  string letter4 = WORDS[choice][LETTER4];
		  string letter5 = WORDS[choice][LETTER5];

		  string scramble = theWord;
		  int length = scramble.size();
		  for (int i=0;i<length;++i)
		  {
		  int index1 = (rand() % length);
		  int index2 = (rand() % length);
		  char temp = scramble[index1];
		  scramble[index1]=scramble[index2];
		  scramble[index2]=temp;
		  }

		  cout << "You're scrambled word is " << scramble;

		  while (letter=1)
		  {
		   string guess_one;
		   cout << "\nFirst letter? ";
		   cin >> guess_one;
		   while ((guess_one != letter1)&&(guess_one != "quit"));
		  {
				if (guess_one=="hint")
				{
				guesses - 1;
				cout << theHint;
				}
				else
				{
				cout << "Oops!  That's wrong!";
				guesses -1;
				}
				cout << "\nFirst letter? ";
				cin >> guess_one;
		 }
		  if (guess_one==letter1);
		 {
		  cout << "\nCorrect!  Next letter!\n";
		  points + 1;
		  letter + 1;
		 }
		 }

		 while (letter==2);
		 {
		   string guess_two;
		   cout << "\nSecond letter? ";
		   cin >> guess_two;
		   while ((guess_two != letter2)&&(guess_two != "quit"));
		  {
				if (guess_two=="hint")
				{
				guesses - 1;
				cout << theHint;
				}
				else
				{
				cout << "Oops!  That's wrong!";
				guesses -1;
				}
				cout << "\nSecond letter? ";
				cin >> guess_two;
		 }
		  if (guess_two==letter2);
		 {
		  cout << "\nCorrect!  Next letter!\n";
		  points + 1;
		  letter + 1;
		 }
		 }

		 while (letter==3)
		 {
		   string guess_three;
		   cout << "\nThird letter? ";
		   cin >> guess_three;
		   while ((guess_three != letter3)&&(guess_three != "quit"));
		  {
				if (guess_three=="hint")
				{
				guesses - 1;
				cout << theHint;
				}
				else
				{
				cout << "Oops!  That's wrong!";
				guesses -1;
				}
				cout << "\nThird letter? ";
				cin >> guess_three;
		 }
		  if (guess_three==letter3);
		 {
		  cout << "\nCorrect!  Next letter!\n";
		  points + 1;
		  letter + 1;
		 }
		 }

		 while (letter==4);
		 {
		   string guess_four;
		   cout << "\nFourth letter? ";
		   cin >> guess_four;
		   while ((guess_four != letter4)&&(guess_four != "quit"));
		  {
				if (guess_four=="hint")
				{
				guesses - 1;
				cout << theHint;
				}
				else
				{
				cout << "Oops!  That's wrong!";
				guesses -1;
				}
				cout << "\nFourth letter? ";
				cin >> guess_four;
		 }
		  if (guess_four==letter4);
		 {
		  cout << "\nCorrect!  Next letter!\n";
		  points + 1;
		  letter + 1;
		 }
		 }

		 while (letter==5)
		 {
		   string guess_five;
		   cout << "\nFifth letter? ";
		   cin >> guess_five;
		   while ((guess_five != letter5)&&(guess_five != "quit"));
		  {
				if (guess_five=="hint")
				{
				guesses - 1;
				cout << theHint;
				}
				else
				{
				cout << "Oops!  That's wrong!";
				guesses -1;
				}
				cout << "\nFifth letter? ";
				cin >> guess_five;
		 }
		  if (guess_five==letter5);
		 {
		  cout << "\nWell done!  Next word!\n";
		  points + 1;
		  letter +1;
		  word + 1;
		  return 86;
		 }
		 }
		  if ((lives <= 0) || (playon='n'))
		 {
		 cout << "\n\nGAME OVER!!";
		 return 1;
		 }
		 }
		 }
		 }
		 return 0;
		 }
}
This is the i now have i am stuck on when compiling it i have 1 error 26 warning if anyone can look at it for me plz to see were i have gone wrong..
Was This Post Helpful? 0
  • +
  • -

#6 okyup  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 207
  • Joined: 06-November 06

Re: word sramble game help!

Posted 04 December 2006 - 10:36 AM

Few things i noticed in the first half...

Should declare your const variables outside the while loop...

And you do not need to use strings to store each letter in the word. Just use chars, or a char array.
Was This Post Helpful? 0
  • +
  • -

#7 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: word sramble game help!

Posted 04 December 2006 - 11:06 AM

You have a lot going on in that do while loop. Why don't you break it up into seperate functions. It will make your code easier to follow and easier to debug. Your do-while does not have a while to finish it off.
Was This Post Helpful? 0
  • +
  • -

#8 KevSmith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-December 06

Re: word sramble game help!

Posted 04 December 2006 - 11:17 AM

Can Any one show me an example plz
Was This Post Helpful? 0
  • +
  • -

#9 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: word sramble game help!

Posted 04 December 2006 - 11:42 AM

Here is an example of picking a random word from a list of words, then pass that word to another function. You program should use functions for each part of the program that you do. Like picking a word, pass it to another function to scramble the word then to another function to do the guessing, finish off with yet another function to finish up the game and print out the scores etcetera.

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

void printWord( std::string &wrd );

int main()
{
	const std::string::size_type SIZE = 3;
	std::string words[SIZE] = { "Dream", "In", "Code" };
	std::srand( std::time( 0 ) );
	std::string scrambledWord = words[ std::rand() % SIZE ];

	printWord( scrambledWord );	

	return 0;
}

void printWord( std::string &wrd )
{
	std::cout << "Random word is: " << wrd << std::endl;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1