please can someone help me with this HW
the instructions are :
# The computer should ask the player for the largest possible value which could be generated (N)

# The maximum number of guesses should be set equal to 1/2 N. (Don't prompt the user).

# When the game starts, the top 5 high scores should be displayed.

* At the beginning of a round, the high scores will be read from a file into an array
o You can decide where this file is to be stored. You DON'T need to prompt the user to find it.
o Tip: If you wish to use relative paths for your file location... it can be tricky.
* The High-Scores should be displayed in descending order.
* The high-score file just contains a series of integers.
o For simplicity, no other information will be stored.... just the scores themselves.
o (See the sample file below)


# The player should be asked to guess a number between 1 and N.

* The user should receive feedback pertaining to whether their guess is too high or too low.


# The player should be able to enter guesses until:

* The correct number is guessed
* The guess limit is reached
* The user enters a negative number to quit the round.


# Once a round is quit, the player should be prompted to play another round.

# Once a round ends:

* The player should be shown their score using the following formula:
o Score = (Maximum Value) / (Number of Guesses to get Correct)
o Score = 0 if the user quits before getting the correct answer or exceeds the maximum number of guesses.
o Round the score to the nearest integer
* If the players score is higher than the lowest score in the "high-score" file:
o A new high-score file should be written over the old one.


# The user should be prompted to play again.

* The should be able to answer in upper or lower case
* The continued game should use the same upper bound and max guesses as entered at the beginning.
* Before staring the each round, the current high scores should
be displayed.


Here wat i have done so far:

import java.io.*;
import java.util.*;


public class trial{
  
  public static void main(String[] args)  throws FileNotFoundException {
    Scanner input = new Scanner(new File("highscores.txt"));   
    Scanner console = new Scanner(System.in);
   System.out.println(" Game Setup:");
     System.out.println(" I will pick a random number between 1 and N");
      System.out.println(" Please enter a value for N: ");
      int N= console.nextInt();
      System.out.println();
      
      readScores(input);
    
 
      Random r= new Random();
      int result = 0;
      String ans;
     
      int maxGuesses = N/2;
      System.out.println();

     
     System.out.println("I'm thinking of a random Number between 1 and " + N + ".");
    
     System.out.print("You have " +  maxGuesses + " chances to guess the number.");
     System.out.println(" Enter -1 at anytime to quit.");
      int guessNum = 0;
     for(int i= 1; i <= maxGuesses; i++) {
       System.out.println("Enter guess number " + i + " :");
       guessNum= console.nextInt();
        result = r.nextInt(N);
       if(guessNum < result) {
         System.out.println("Your guess is too low.");
       } else if(guessNum >result){
         System.out.println("Your guess is too high.");
       } if(guessNum == result) {
         System.out.println("Congratulations, you guessed the correct value in " + i + " tries!");
          int score = N/i;
         System.out.println("Your score is " + score + " points. You did not beat a high score.");
         PrintStream output = new PrintStream(new File("Highscores.txt"));
         output.println(score);
         break;
         
       }
     }
     
     if (guessNum != result)  {
         System.out.println("Sorry, you failed to guess the correct number!");
    }
  }

  
  public static void readScores(Scanner input) {
     System.out.println("Highscores: ");   
    while(input.hasNextInt()) {
      int next = input.nextInt();
      System.out.println(next);       
    }
  }
}

Recommended Answers

All 5 Replies

First please help us try to help you by :-

  1. Putting your code inside [code=java]and [/code] tags, that way your formatting, Indentation (if any) is maintained and it also provides syntax highlighting so the code becomes more readable. you can read more about them here.
  2. Next tell us what you have achieved so far, you have pasted the code, which is OK, but tell us what output it is giving (or is there a compile error or a runtime exception occurring along with whatever the compiler or the JRE is throwing up at you) and what exactly it should achieve with sample output preferably so that we do not have to waste our brain cycles for these rudimentary things
import java.io.*;
import java.util.*;


public class trial{
  
  public static void main(String[] args)  throws FileNotFoundException {
    Scanner input = new Scanner(new File("highscores.txt"));   
    Scanner console = new Scanner(System.in);
   System.out.println(" Game Setup:");
     System.out.println(" I will pick a random number between 1 and N");
      System.out.println(" Please enter a value for N: ");
      int N= console.nextInt();
      System.out.println();
      
      readScores(input);
    
 
      Random r= new Random();
      int result = 0;
      String ans;
     
      int maxGuesses = N/2;
      System.out.println();

     
     System.out.println("I'm thinking of a random Number between 1 and " + N + ".");
    
     System.out.print("You have " +  maxGuesses + " chances to guess the number.");
     System.out.println(" Enter -1 at anytime to quit.");
      int guessNum = 0;
     for(int i= 1; i <= maxGuesses; i++) {
       System.out.println("Enter guess number " + i + " :");
       guessNum= console.nextInt();
        result = r.nextInt(N);
       if(guessNum < result) {
         System.out.println("Your guess is too low.");
       } else if(guessNum >result){
         System.out.println("Your guess is too high.");
       } if(guessNum == result) {
         System.out.println("Congratulations, you guessed the correct value in " + i + " tries!");
          int score = N/i;
         System.out.println("Your score is " + score + " points. You did not beat a high score.");
         PrintStream output = new PrintStream(new File("Highscores.txt"));
         output.println(score);
         break;
         
       }
     }
     
     if (guessNum != result)  {
         System.out.println("Sorry, you failed to guess the correct number!");
    }
  }

  
  public static void readScores(Scanner input) {
     System.out.println("Highscores: ");   
    while(input.hasNextInt()) {
      int next = input.nextInt();
      System.out.println(next);       
    }
  }
}

and here is an example of wat im tryin to get it to do :

Initial Input:

Game Setup:

I will pick a random number between 1 and N.
Please enter a value for N: 100

Game Play Input/Output:

High Scores:
15
10
10
5
3

I am thinking of a random number between 1 and 100.
Enter -1 at any time to quit.

Enter guess number 1: 50
Your guess is too high.

Enter guess number 2: 10
Your guess is too low.
(... The game continues ... )

Enter guess number 50: 45

Congratulations, you guessed the correct value in 50 tries!
You scored is 2 points. You did not beat a high score.

Would you like to play again (Y/N)? Y

Yes but again where is it that you are getting stuck up ? Don't expect us to form the logic for you since you know what's to be done, you should be able to deduce a correct logic for that and write the code for it, then if you are getting stuck at any particular place ask us specific questions about it.

First of all you need a class to represent each 'game'. The class would contain the following data:

int correctNumber;
int guessesAllowed;
int guessesLeft;

You might need a few more things. You also need to split each task into methods. For example, prompting the user for their guesses could be one method; generating the random number that you want them to guess could be another method; etc

yea i have a hard time breaking things up into methods. can someone please help me out with that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.