Tuesday, January 10, 2012

Tutorial: A Guessing Game in Java

by eturo


Today, I want to introduced a simple guessing game I made in Java. This game is for everybody wherein the player has to guessed a random number from 1 to 10 which is generated by the program. If the player guessed the correct random number then he/she wins, otherwise he/she will be a loser. The game has also the ability to ask the player if he/she wants to continue the game.

In creating this simple game program, I used predefined (built-in) methods of classes such as the random() method of the Math class. I used looping statements (do-while, while, for loops) to create iteration such as in continuing the game. I also used conditional statements such as if-else and nested-if to compare the guessed number of the player with the random number generated by the computer.

How to Play the Game?

You need to register first your name. (see the image below)


After entering your name, the computer will ask you to enter any guessed number from number 1 to 10. (see the picture below)



The player enter his guessed number as shown below.

 

Here, the player enter number 5 as his guessed number but unfortunately the computer displayed a message that the player lose the game because his guessed number did not matched with the number generated by the computer.

Now, if the player enter a guessed number higher to 10, then an error message stating that the player needs to enter number from 1 to 10 only (see the image below).




















In this type of game, guessing the correct number is not that just easy than you think. You need a special power, I mean you need to activate your third eye (hehehe), or what they call it the ESP (Extra Sensory Perception). For me, this game can help you to think wisely and intelligently using your ESP to be able to win. But, just like what I said, hindi ganung kadali (lol).

The player can continue the game by entering either the big letter 'Y' or small letter 'y'. (see the image below).





















If the player either big letter 'N' or small letter 'n', the game will be terminated as shown in the image below.



The Program Source Code:

Here is the completer source code of the said game. It's free and if you think you can add more challenging features such as giving a maximum tries for the player to guessed the random number, then definitely you are free to experiment it.

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;
import java.io.*;


class GuessingGame
{
    public static void main(String args[])
    { 
        Scanner myScanner = new Scanner(System.in);
        String myChoice;
        char myAnswer;
       
        out.println("\"THIS IS A SIMPLE GUESSING GAME\"");
        out.println("==================================");
        out.println("\nInstruction:");
        out.println("\t1.Try to guess a number.");
        out.println("\t2.Enter a guessed number from 1 to 10 only.");
        out.print("\nPlease enter your name to register: ");
        String myName = myScanner.nextLine();
       
        //execute the statements inside
        do
        {
            //the player input his/her guessed number
            out.println("\nGuess what the number is?");
            out.print("Enter any integer from 1 to 10: ");
            int inputNumber = myScanner.nextInt();
            int randomNumber = new Random().nextInt(10) + 1;
                      
            //the program test whether the player guessed a number
            //greater than to 10 (the maximum number to guess),then
            //it will prompt an error message
            if (inputNumber >= 10)
            {
                out.println("\nError!Please input number from 0 to 10 only.");
            }
            else
            {
                //the program test whether the guessed number of the player
                //is equal to the random number generated by the computer
                if (inputNumber == randomNumber)
                {
                    out.println("\n****************");
                    out.println("*** You WIN! ***");
                    out.println("*****************");
                }
                //if it is not, then the program prompt a message
                else
                {
                    out.println("\n****************");
                    out.println("*** You LOSE! **");
                    out.println("****************");
                    out.print("The random number was ");
                    out.println(randomNumber + ".");
                }
            }
            //this line ask the computer if the player wants to continue thea
            //game or not
            out.print("\nContinue to play...Y/N?");
            myChoice = myScanner.next();
            myAnswer = myChoice.charAt(0);
        }
        //this line of the program test whether the choice of the player is equal to
        //Y or y, then if this statement satisfied then it will go back to the
        //upper part of the program mainly inside the body of the "do" statement
       
        //if the choice of the player do not match with the correct answer then
        //the game will be terminated
        while (myAnswer == 'Y' || myAnswer == 'y');    
        out.println("Goodbye " + myName.toUpperCase());       
        out.println("\nThank you for playing.");
   
    }
}


Note: The two backslashes (//) in the source code indicates a comment. Meaning that they do not actually executed when you run your program. They just give an instruction or idea to other people or programmer to what those line of codes do in the program given by the author. That's it. More Java games to come. More Java tutorials soon. So, visit my blog regularly to learn more! Happy coding...

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...