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

Write a program in java that follows the following requirements Please read ever

ID: 3695584 • Letter: W

Question

Write a program in java that follows the following requirements Please read everything. Provide Output as well to your file so I can see that it is working correctly. I have code provided below and the only thing this program doesn't do is right the score of the top 5 players to a score text file. but it does read from a data text file.

Requirements:

It must randomly choose a word from a list of words. All words must be read from a data text file which contains at least 15 words. Each word includes at least 8 letters because it’s relatively easier to guess.

It must stop when all the letters are guessed. Program will ask player whether to keep going or quit.

It must give them limited tries and stop after they run out. (20 tries limitation)

It must display letters they have already guessed (either only the incorrect guesses or all guesses).

It must record player’s score and write the score into the score text file before user quit the game if the score belongs to the top 5 scores. That means the score file will store the 5 highest scores and the irrelative player names. At the beginning of the game, the top 5 scores list will be shown on the screen.

Score rules: 3 points for no missed letter,

2 points for 4 or less than 4 missed letters,

1 point for equal to or less than 7 tries.

Strategy:

(1) Define some array variables (or use ArrayList better) to contain word list, top five scores, and top five names.

(2) Define some String variables to contain guessed letters. Use string methods to print out every step guess.

(3) Define a variable to contain the score.

(4) At the beginning, program will read data file and score file and show score file content on the screen.

(5) Before program quits, it will compare the score player got to the top 5 scores. If the score is bigger than the fifth highest record, the top 5 records will be updated and the score file will be updated as well.

Hint:You may need to compare two string, but if they are not same type, a compare function can be created, in which program can compare each element and return a boolean value.

Hint: For input, Scanner can be used, but when program needs to read data from file, and when to read data from keyboard, the parameters are different. For example: Read data from file: File dataFile = new File(“data.txt”); Scanner input = new Scanner(dataFile); Read data from keyboard: Scanner input = new Scanner(System.in);

Hint: For output, Form atter can be used. For example: Formatter output = new Formatter(“score.txt”); output.format(“%d%d%s ", i+1, top5ScoreList.get(i),top5NameList.get(i)); System.out.printf(“%d%d%s ", i+1, top5ScoreList.get(i),top5NameList.get(i));

My Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class WordGuess {
public static void main(String[] args) {
File dataFile = new File("guesswords.txt");

ArrayList<String> wordsFromFile = new ArrayList<> ();
String[] guessWord = null;
ArrayList<String> miss = new ArrayList<>();
String secretWord = null;
//ArrayList<String> topScores = new ArrayList<String> ();
//ArrayList<String> topNames = new ArrayList<String> ();

int score = 0;
int attempts = 0;
int exit=0;
try {

Scanner input = new Scanner(dataFile);
//read data file
while(input.hasNextLine()) {
    wordsFromFile.add(input.nextLine().trim());
}
input.close();
//choose the secret word
Random rand = new Random();
int index = rand.nextInt(wordsFromFile.size());

secretWord = wordsFromFile.get(index);
guessWord = new String[secretWord.length()];
for(int i = 0; i < secretWord.length(); i++) {
guessWord[i] = "_";
}

//Read data from keyboard:
Scanner sc = new Scanner(System.in);
int misses = 0;
String charStr = null;
String name=null;
int correct = 0;
do{
System.out.println("Word:");
for(int i = 0; i < guessWord.length; i++)
System.out.print(guessWord[i]+" ");

System.out.println(" Misses:"+miss);
System.out.println("Misses:"+misses);
System.out.println("Guess:");
charStr = sc.nextLine();

if(charStr.length() == 1) {
index = -1;
index = secretWord.indexOf(charStr.trim().charAt(0),index+1);
if(index == -1){
misses++;
miss.add(charStr.trim());
}
else{
do{
correct++;
guessWord[index] = charStr.trim();
index = secretWord.indexOf(charStr.trim().charAt(0),index+1);
}while(index != -1);
}
attempts++;
}

//validating
if(correct>=secretWord.length()){
int flag=0;
for(int j=0; j < guessWord.length; j++){
char c=secretWord.charAt(j);
char f=guessWord[j].charAt(0);
//System.out.println(f);
//System.out.println(c);
if(c!=f)
flag=1;
}
if(flag==0)
exit=1;
}

}while(attempts <= 20 && exit!=1);

if(exit==1){ //you win
System.out.println(" YOU GOT IT:");
if(misses<=7) {
score=1;
}
if(misses<=4) {
score=2;
}
if (misses==0) {
score=3;
}
if (attempts<=7) {
score=score+1;
}
System.out.println("Your Name?:");
name = sc.nextLine();
System.out.println("Winner:"+name);
System.out.println("Score:"+score);
}
sc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class WordGuess {
   public static void main(String[] args) {
       File dataFile = new File("guesswords.txt");
       Scanner scoreFile = null;
       try {
           scoreFile = new Scanner(new File("Scores.txt"));
       } catch (FileNotFoundException e1) {
           e1.printStackTrace();
       }
       ArrayList<Integer> highest = new ArrayList<Integer>();
       while(scoreFile.hasNext()){
           highest.add(scoreFile.nextInt());
       }
       System.out.print("Highest scores: ");
       for(int i = 0; i < highest.size(); i++){
           if(i >= 5) break;
           System.out.print(highest.get(i) + " ");
       }
       System.out.println();
       ArrayList<String> wordsFromFile = new ArrayList<> ();
       String[] guessWord = null;
       ArrayList<String> miss = new ArrayList<>();
       String secretWord = null;
       //ArrayList<String> topScores = new ArrayList<String> ();
       //ArrayList<String> topNames = new ArrayList<String> ();

       int score = 0;
       int attempts = 0;
       int exit=0;
       try {

           Scanner input = new Scanner(dataFile);
           //read data file
           while(input.hasNextLine()) {
               wordsFromFile.add(input.nextLine().trim());
           }
           input.close();
           //choose the secret word
           Random rand = new Random();
           int index = rand.nextInt(wordsFromFile.size());

           secretWord = wordsFromFile.get(index);
           guessWord = new String[secretWord.length()];
           for(int i = 0; i < secretWord.length(); i++) {
               guessWord[i] = "_";
           }

           //Read data from keyboard:
           Scanner sc = new Scanner(System.in);
           int misses = 0;
           String charStr = null;
           String name=null;
           int correct = 0;
           do{
               System.out.println("Word:");
               for(int i = 0; i < guessWord.length; i++)
                   System.out.print(guessWord[i]+" ");

               System.out.println(" Misses:"+miss);
               System.out.println("Misses:"+misses);
               System.out.println("Guess:");
               charStr = sc.nextLine();

               if(charStr.length() == 1) {
                   index = -1;
                   index = secretWord.indexOf(charStr.trim().charAt(0),index+1);
                   if(index == -1){
                       misses++;
                       miss.add(charStr.trim());
                   }
                   else{
                       do{
                           correct++;
                           guessWord[index] = charStr.trim();
                           index = secretWord.indexOf(charStr.trim().charAt(0),index+1);
                       }while(index != -1);
                   }
                   attempts++;
               }
               //validating
               if(correct>=secretWord.length()){
                   int flag=0;
                   for(int j=0; j < guessWord.length; j++){
                       char c=secretWord.charAt(j);
                       char f=guessWord[j].charAt(0);
                       //System.out.println(f);
                       //System.out.println(c);
                       if(c!=f)
                           flag=1;
                   }
                   if(flag==0)
                       exit=1;
               }
           }while(attempts <= 20 && exit!=1);
           if(exit==1){ //you win
               System.out.println(" YOU GOT IT:");
               if(misses<=7) {
                   score=1;
               }
               if(misses<=4) {
                   score=2;
               }
               if (misses==0) {
                   score=3;
               }
               if (attempts<=7) {
                   score=score+1;
               }
               System.out.println("Your Name?:");
               name = sc.nextLine();
               System.out.println("Winner:"+name);
               System.out.println("Score:"+score);
           }
           sc.close();
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       if(highest.size() < 5){
           highest.add(score);
       }
       else{
           if(highest.get(4) < score){
               highest.remove(4);
               highest.add(score);
           }
       }
       Collections.sort(highest);
       FileWriter fWriter = null;
       try {
           fWriter= new FileWriter(new File("Scores.txt"));
       } catch (IOException e) {
           e.printStackTrace();
       }
       for(int i = 0; i < highest.size(); i++){
           try {
               fWriter.write(highest.get(i) + " ");
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
}

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