Results 1 to 4 of 4
  1. #1
    equinox55 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Scramble Java Game

    Hello I am trying to make a word scramble game in Java. I am having difficulties scrabbling the word up. We need to use a random number and substrings in order to scrabble it. Then the scramble word has to be stored in a new string. This is what I have so far. Everything works fine except the scrabbling of the letters. Any help appreciated!

    import java.util.Scanner;

    public class WordJumbleGame
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner( System.in ); //imports scanner
    String originalWord="";//holds original word from user
    String originalWordCopy="";//holds a copy of original word from user
    String scrambelWord = "";//holds jumbled word
    String tempSubString = "";
    int originalWordLength = 0; //holds word length
    int swapWith =0;
    char temp = 0;


    System.out.println("Hello welcome to the Word Jumble Game. This game requires two players. One enters the word the other gusses it!");
    System.out.println("Please enter what word would you like to scrambel?");
    originalWord = input.next().toLowerCase();//get original word set to lower case
    originalWordLength = originalWord.length();
    System.out.println("This is the orignal word " + originalWord + " lenght of word " + originalWordLength);
    originalWordCopy = originalWord;


    for(int i=0; i<=originalWordLength; i++)
    {
    int randomNumber = (int) (Math.random() * originalWordLength -i ) + 1;
    temp = originalWordCopy.charAt(randomNumber);
    scrambelWord = scrambelWord + temp;//add the random char to the scrambeled word
    System.out.println(scrambelWord + "this is the random #" + randomNumber);
    originalWordCopy = originalWordCopy.substring(1, originalWordLength);


    System.out.println(scrambelWord + " " + originalWordLength + " " + randomNumber );

    }



    //place word in to substring add the 2 substrings together make new variable for substring
    // Hint: An easy way to scramble the word is to randomly select a letter from the word, remove that letter
    // from the original word, and then add that letter onto the scrambled word. Once you have randomly removed
    // all the letters from the original word and added them on to the scrambled word, you will be done. Perhaps
    // you might need a loop to do this?

    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Are you familiar with the .substring(start,end) method for String objects? How about the .length() method?

    Are you familiar with Math.rand()?

    If so, you have everything you need. In this case, you will want to pick a random number between 0 and the length of the string minus one (because it's zero-based). Then, you want to select that substring (with a length of 1) from the string, and add it to your new string (which should start blank). Additionally, you will want to remove your substring from the old string each time, so you don't get the same letter twice.

    Always try working it out on paper first; first by an example then an algorithm:
    Java Code:
    OLD STRING		NEW STRING
    i am a bear		<empty string>
    i a a bear		m
    i aa bear		m  <there is an extra space here>
    i aa ber		m a
     aa ber			m ai
     aa er			m aib
     a er			m aiba
    a er			m aiba  <and another space>
    a r			m aiba e
     r			m aiba ea
      <just a space>	m aiba ear
    <empty string>		m aiba ear  <ends with a space>
    // Final string: "m aiba ear "
    That should at least get you thinking.

    PS: For the future, surround pertinent code in [code] tags.

  3. #3
    equinox55 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    I understand that i do not understand why my loop is not working and why it is not adding the individual letters to the new string. Also it need to just scrambel a word. Like hello.

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    The loop isn't working first of all because of this line:

    Java Code:
    temp = originalWordCopy.charAt(randomNumber);

    What if the random number that was generated is greater than your word length? It will generate an error. I recommend using a random number generator that uses the length of the string as a range so that every number generated will be a number corresponding to a character.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Java Game, Really need help
    By acash229 in forum Java Gaming
    Replies: 1
    Last Post: 09-11-2010, 07:51 AM
  2. help Java game
    By rawan in forum JCreator
    Replies: 1
    Last Post: 12-25-2009, 07:43 PM
  3. java game
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 08:01 AM
  4. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 AM
  5. Word Scramble
    By lk9865 in forum New To Java
    Replies: 5
    Last Post: 11-17-2007, 03:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •