Results 1 to 8 of 8
  1. #1
    egriz10 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Word Scrambler game

    Hey guys, I'm working on a small word scrambler game. The game has a preset list of words to use. It randomly picks a words to use. Within that word, it randomly cycles through the letters to scramble up the word.

    My problem is that when it scrambles the word, some letters will not come up, and some will be repeated. For example: umbrella will become aellraba.

    A bit of help would be nice, or if there is a function that automatically scrambles the word?

    Thank you :)

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
         
        public class WordGame {
                public static void main(String[] args) {
                        Scanner scan = new Scanner(System.in);
                        Random rand = new Random();
                        Random rand2 = new Random();
                        String[] WordList1 = {"pirate", "dog", "adult", "aeroplane", "air", "aircraft carrier", "airforce", "airport", "album", "alphabet", "apple",
        "arm", "army", "baby", "backpack", "balloon", "chess board", "chief", "child", "chisel", "chocolates", "church", "circle", "circus", "clock", "clown",
        "coffee", "coffee-shop", "comet", "compact disc", "compass", "computer", "crystal", "cup", "cycle", "database", "desk", "diamond", "dress", "drill", "drink", "drum",
        "ears", "earth", "electricity", "fruit", "fungus", "horse", "meteor", "microscope", "milkshake", "mist", "money", "monster", "school", "sex", "ship",
        "skeleton", "slave", "snail", "software", "solid", "space", "shuttle", "star", "stomach", "sunglasses", "surveyor", "swimming pool", "tiger", "tunnel", "typewriter",
        "umbrella", "vacuum", "window", "woman", "worm"};
         
                        int Length1 = WordList1.length;
                        int Choice = rand.nextInt(Length1);
                        String WordChoice = WordList1[Choice];
                        System.out.println(WordChoice);
                        char mCharArray[] = WordChoice.toCharArray();
                       
                        int RandNumb = rand2.nextInt(mCharArray.length);
                        int RandNumb2 = rand2.nextInt(mCharArray.length);
                        int x = 0;
                        int x1 = RandNumb;
                        int x2;
                        while (x < mCharArray.length){
                        	System.out.print(mCharArray[x1]);
                        	x2 = x1;
                        
                        	if (x1 == x2){
                        		x1 = rand2.nextInt(mCharArray.length);
                        	}
                        	x = x + 1;
                      
                        }
                        
                }
         
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    I can't really follow the logic of your scrambling algorithm. I would guess that the problem is that you aren't keeping track of the letters you've already used.

    You could also convert the char array to a List and use Collections.sort(), I suppose.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    egriz10 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Well it's supposed to pick a random number between 0 and the length of the word, and if that letter was just already picked, to randomly pick another letter.

    This is where I'm stuck.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    That might be your problem- what if the letter was already picked, but it wasn't the most recent letter picked?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    egriz10 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    That might be your problem- what if the letter was already picked, but it wasn't the most recent letter picked?
    Yeah this is the problem I'm running into. It's able to make sure it doesn't repeat the letter that was just picked. However I can't think of a way to ensure it doesn't repeat any letter that was already picked.

    Any thoughts?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If you do not have to write the code yourself then use Collections.shuffle.

    If you have to write the code...
    Java Code:
    generate random number (this is for how many times to swap)
    loop {
        generate random number index one
        generate random number index two
        swap letters at index one and two
    }
    There is a chance that the two indicies will be the same but who cares? Swap them anyway. Or you can add an if statement to only swap if they are not equal.

  7. #7
    egriz10 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Success!

    Thank you for the help everybody :)

    Java Code:
    package WordGame;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    import java.util.Random;
    import java.util.ArrayList;
         
    public class WordGame {
                public static void main(String[] args) {
                        Scanner scan = new Scanner(System.in);
                        Random rand = new Random();
                        String[] WordList1 = {"pirate", "dog", "adult", "aeroplane", "air", "aircraft carrier", "airforce", "airport", "album", "alphabet", "apple",
        "arm", "army", "baby", "backpack", "balloon", "chess board", "chief", "child", "chisel", "chocolates", "church", "circle", "circus", "clock", "clown",
        "coffee", "coffee-shop", "comet", "compact disc", "compass", "computer", "crystal", "cup", "cycle", "database", "desk", "diamond", "dress", "drill", "drink", "drum",
        "ears", "earth", "electricity", "fruit", "fungus", "horse", "meteor", "microscope", "milkshake", "mist", "money", "monster", "school", "sex", "ship",
        "skeleton", "slave", "snail", "software", "solid", "space", "shuttle", "star", "stomach", "sunglasses", "surveyor", "swimming pool", "tiger", "tunnel", "typewriter",
        "umbrella", "vacuum", "window", "woman", "worm"};
                        String wordchoice;
                        int Length1 = WordList1.length;
                        int Choice = rand.nextInt(Length1);          
                        List listA = new ArrayList();
                        wordchoice = WordList1[Choice];
                        System.out.println(wordchoice);
                        for (int i =0; i < wordchoice.length(); i++){
                        	listA.add(wordchoice.charAt(i));
                        }
                	System.out.println(shuffler(listA));
                } 
                public static String shuffler(List a){
                	String word = "";
                	Collections.shuffle(a);
                	for (int j = 0; j < a.size(); j++){
                		word += a.get(j);
                	}
                	return(word);
                }
             
    }

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    String concatenation in a loop creates a lot of new objects which are then to be garbage collected, slowing down the program. I recommend you use StringBuilder#append(...) instead of String += ... inside the for loop.

    db

Similar Threads

  1. [Help] word guessing game
    By joshua03v in forum Java Gaming
    Replies: 0
    Last Post: 04-03-2011, 03:40 AM
  2. trying to write a program for hangman word game
    By durdanto in forum New To Java
    Replies: 1
    Last Post: 02-12-2011, 02:53 AM
  3. String builder scramble word game
    By moncur in forum New To Java
    Replies: 4
    Last Post: 10-22-2010, 03:14 AM
  4. Help with a word guessing game.
    By The_Round_One in forum New To Java
    Replies: 6
    Last Post: 05-19-2010, 04:22 AM
  5. The 3 Word Story [Mini Game]
    By Eku in forum Forum Lobby
    Replies: 90
    Last Post: 11-16-2008, 09:01 PM

Posting Permissions

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