manish icon

Word Scramble Game by manish

manish | PRO | 09/01/17 09:57:37 AM UTC | 0 ⭐ | 305 👁️ | Never ⏰ | []
Java |

2.04 KB

|

None

|

0 👍

/

0 👎

import java.util.*;
 
public class WordScramble {
 
    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"}};
    static int rounds = 0;
 
    public static void main(String s[]) {
        System.out.println(" Get ready to play Word Scramble. You will play 3 rounds - Easy, Medium and Hard. ");
        System.out.println("Enter anything to continue.");
        String x = new Scanner(System.in).nextLine();
        play();
    }
 
    static void play() {
        if (rounds == 3) {
            System.out.println("You have successfully won the game.");
        } else {
            String word = words[rounds][(int) (Math.random() * 10)];
            System.out.println("Round " + (rounds + 1) + ": " + scramble(word));
            System.out.println("You have only 1 chance to answer!");
            String y = new Scanner(System.in).nextLine();
 
            if (y.compareToIgnoreCase(word) == 0) {
                System.out.println(" Congratulations! You have given the correct answer: " + word);
                System.out.println("Enter anything to continue.");
                String z = new Scanner(System.in).nextLine();
                rounds++;
                play();
            } else {
                System.out.println(" You got the word wrong! The correct word was: " + word + ". You have lost the game.");
            }
        }
    }
 
    static String scramble(String x) {
        String y = "";
        while (x.compareTo("") != 0) {
            int n = x.length();
            String ch = "" + x.charAt((int) (Math.random() * n));
            y += ch;
            x = x.replaceFirst(ch, "");
        }
        return y;
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎