word scramble complete

Word scrambler completed, from :  Do-It-Yourself Java Games, by Annette Godtland.

I haven't come up with something I want to alter about this one.


package nellie.tobey.wordscramble;
import java.util.Random;
import nellie.tobey.mywindow.MyWindow;
public class WordScramble extends MyWindow {
 public WordScramble() {
  String words[] = {"ANIMALS", "ELEPHANT", "GIRAFFE",
    "PENGUIN", "GORILLA", "NEWT", "LEMMMING", "RED HARRING"
  };
  int numberofWords = words.length;
  //create a loop that scrambles all the words on the list
  for(int i= numberofWords; i > 0; i--) {
 
  String scrambled = scramble(words[i - 1]);
  print(scrambled);
 
 }}
    private String scramble(String word) {
     String scrambled = "";
     //repeat these steps until original word is gone
     while(word.length()> 0) {
     //pick random letter from word
     Random rand = new Random();
     int length = word.length();
     int index = rand.nextInt(length);
     String letter = word.substring(index, index + 1);
     //remove letter from original word
     String firstString = word.substring(0, index);
     String secondString = word.substring(index + 1);
     word = firstString + secondString;
     //add that letter to a new word
     scrambled += letter;
     //repeat until all letters removed from original
   
     //System.out.println("index = " + index);
     //System.out.println("letter = " + letter);
     //System.out.println("word = " + word);
     //System.out.println("Scrambled = " + scrambled);
     }
     return scrambled;
    }
 public static void main(String[] args) {
  new WordScramble();
 }
}

Comments

Popular posts from this blog

Statistics, Python, Making a Stem Plot

Pandas Python and a little probability

JavaScript Ascii animation with while loops and console.log