package cse1030; import java.util.Random; /** * Word puzzle where the puzzle word is a randomly scrambled version of the * solution word. For example, if the solution word * is "hello" then a puzzle word might be "lohel". * * @author CSE1030Z * */ public class ScrambleWordPuzzle extends AbstractWordPuzzle { private String scramble; /** * Create a scrambled word puzzle given the solution word. * * @param word the solution word */ public ScrambleWordPuzzle(String word) { super(word); Random rng = new Random(); StringBuilder b = new StringBuilder(word); StringBuilder s = new StringBuilder(); for (int i = 0; i < word.length(); i++) { int j = rng.nextInt(b.length()); s.append(b.charAt(j)); b.deleteCharAt(j); } this.scramble = s.toString(); } /** * Get the puzzle word. The puzzle word is a randomly scrambled version of the * solution word. Note that calling this method repeatedly always * returns the same puzzle word (i.e., the solution word is not randomly * scrambled each time this method is called). * * @see cse1030.AbstractWordPuzzle#getPuzzleWord() * * @return the puzzle word */ @Override public String getPuzzleWord() { return this.scramble; } }