This is my java assignment for school and I have done most of the work. But I still have some other features I have problems with adding. This is a guessing game where to program requests a number from the user between 1-1000.
It will tell the user if the user's guess was too high or too low. And when the user entered the correct guess the program will add the result to the high score list and ask the user if he wants to play again. The high score list consists of name, nr of guesses and how long time it took for the user to guess the correct number.
The following features are:
1. I want it to say "Stupid Guess" if you guess a number greater than 1000 or less than 0 and the program shouldn't count it as a guess.
2. I want it to say "Stupid Guess" if you guess a non-numeric value 0 and the program shouldn't count it as a guess.
3. The user should be able to input 'quit' and the program should stop.
Could you please give me some hints? :)
package game;
import java.util.*;
public class Game
{
public void start()
{
Scanner in = new Scanner(System.in);
Random generator = new Random();
String ch;
int numberOfGuesses;
String name;
int[] guesses = new int[1000];
String names[] = new String[1000];
double times[] = new double[1000];
int counter = 0;
double start;
double end;
int num = 0;
String n = "";
do
{
if(counter > 0)
{
int i, j;
for(i = 0;i < counter; i++)
for(j = 0; j <counter-1; j++)
{
if(guesses[j] > guesses[j+1])
{
int temp = guesses[j];
guesses[j] = guesses[j+1];
guesses[j+1] = temp;
/////////////////////
String tempName = names[j];
names[j] = names[j+1];
names[j+1] = tempName;
}
if(guesses[j] == guesses[j+1] && times[j] > times[j+1])
{
int temp = guesses[j];
guesses[j] = guesses[j+1];
guesses[j+1] = temp;
/////////////////////
String tempName = names[j];
names[j] = names[j+1];
names[j+1] = tempName;
/////////////////////
double time = times[j];
times[j] = times[j+1];
times[j+1] = time;
}
}
System.out.println("Current highscore list: ");
System.out.println("Name Guesses");
for(i=0; i < counter; i++)
{
System.out.print(names[i]+" ");
System.out.println(guesses[i]);
System.out.println("Time: "+times[i] / 1000);
}
}
int randomIndex = generator.nextInt( 1001);
numberOfGuesses = 0;
int target = randomIndex + 1;
start = System.currentTimeMillis();
do
{
System.out.print("Please enter your guess(1-1000): ");
n = in.next();
if(isIntNumber(n))
num = Integer.parseInt(n);
}
while(!isIntNumber(n));
while(target != num)
{
if(target < num)
System.out.println("Too high");
else if(target > num)
System.out.println("Too low");
numberOfGuesses++;
System.out.println("Please enter your guess");
num = in.nextInt();
}
numberOfGuesses++;
System.out.println("Right ... the number was "+target);
end = System.currentTimeMillis();
System.out.print("Please enter you name: ");
name = in.next();
guesses[counter] = numberOfGuesses;
names[counter] = name;
times[counter] = end-start;
System.out.print("Do you wish to play again ? (yes/no)");
ch = in.next();
counter++;
}
while(ch.equalsIgnoreCase("yes"));
}
public boolean isIntNumber(String num){
try{
Integer.parseInt(num);
} catch(NumberFormatException nfe) {
return false;
}
return true;
}
public static void main(String[] args)
{
Game game=new Game();
game.start();
}
}