Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.concurrent.ThreadLocalRandom;
  3.  
  4. public class JumbleGame {
  5.  
  6.     private static final String[] WORDS = new String[] {
  7.         "magical","quarter","security","phrase","xylophone","hydrogen"
  8.     };
  9.    
  10.     public static void main(String[] args) {
  11.         JumbleGame jg = new JumbleGame();
  12.         jg.startGame();
  13.     }
  14.  
  15.     private void startGame() {
  16.         int numberOfGuesses = 0;
  17.         String original = selectRandomWord();
  18.         String shuffled = getShuffledWord(original);
  19.         boolean gameOn = true;
  20.  
  21.         while(gameOn) {
  22.             System.out.println("Shuffled word is: "+shuffled);
  23.             numberOfGuesses++;
  24.             if(numberOfGuesses == 7) {
  25.             System.out.println("HINT: Here are the first two letters in order: " + s1.charAt(0) + s1.charAt(1));
  26.             }
  27.  
  28.             if(numberOfGuesses == 10) {
  29.             System.out.println("Game Over! Try again next time!");
  30.             System.exit(0);
  31.             }
  32.  
  33.             String userGuess = getUserGuess();
  34.  
  35.             if(original.equalsIgnoreCase(userGuess)) {
  36.                 System.out.println("Congratulations! You found the word in "+numberOfGuesses+" guesses");
  37.                 gameOn = false;
  38.             }
  39.             else {
  40.                 System.out.println("Sorry, Wrong answer");
  41.             }
  42.         }        
  43.     }
  44.    
  45.     public String getUserGuess() {
  46.         Scanner sn = new Scanner(System.in);
  47.         System.out.println("Please type in the original word: ");
  48.         return sn.nextLine();
  49.     }
  50.    
  51.     public String selectRandomWord() {
  52.         int rPos = ThreadLocalRandom.current().nextInt(0, WORDS.length);
  53.         return WORDS[rPos];
  54.     }
  55.  
  56.     public String getShuffledWord(String original) {
  57.         String shuffledWord = original;
  58.         int wordSize = original.length();
  59.         int shuffleCount = 10;
  60.  
  61.         for(int i=0;i<shuffleCount;i++) {
  62.            
  63.             int position1 = ThreadLocalRandom.current().nextInt(0, wordSize);
  64.             int position2 = ThreadLocalRandom.current().nextInt(0, wordSize);
  65.             shuffledWord = swapCharacters(shuffledWord,position1,position2);
  66.         }
  67.         return shuffledWord;
  68.     }
  69.  
  70.     private String swapCharacters(String shuffledWord, int position1, int position2) {
  71.         char[] charArray = shuffledWord.toCharArray();
  72.         char temp = charArray[position1];
  73.         charArray[position1] = charArray[position2];
  74.         charArray[position2] = temp;
  75.         return new String(charArray);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement