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
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 its 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 players 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
//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(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
