Thread: Need Help Scrambling Words for a Game.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    Need Help Scrambling Words for a Game.

    Hello! I am trying to make a unscrambling game. I need to know how to scramble words that I read from a file? Also how would I loop the same word back the prompt if I unscramble the word wrong?

    Code:
    #include <iostream>
    using namespace std;
    #include <fstream>
    #include <cstring>
    #include <cstdlib>
    
    
    int main()
    {
    	ifstream list;
    	char word[15], name[15], choice, answer[15]; 
    
    	
    	list.open ("list.txt");
    
    	do	
    	{
    		 
    		list >> name;
    		cout << "Unscramble the following word: " <<name << endl;
    		cout << "Write your answer: ";
    		cin >> answer;
    
    		while(strcmp(answer, name) == 0)
    		{
    			cout << "Great!\n";
    			break;
    		}
    		while(!strcmp(answer,name) == 0) 
    		{
    			
    			cout <<"Try again.\n";
    			break;
    		}
    		
    		if(strcmp(answer,name) == 0)
    		{
    			cout << "Do you want to play again [y/n] ";
    			cin >> choice;
    
    			if(choice == 'n' || choice == 'N') 
    				{
    					cout <<"Bye!\n";
    					break;
    				}
    		}
    		
    
    	
    	}while (!list.eof());
    
    	
    	return 0;
    	
    
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Your two inner while() loops are worthless. What's the point of immediately break-ing? while() loops are loops, and loops are meant to have the possibility of looping. Your inner while() loops will only 'loop' once. Your program structure need to be re-thunk.

    As for scrambling words, the easiest way would be to select two characters at random from the word, and swap them. Repeat a few times, maybe 5, 10, the strlen() of the word...

    (This is probably better situated in the C++ Forum as well. Please be sure to post in the correct location, help will come quicker!)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Moved to correct forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Once the word is scramble how do you unscramble the word back into the original order?

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Once the word is scramble how do you unscramble the word back into the original order?
    Why would you need to do that? You know the original word.

  6. #6
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by slickwilly440
    Once the word is scramble how do you unscramble the word back into the original order?
    Its easy to unscramble back if the original list has a pattern or if it is sorted.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    I am a real noob to programming. When the word is scramble, the word variable changes, so I need to know how to unscramble the word into the original order so the program can compare the users input to the original word to check if the user unscramble the word correctly. Bye the way I am using the swap function to scramble the words. Also for example the char variable word[15] hold the a set of letters and char original, so how do you correctly make "original=word" without getting an error?

  8. #8
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Why don't you make two copies of the array. One will contain all of the words. The other would correspond to the scrambled words. That way unscrambledArray[5] would correspond to scrambledArray[5].

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Use strings, and copy into a char array. Then randomize your char array.
    When the user inputs the guessed word, input it to a string. And compare the user string with the original string.

    Like so:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    	string example;
    	int string_length; //Int to hold string length so you know how much to copy.
    	char randomized_word[40];
    
    	memset(randomized_word, '\0', 40); /*This 'clears' everything
             in the memory space of the char array. Other wise, if you copy
     less characters than total array you will have a bunch of garbage */
    	
    	example = "word or sentence to be randomized";
    
    	string_length = example.length(); // Returns string length and places it variable
    
    	example.copy(randomized_word,string_length); /*This is the bread and butter,
     first variable in the () is the array name, then coma then the array length */	
    
    
    	cout << randomized_word; //You still have to randomize this array, but I will leave that up to you. 
    But now you have a string (to compare easily, with the
     == operator to what the user inputs. And an array to randomize to output.
    
    
    	return 0;
    
    
    }
    *edit*
    It took a bunch of edits, but I think I finally fixed the spacing error (the forum screwed up my spacing in program and made the page really freakin wide!).
    Last edited by Enahs; 10-08-2005 at 10:20 PM.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Thank you so much Loctan, the "example.copy(randomized_word,string_length);" becaue I never heard of it until know and that is just what I needed.

    Okay now I need help on how to loop the same question with the same exact word if the user enters the wong scrambled word.

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Nevermind, I just figured it out! Thanks alot guys!

    Okay, what would I use if I have to select a word randomly from a list?
    Last edited by slickwilly440; 10-08-2005 at 11:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. So you want to be a game programmer?
    By dxfoo in forum Game Programming
    Replies: 23
    Last Post: 09-26-2006, 08:38 AM
  4. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM
  5. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM