Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class guessGame {
- public static final String[] diff = {"Easy (1-10)","Medium (1-50)","Hard (1-100)"};//difficulty array
- public static Scanner input = new Scanner(System.in);
- public static Random rGen = new Random();
- public static int rNum = 0;
- public static int guesses = 0;
- public static int diffChoice = 0;
- public static int guessNum = 0;
- public static int choice = 0;
- public static Main mainObj = new Main();
- public guessGame(){
- System.out.println("\tNUMBER GUESSING GAME v1.0");
- System.out.println("Welcome to the Number Guessing game!");
- getDifficulty();
- }
- public static void getDifficulty(){
- guesses = 0;
- System.out.println("Select a difficulty in order to begin!\nType the number of the difficulty you want to play!");
- //print out difficulties
- for(int x=0;x<diff.length;x++){
- System.out.printf("%d. ", x);
- System.out.println(diff[x]);
- }
- //get difficulty as an int
- System.out.print("Difficulty: ");
- diffChoice = input.nextInt();//get the input
- //now that difficulty is picked we need to generate a number
- genNumber(diffChoice);
- }
- public static void genNumber(int diffChoice){
- switch(diffChoice){
- case 0:
- //1-10
- rNum = rGen.nextInt(9) + 1; //generate 0-9, add 1
- gameBody(rNum);
- break;
- case 1:
- //1-50
- rNum = rGen.nextInt(49) + 1;
- gameBody(rNum);
- break;
- case 2:
- //1-100
- rNum = rGen.nextInt(99) + 1;
- gameBody(rNum);
- break;
- default:
- rNum = rGen.nextInt(9) + 1;
- gameBody(rNum);
- }
- }
- public static void gameBody(int rNum){
- //number they have to guess is rNum
- System.out.println("\nA number has been chosen!");
- while(guesses < 5 && guessNum != rNum){
- System.out.printf("Guess(%d): ",guesses);
- guessNum = input.nextInt();
- if(guessNum > rNum){
- guesses+=1;
- System.out.printf("That guess was too high!\nGuesses remaining: %d\n", 5-guesses);
- }else if(guessNum < rNum){
- guesses+=1;
- System.out.printf("That guess was too low!\nGuesses remaining: %d\n", 5-guesses);
- }else if(guessNum == rNum){
- guesses+=1;
- gameOver(guesses);
- }
- }
- gameOver(guesses);
- }
- public static void gameOver(int guesses){
- if(guesses == 5){
- System.out.println("Oh no! You lost!");
- System.out.println("What would you like to do?");
- System.out.println("Type the number of the option you choose.");
- System.out.println("0. Play Again\n1. Choose Another Game\n2. Quit");
- System.out.print("Choice: ");
- choice = input.nextInt();
- if(choice == 0){
- getDifficulty();
- }else if(choice == 1){
- Main.whatGame();
- }else if(choice == 2){
- System.exit(0);
- }
- }else{
- System.out.println("Congrats! You won " + Main.name + " !\nYou beat the game in " + guesses + "/5 guesses!");
- System.out.println("What would you like to do?");
- System.out.println("Type the number of the option you choose.");
- System.out.println("0. Play Again\n1. Choose Another Game\n2. Quit");
- System.out.print("Choice: ");
- choice = input.nextInt();
- if(choice == 0){
- getDifficulty();
- }else if(choice == 1){
- Main.whatGame();
- }else if(choice == 2){
- System.exit(0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement