Results 1 to 8 of 8
Thread: Word Scrambler game
- 04-05-2011, 06:58 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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; } } }
- 04-05-2011, 07:01 PM #2
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!
- 04-05-2011, 07:03 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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.
- 04-05-2011, 07:38 PM #4
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!
- 04-05-2011, 11:39 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
- 04-06-2011, 02:58 AM #6
If you do not have to write the code yourself then use Collections.shuffle.
If you have to write the code...
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.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 }
- 04-06-2011, 07:01 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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); } }
- 04-06-2011, 07:36 PM #8
Similar Threads
-
[Help] word guessing game
By joshua03v in forum Java GamingReplies: 0Last Post: 04-03-2011, 03:40 AM -
trying to write a program for hangman word game
By durdanto in forum New To JavaReplies: 1Last Post: 02-12-2011, 02:53 AM -
String builder scramble word game
By moncur in forum New To JavaReplies: 4Last Post: 10-22-2010, 03:14 AM -
Help with a word guessing game.
By The_Round_One in forum New To JavaReplies: 6Last Post: 05-19-2010, 04:22 AM -
The 3 Word Story [Mini Game]
By Eku in forum Forum LobbyReplies: 90Last Post: 11-16-2008, 09:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks