Hi, I have a homework problem where i have to make a guessing game that will che
ID: 3913164 • Letter: H
Question
Hi, I have a homework problem where i have to make a guessing game that will chekc and see if the score the player gets, which means whoever can get the secret number in the lowest number of guesses gets the high score. The objective is to print the score and name of the high score holder, and then make sure that if someone was to guess above that number they would not get the high score, but if someone was to guess below the high score they would get the high score and have there name and score printed on the file "highScore.txt". I am currently getting a logic error and am confused on why my program is not working. Thanks for the help!
Here is my code:
import java.util.*;
import java.io.*;
public class GuessingGame2{
Scanner scan;
int secret;
int guess;
boolean done;
boolean won;
int currentScore;
int score = 0;
PrintWriter pw;
BufferedReader br;
BufferedReader betterScan = new BufferedReader(new InputStreamReader(System.in));
String name;
String highScore;
public GuessingGame2(){
playGame();
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
/*public boolean isNewHighScore(int score){
if (currentScore > score){
return true;
}else{
return false;
}
// get score from file
//if score is higher than from file
//set new high score (with name?)
}*/
public void setNewScore(int score){
try{
pw = new PrintWriter(new FileWriter(
new File("highScore.txt")));
pw.print(score);
pw.print(name);
pw.close();
}catch(FileNotFoundException fnfe){
System.out.println(fnfe.getMessage());
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
public void getNewScore(){
try{
BufferedReader br = new BufferedReader(new FileReader(
new File("highScore.txt")));
highScore = br.readLine();
br.close();
}catch(FileNotFoundException fnfe){
System.out.println(fnfe.getMessage());
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
public void playGame(){
do{
reset();
currentScore = 0;
secret = (int)(Math.random()*100 + 1);
do{
System.out.println("Guess a number (1-100)");
guess = scan.nextInt();
scan.nextLine();
if(secret == guess){
won = true;
}else if (secret > guess){
System.out.println("Too low. Try again.");
}else{
System.out.println("Too high. Try again.");
}
currentScore += 1;
}while(!won);
System.out.println("Congratulations! You won!");
try{
if (currentScore < score){
System.out.println("Sorry, you did not beat the current high score of " + highScore + " by " + getName());
}else{
setNewScore(currentScore);
System.out.println("Congrats you hold the new high score of " + currentScore);
System.out.println("Please enter your name: ");
name = betterScan.readLine();
}
}catch(FileNotFoundException fnfe){
System.out.println(fnfe.getMessage());
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
System.out.println("Play again? (yes or no)");
String response = scan.nextLine();
done = response.toLowerCase().startsWith("y");
}while(done);
}
public void reset(){
scan = new Scanner(System.in);
secret = 0;
guess = 0;
done = false;
won = false;
}
public static void main(String[] args){
GuessingGame2 gg = new GuessingGame2();
}
}
Explanation / Answer
If you have any doubts, pelase give me comment...
import java.util.*;
import java.io.*;
public class GuessingGame2 {
Scanner scan;
int secret;
int guess;
boolean done;
boolean won;
int currentScore;
int score = 0;
PrintWriter pw;
BufferedReader br;
BufferedReader betterScan = new BufferedReader(new InputStreamReader(System.in));
String highScore_name;
int highScore;
String name;
public GuessingGame2() {
playGame();
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
/*
* public boolean isNewHighScore(int score){
*
* if (currentScore > score){
*
* return true;
*
* }else{
*
* return false;
*
* }
*
* // get score from file
*
* //if score is higher than from file
*
* //set new high score (with name?)
*
* }
*/
public void setNewScore(int score) {
try {
pw = new PrintWriter(new FileWriter(
new File("highScore.txt")));
pw.print(score+" ");
pw.print(getName());
pw.close();
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
public void getNewScore() {
try {
Scanner br = new Scanner(new File("highScore.txt"));
highScore = br.nextInt();
highScore_name = br.nextLine();
br.close();
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
}
}
public void playGame(){
scan = new Scanner(System.in);
System.out.print("Enter player name: ");
String name = scan.next();
setName(name);
do {
reset();
currentScore = 0;
secret = (int) (Math.random() * 100 + 1);
do {
System.out.println("Guess a number (1-100)");
guess = scan.nextInt();
scan.nextLine();
if (secret == guess) {
won = true;
} else if (secret > guess) {
System.out.println("Too low. Try again.");
} else {
System.out.println("Too high. Try again.");
}
currentScore += 1;
} while (!won);
System.out.println("Congratulations! You won!");
try {
if (currentScore < score) {
System.out.println(
"Sorry, you did not beat the current high score of " + highScore + " by " + highScore_name);
} else {
setNewScore(currentScore);
System.out.println("Congrats you hold the new high score of " + currentScore);
System.out.println("Please enter your name: ");
name = betterScan.readLine();
}
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
System.out.println("Play again? (yes or no)");
String response = scan.nextLine();
done = response.toLowerCase().startsWith("y");
} while (done);
}
public void reset() {
scan = new Scanner(System.in);
secret = 0;
guess = 0;
done = false;
won = false;
}
public static void main(String[] args) {
GuessingGame2 gg = new GuessingGame2();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.