manish

Word Scramble Game by manish

Sep 1st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class WordScramble {
  4.  
  5.     static String words[][] = {{"EXPERIMENT", "PRESSURE", "ECLIPSE", "TEMPERATURE", "TELESCOPE", "INTERNET", "WINDOW", "RELEASE", "EXPRESSION", "REFLECT"}, {"INTELLIGENT", "MIGRATION", "ENVIRONMENT", "CONTINENT", "PLATINUM", "INTERNATIONAL", "RENOVATION", "ACCELERATION", "ABSTRACT", "CIVILIZATION"}, {"ASTEROID", "MICROSCOPE", "METAMORPHOSIS", "ELECTRONICS", "EFFORTLESSLY", "COMMANDER", "COORDINATION", "SOPHISTICATE", "UNRESTRICTED", "URBANIZATION"}};
  6.     static int rounds = 0;
  7.  
  8.     public static void main(String s[]) {
  9.         System.out.println(" Get ready to play Word Scramble. You will play 3 rounds - Easy, Medium and Hard. ");
  10.         System.out.println("Enter anything to continue.");
  11.         String x = new Scanner(System.in).nextLine();
  12.         play();
  13.     }
  14.  
  15.     static void play() {
  16.         if (rounds == 3) {
  17.             System.out.println("You have successfully won the game.");
  18.         } else {
  19.             String word = words[rounds][(int) (Math.random() * 10)];
  20.             System.out.println("Round " + (rounds + 1) + ": " + scramble(word));
  21.             System.out.println("You have only 1 chance to answer!");
  22.             String y = new Scanner(System.in).nextLine();
  23.  
  24.             if (y.compareToIgnoreCase(word) == 0) {
  25.                 System.out.println(" Congratulations! You have given the correct answer: " + word);
  26.                 System.out.println("Enter anything to continue.");
  27.                 String z = new Scanner(System.in).nextLine();
  28.                 rounds++;
  29.                 play();
  30.             } else {
  31.                 System.out.println(" You got the word wrong! The correct word was: " + word + ". You have lost the game.");
  32.             }
  33.         }
  34.     }
  35.  
  36.     static String scramble(String x) {
  37.         String y = "";
  38.         while (x.compareTo("") != 0) {
  39.             int n = x.length();
  40.             String ch = "" + x.charAt((int) (Math.random() * n));
  41.             y += ch;
  42.             x = x.replaceFirst(ch, "");
  43.         }
  44.         return y;
  45.     }
  46. }
Add Comment
Please, Sign In to add comment