Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

number guess game

807600Nov 14 2007 — edited Nov 14 2007
Hi i have a basic number guessing game, so far i have made it give the user hints etc if its near the number the computer is thinking off. But i have two more parts to do and i'm struggle abit with it, on how to go about doing them.

1: Game consists of 8 rounds, each round you have 3 guess's to get the number. This bit i think is fine i'm just thinking about adding another loop to what i already have around the 3 guess's, so this will happen 8 times. If thats wrong please point me in the right direction.
So once 8 rounds have been played the program should print out ratings based on how many games were won. etc 8 out of 8 = pro, 7 out of 8 = well done things like that. But i'm not to sure how to go about that. Thinking about a switch statement so if 7 out of 8 display well done message and doing that for everyone but i'm unsure how to store if they one or not????

My second problem is making the program so that it can be played by multiple players, the user will enter how many players want to play then computer has to generate one randomer number for every round. After 8 rounds have been played a message should be printed out which player won most games?? This one i'm not sure at all how to go about doing?

All help very much appreciated.
Cheers Paul

below is the code that i have so far:
package assessment;

import javax.swing.JOptionPane;

public class ASSESSMENTQ2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// win and lose messages
		final String win = "You won";
		final String lose = "Try again";

		// Maximum number of guesses allowed and the range of the numbers between
		final int MAX_GUESS = 3;
		final int RANGE = 10;

		int computersNumber = 0;
		int usersGuess = 0;
		int i = 0;

		String userInput;

		// generate a random number between 1 and 10
		computersNumber = (int) (Math.random() * RANGE) + 1;
		System.out.println(computersNumber);

		// users guess
		for(i = 0; i < MAX_GUESS; i++) {
			
			userInput = JOptionPane.showInputDialog(null, "I am thinking of a number between 1 and 10 " +
					"\n You have 3 attempt to guess what it is:",
					"Guessing game", JOptionPane.INFORMATION_MESSAGE);

			// convert users input to a integer
			usersGuess = Integer.parseInt(userInput);
			
			


			// call user defined method
			boolean answer = checkAnswer(computersNumber, usersGuess);
			 giveHint(computersNumber, usersGuess);

			if(answer == true) {
				JOptionPane.showMessageDialog(null, win, "Result",
						JOptionPane.WARNING_MESSAGE);
				System.exit(0);
			}
			if(answer == false) {
				JOptionPane.showMessageDialog(null, lose, "Result",
						JOptionPane.ERROR_MESSAGE);

			}
		}
		JOptionPane.showMessageDialog(null, "Winning number was " + computersNumber,
				"Guessing game", JOptionPane.INFORMATION_MESSAGE);

		JOptionPane.showMessageDialog(null, "Sorry you didn't win this time",
				"Guessing game", JOptionPane.INFORMATION_MESSAGE);

	}

	// user defined method - check answer
	public static boolean checkAnswer(int computersNumber, int usersGuess) {

		if(computersNumber == usersGuess) {
			return true;
		} else {
			return false;
		}
	}

	// user defined method - give user a hint
	public static void giveHint(int computersNumber, int usersGuess) {

		if(usersGuess + 3 == computersNumber || usersGuess - 3 == computersNumber) {
			JOptionPane.showMessageDialog(null, "Hint - your guess was cold",
					"Guessing game", JOptionPane.INFORMATION_MESSAGE);
		} else if(usersGuess + 2 == computersNumber || usersGuess - 2 == computersNumber) {
			JOptionPane.showMessageDialog(null, "Hint - your guess was warm",
					"Guessing game", JOptionPane.INFORMATION_MESSAGE);
		} else if(usersGuess + 1 == computersNumber || usersGuess - 1 == computersNumber) {
			JOptionPane.showMessageDialog(null, "Hint - your guess was hot",
					"Guessing game", JOptionPane.INFORMATION_MESSAGE);
		}
		

	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 12 2007
Added on Nov 14 2007
7 comments
935 views