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

Please write the following program in Java I have attached the the directions in

ID: 3583228 • Letter: P

Question

Please write the following program in Java

I have attached the the directions in pictures. All of the instructions and steps are in the pictures.

Picture 1:

Picture 2:

Picture 3:

Game rules: Like hangman and wheel of Fortune, you are given a display of placeholders for letters, and spaces where there will be spaces. This phrase is retrieved randomly from a file. The user will provide a letter guess. o Guess correctly (and they didn't previously guess that letter), the letter is placed on the display in all placeholders. The user earns 100 points per letter o Guess correctly (but they previously guessed that letter), the user loses 200 points. o Guess incorrectly, the user loses 200 points. Game finished when all placeholders contain the correct letter. Before you start: Create a file on the user profile of the laptops that will be running this name it Phrases.txt Put 4-5 Phrases in it Mine looks like this: File Edit Format View Help Only you can control your future It is better to light a candle than curse the darkness I became insane, with long intervals of horrible sanity We shall never know all the good that a simple smile can do Write and successfully run a program that does the following: 1. Display a dialog box that requests the player's name, and returns that name when you (or press Enter). My dialog box looks like this while the program runs and I've typed in my me: Input Enter your player name: Sharon OK Cancel 2. After the name is entered in the dialog box above, a message box appears displaying the following information: Name of the game A few lines describing the rules Wishing the user luck by name Mine looks like this: (see next page)

Explanation / Answer

Solution:

Java code for the given guessing game problem

package guess;
public class Guess {

   public static final int WrongCost = 200;

   private RandomP selectrand;

   private int chance;

   private HiddenWord hiddenphrase;
   private int[] remain;

   private boolean gameover;


   private boolean inputnext;


   public Guess(RandomP selectrand) {
       this.selectrand = selectrand;
   }


   public void gameBegin(int chance, HiddenWord hiddenphrase) {
       this.chance = chance;
       this.hiddenphrase = hiddenphrase;
       gameover = false;
       remain = new int[2];
       inputnext = true;
   }


   public int putchar(char temchar) {
       remain[chance] -= WrongCost;
       inputnext = true;
       int temp = hiddenphrase.letterCount(temchar);
       if (temp > 0) {
           hiddenphrase.update(temchar);
       } else {
           chance = (chance > 0) ? 0 : 1;
       }
       return temp;
   }


   public String returnresult() {
       return hiddenphrase.returnHiddenphrase();
   }


   public int Balancepoint(int player) {
       return remain[player];
   }

   public String Disp() {
       return hiddenphrase.returnphrse();
   }


   public int returnPharse() {
       return selectrand.getState();
   }


   public int randomletter(char guess) {
       int temp = hiddenphrase.letterCount(guess);
       inputnext = true;
       if (temp > 0) {
           hiddenphrase.update(guess);
           remain[chance] += temp * selectrand.getState();
       } else {
           chance = (chance > 0) ? 0 : 1;
       }
       return temp;
   }

   public boolean randmPhrse(String guess) {
       boolean matches = guess.toUpperCase().equals(hiddenphrase.returnHiddenphrase());
       int otherPlayer = (chance > 0) ? 0 : 1;
       if (matches) {
           remain[otherPlayer] = 0;
           remain[chance] += selectrand.getState() * hiddenphrase.countRemainingConsonants();
           hiddenphrase.updateAllRemaining();
           gameover = true;
       } else {
           chance = otherPlayer;
           inputnext = true;
       }
       return matches;
   }


   public boolean inputnext() {
       return inputnext;
   }


   public boolean gameover() {
       return gameover;
   }


   public void selectnext() {
       selectrand.spin();
       int otherPlayer = (chance > 0) ? 0 : 1;
       if (selectrand.getState() == RandomP.incor) {
           remain[chance] = 0;
           chance = otherPlayer;
       } else if (selectrand.getState() == RandomP.repeat) {
           chance = otherPlayer;
       } else {
           inputnext = false;
       }
   }

   public int chance() {
       return chance;
   }

}

package guessgame;


import java.util.Random;

public class RandomP {

   public static final int incor = -1;

   public static final int repeat = 0;

   private Random ran;

   private int indx;

   public RandomP() {
       ran = new Random();
       indx = incor;
   }


   public RandomP(int seed) {
       ran = new Random(seed);
       indx = incor;
   }


   public int getState() {
       int point = 0;
       switch (indx) {
       default:
           point = 50 * (indx + 6);
           break;
       case 13:
           point = 100;
           break;
       case 14:
           point = repeat;
           break;
       case 15:
           point = incor;
           break;
       }
       return point;
   }


   public void spin() {
indx = ran.nextInt(16);
  
   }
}

//HiddenWord.Java
package guessgame;

public class HiddenWord {


   public static final char letterindx = 42;

   private String inputPhrse;

   private StringBuilder Dispphrase;


   public HiddenWord(String inputPhrse) {
       inputPhrse = inputPhrse.toUpperCase();
       this.inputPhrse = inputPhrse;
       String selectedphrase = "";
       for (int vi = 0; vi < inputPhrse.length(); vi++) {
           char temchar = inputPhrse.charAt(vi);
          
           selectedphrase += (temchar > 64 && 91 > temchar) ? letterindx : temchar;
       }
       Dispphrase = new StringBuilder(selectedphrase);
   }


   public int remainLetter() {
       String wordtodiaply = Dispphrase.toString();
       int num = 0;
       for (int vi = 0; vi < wordtodiaply.length(); vi++) {
          
           num += (wordtodiaply.charAt(vi) == letterindx) ? 1 : 0;
       }
       return num;
   }


   public int countRemainingConsonants() {
       String wordtodiaply = Dispphrase.toString();
       int num = 0;
       for (int vi = 0; vi < wordtodiaply.length(); vi++) {
          
           num += (wordtodiaply.charAt(vi) == letterindx && "BCDFGHJKLMNPQRSTVWXYZ".indexOf(inputPhrse.charAt(vi)) >= 0) ? 1 : 0;
       }
       return num;
   }

   public String returnphrse() {
       return Dispphrase.toString();
   }


   public String returnHiddenphrase() {
       return inputPhrse;
   }

  
   public int letterCount(char temchar) {
       int num = 0;
       for (int vi = 0; vi < inputPhrse.length(); vi++) {
           num += (inputPhrse.charAt(vi) == temchar) ? 1 : 0;
       }
       return num;
   }


   public void update(char temchar) {
       for (int vi = 0; vi < inputPhrse.length(); vi++) {
           if (inputPhrse.charAt(vi) == temchar) {
               Dispphrase.setCharAt(vi, temchar);
           }
       }
   }

   public void updateAllRemaining() {
       Dispphrase = new StringBuilder(inputPhrse);
   }
}


//PhraseReturn.java
package guessgame;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class PhraseReturn {

   private Random generator;

   private ArrayList<String> phrases;


   private ArrayList<Boolean> beenUsed;

   public PhraseReturn(String givenFilename) throws FileNotFoundException {
       File phraseFile = new File(givenFilename);
       generator = new Random();

       Scanner filePhrases = new Scanner(phraseFile);
       phrases = new ArrayList<String>();
       beenUsed = new ArrayList<Boolean>();
       for (; filePhrases.hasNextLine(); phrases.add(filePhrases.nextLine().toUpperCase()), beenUsed.add(false));

       filePhrases.close();
   }

   public String getRandomPhrase() {
       int index = generator.nextInt(phrases.size());
       if (!beenUsed.get(index)) {
           beenUsed.set(index, true);
           return phrases.get(index);
       }
      
       return getRandomPhrase();
   }

}