Coders Packet

Number Guessing Game on different difficulty levels using Java Swings and AWT

By P. Veera Sai Rakesh

  • .settings/
  • bin/
  • src/
  • .classpath
  • .project
  • To develop a Number Guessing game is a game where we have to predict the number generated by system using some clues using java swings and AWT.

    Requirements:

    =>Desktop/Laptop with Windows

    =>Eclipse IDE for Java Developers

    Creating Window page to select the difficulty level:

    The first step in developing this game is to create a winow that selects the difficult level of the game. 

    Create a container consists of following components:

    1. JLabel - "Select Difficulty Level"

    2. JButton - "Easy"

    3. JButton - "Medium"

    4. JButton - "Hard"

    As shown in the image :

       Difficulty Level

    Add action listener to the above mentioned buttons as below :

    Easy: set the range as 1,10 by calling the method in the class Number_Guessing_levels.java

    Medium: set the range as 1,50

    Hard: set the range as 1,100

    Creating a class for setting the range of random number:

    create a class with the lower_bound and upper_bound as the data members and declare them as static. And set the values of those variables as mentioned difficulty level in a static method. i.e. for easy level lower_bound=1 and upper_bound=10, for medium level lower_bound=1 and upper_bound=50, for hard level lower_bound=1 and upper_bound=100.(The range is as your wish.). Lets say the class name as Number_guessing_levels

    Add the following code in the Number_guessing_levels:

    import java.io.*;
    import java.util.*;
    
    import javax.swing.JLabel;
    
    import java.lang.*;
    class Number_Guessing_levels{
      static int max_num;
      static int min_num;
      static int rand_num;
      static int i=1;
      //static boolean ext=false;
      public static void easy() {
        max_num=10;
        min_num=1;
        rand_num=min_num+(int)(Math.random()*(max_num-min_num+1));
    }
      public static void medium() {
        max_num=50;
        min_num=1;
        rand_num=min_num+(int)(Math.random()*(max_num-min_num+1));
    }
      public static void hard() {
        max_num=100;
        min_num=1;
        rand_num=min_num+(int)(Math.random()*(max_num-min_num+1));
    }
      public int compare_guess(int n1,int n2) {
        if(n1==n2) {
          return 1;
        }
        else if(n1<n2) {
          return 0;
        }
        else {
          return 2;
        }
      }
    }

     

    Creating a window for game:

    Next step is to create a window for game.Create a container with the following components:

    1. TextField - for input.

    2. Label - represents the range of random number.

    3. Label - represents the number of attempts you have done to find the correct number.

    4. Button - "Submit".

    5. Label - represents the field that gives the clues.

    As shown in the below figure:

    Game Window

    Add the action listener for the submit button above as below:

    if the generated number matches with the guessed number then set the label as "Congratulations.... You Win!  Your Score: {score}", or else the generated number is greater than guessed numbed then print the clue label as "Random number is greater than guessed number", else "Random number is less than guessed number".

    Hence the output will be as follow:

     

    Output

     

    Download Complete Code

    Comments

    No comments yet

    Download Packet

    Reviews Report

    Submitted by P. Veera Sai Rakesh (SaiRakesh31)

    Download packets of source code on Coders Packet