mynumberguessinggame

Number Guessing Game

Table of Contents

The following documentation gives instruction on how to get started and play the number guessing game.

Materials Needed

This program works for both IOS and Windows OS.

Getting Started with Eclipse

This game will run in Eclipse. Prior to downloading eclipse you will want to check if you have Java JDK installed The easiest way to check if you have Java JDK installed is

![](https://cdn.discordapp.com/attachments/833448220620488795/910998507958177792/command.png) - If you need to download [Java JDK](https://www.oracle.com/java/technologies/downloads/#jdk17-windows) then click on the link and follow the instructions from there.  ##### Once you have [Java JDK](https://www.oracle.com/java/technologies/downloads/#jdk17-windows) Installed 1. Begin by downloading [Eclipse](https://www.eclipse.org/downloads/packages/release/kepler/sr1/eclipse-ide-java-developers)   ![](https://cdn.discordapp.com/attachments/833448220620488795/911003955184693308/Copy.png)
  1. Open the .exe file located in the .zip folder

  2. Now you have successfully downloaded and opened eclipse

Installation

The website that the source code is obtained from did not provide a .zip file for the game. So you will need to

  1. Create a new Java Project in Eclipse
  2. Title this project “numberGuessingGame” with the “Use default location” option selected. Then select finish
  3. When prompted to create “module-info.java” file select “Don’t Create”.

  4. Next you will double click the file “numberGuessingGame”, right click on the “src” file and select “new” then “class”.

  5. When prompted to title your new class enter “GuessingGame” and select “finish”.

Setup

package numberGuessingGame;
import javax.swing.*;
 
public class GuessingGame {
    public static void main(String[] args) {
        int computerNumber = (int) (Math.random()*100 + 1);
        int userAnswer = 0;
        System.out.println("The correct guess would be " + computerNumber);
        int count = 1;
        while (userAnswer != computerNumber)
        {
            String response = JOptionPane.showInputDialog(null,
                "Enter a guess between 1 and 100", "Guessing Game", 3);
            userAnswer = Integer.parseInt(response);
            JOptionPane.showMessageDialog(null, ""+ determineGuess(userAnswer, computerNumber, count));
            count++;
        }  
    }

    public static String determineGuess(int userAnswer, int computerNumber, int count){
        if (userAnswer <=0 || userAnswer >100) {
            return "Your guess is invalid";
        }
        else if (userAnswer == computerNumber ){
            return "Correct!\nTotal Guesses: " + count;
        }
        else if (userAnswer > computerNumber) {
            return "Your guess is too high, try again.\nTry Number: " + count;
        }
        else if (userAnswer < computerNumber) {
            return "Your guess is too low, try again.\nTry Number: " + count;
        }
        else {
            return "Your guess is incorrect\nTry Number: " + count;
        }
    }
}

How to Play

  1. After you copy the source code into your java project you need to select the run option.
  2. A window will appear asking you to choose a number between 1 and 100.
  3. Type in a value from your keyboard that is 1-100.
  4. If you guess wrong you will continue until you guess correctly, or click the cancel button to stop the game.

Code

import

Where can I find the source code for this game?

You can find the original source code from the page >hackr.io.

Do I need to know Java prior to playing this game?

No, you should be able to follow this documentation and play this game without any background in java.

Troubleshooting/Questions

If you run into problems you can always email me at: robertscc1@appstate.edu

Contribution

If you would like to contribute to the project, then contact me at robertscc1@appstate.edu.

License

This project is distributed by an MIT license.