Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, Write an application called Scramble that has a GUI to play a game of w

ID: 3820878 • Letter: I

Question

In Java,

Write an application called Scramble that has a GUI to play a game of word anagrams. Create two arrays of strings. The first array will hold words, and the second will hold scrambled versions of those words. Your Java code can initialize these arrays directly with the words. Display the scrambled version of the word in a label. The user will enter a guess for the word in a text field and press a Check button. You should see whether the guess is correct. If it is not correct, change the guess in the text field to Sorry, that is incorrect. Please try again. If the guess is correct, change it to That is correct. Here is a new word to try and display a new scramble. Also provide a Give Up button. If it is pressed, display the unscrambled word and provide a new scrambled word.

Explanation / Answer

package pankhu;

import java.util.Scanner;

   import java.util.concurrent.ThreadLocalRandom;
import javax.swing.*;
  

  

   public class ai {

  

        private static final String[] WORDS_DATABASE = new String[] {

            "superman","jungle","programmer","letter","house","helium","parrot"
    };

       

        public static void main(String[] args) {

            ai jg = new ai();

            jg.startGame();

        }


        private void startGame() {

            int numberOfGuesses = 0;

            String original = selectRandomWord();
            String shuffled = getShuffledWord(original);

            boolean gameOn = true;

            while(gameOn) {

                System.out.println("Shuffled word is: "+shuffled);

                numberOfGuesses++;

                String userGuess = getUserGuess();

                if(original.equalsIgnoreCase(userGuess)) {

                    System.out.println("Congratulations! You found the word in "+numberOfGuesses+" guesses");

                    gameOn = false;

                }else {

                    System.out.println("Sorry, Wrong answer");

                }

            }     

        }

       
      
        public String getUserGuess() {

            Scanner sn = new Scanner(System.in);

            System.out.println("Please type in the original word: ");

            return sn.nextLine();

        }

        public String selectRandomWord() {

            int rPos = ThreadLocalRandom.current().nextInt(0, WORDS_DATABASE.length);

            return WORDS_DATABASE[rPos];

        }

       
    public String getShuffledWord(String original) {

            String shuffledWord = original;

            int wordSize = original.length();

            int shuffleCount = 10;

            for(int i=0;i<shuffleCount;i++) {

              
                int position1 = ThreadLocalRandom.current().nextInt(0, wordSize);

                int position2 = ThreadLocalRandom.current().nextInt(0, wordSize);

                shuffledWord = swapCharacters(shuffledWord,position1,position2);

            }

            return shuffledWord;

    }
        private String swapCharacters(String shuffledWord, int position1, int position2) {

            char[] charArray = shuffledWord.toCharArray();

         

            char temp = charArray[position1];

            charArray[position1] = charArray[position2];

            charArray[position2] = temp;

            return new String(charArray);

        }

   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote