Sunday, 26 May 2013

JAVA Guessing Game

This post will take a look at a "Guessing Game" app I've created using JAVA. There are many ways of accomplishing this but this is one working method I've come up with after taking a few basic JAVA lessons. I though I might share in the hopes that someone like myself may find it useful.

The requirements of this program would be:
  1. A random number is chosen (between 1 and 100)
  2. User is greeted with a message and instructions
  3. User is then prompted to input a 'guess' (an integer)
  4. 'guess' is checked the random number
  5. User is instructed weather the guess was too high, too low or correct
  6. If the guess is not correct, repeat steps 3 to 5 until the guess is correct
  7. Once the guess is correct, tell the user they've guessed correctly and exit program

Since I'm new to Blogger and am yet to find a nice way of formatting my text here, I'm going to paste the entire program here first and then discuss parts of interest below so, without further a-do:

 /* A simple number guessing game  
  * call this GuessGame.java  
  *      
  *    Written by Jarred Baines, 2013  
 */  
   
 import javax.swing.JOptionPane;  
 import java.lang.Math;  
 import java.util.Scanner;  
   
 public class GuessGame {  
   
       
     // Give answer a random int between 1 and 100  
     private static int answer = (int)(Math.random()*100+1);  
     private static int guess; // variable to hold users current guess  
   
     //Method to generate message based on the guess  
     public static String makeGuess(int guess, int answer) {  
           
         // if...else ladder to generate message for user  
         if (guess < 1) { // This guess is invalid  
             return "Nope! The number is between 1 and 100";  
         } else if (guess > 100) { // This guess is invalid  
             return "Nope! The number is between 1 and 100";  
         } else if (guess < answer) { // The guess is too low  
             return "Too low! Try again?";  
         } else if (guess > answer) { // The guess is too high  
             return "Too high! Try again?";  
         } else if (guess == answer) { // The guess is correct  
             return "Congratulations! You are correct!";  
         } else return "Error detected"; // end of if ladder  
           
     } // end makeGuess method  
   
     public static void main(String[] args) {  
   
         // you can uncomment the following line of code to  
         // display answer in console, useful for debugging  
         //System.out.printf("Answer is: %d\n\n", answer);      
       
         do {  
         guess = Integer.parseInt(JOptionPane.showInputDialog(  
             "I'm thinking of a number between 1 and 100.\n\nGuess what it is!"));  
         JOptionPane.showMessageDialog(null, makeGuess(guess, answer));  
         } while (guess != answer);  
                       
     } // end main method  
   
 } // end GuessGame class  

A few points of interest I came across while creating this program:

Math.random() outputs a double value, greater than or equal to 0.0 and less than 1.0.
So to get a number between 1 and 100 we need to multiply it by 100.

Math.random()*100 outputs a double value greater than or equal to 0.0 and LESS THAN 100.0.
This means that it will never actually be 100, although it could be 99.99. Which in conventional maths would ROUND up to 100.

However, in a cast to int such as this:
(int)Math.random()*100
We LOSE the fractional part of the number, it doesn't get rounded, it gets lost! This gives us an output of 0 to 99.

thus, to get our required 1 to 100 random numbers, we must add 1 to the expression, like so:
(int)Math.random()*100+1