Advertisement
daixso

Number guessing game

Nov 10th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class guessGame {
  4.     public static final String[] diff = {"Easy (1-10)","Medium (1-50)","Hard (1-100)"};//difficulty array
  5.     public static Scanner input = new Scanner(System.in);
  6.     public static Random rGen = new Random();
  7.     public static int rNum = 0;
  8.     public static int guesses = 0;
  9.     public static int diffChoice = 0;
  10.     public static int guessNum = 0;
  11.     public static int choice = 0;
  12.     public static Main mainObj = new Main();
  13.    
  14.     public guessGame(){
  15.         System.out.println("\tNUMBER GUESSING GAME v1.0");
  16.         System.out.println("Welcome to the Number Guessing game!");
  17.         getDifficulty();
  18.     }
  19.    
  20.     public static void getDifficulty(){
  21.         guesses = 0;
  22.         System.out.println("Select a difficulty in order to begin!\nType the number of the difficulty you want to play!");
  23.         //print out difficulties
  24.         for(int x=0;x<diff.length;x++){
  25.             System.out.printf("%d. ", x);
  26.             System.out.println(diff[x]);
  27.         }
  28.         //get difficulty as an int
  29.         System.out.print("Difficulty: ");
  30.         diffChoice = input.nextInt();//get the input
  31.        
  32.         //now that difficulty is picked we need to generate a number
  33.         genNumber(diffChoice);
  34.     }
  35.    
  36.     public static void genNumber(int diffChoice){
  37.         switch(diffChoice){
  38.         case 0:
  39.             //1-10
  40.             rNum = rGen.nextInt(9) + 1; //generate 0-9, add 1
  41.             gameBody(rNum);
  42.             break;
  43.         case 1:
  44.             //1-50
  45.             rNum = rGen.nextInt(49) + 1;
  46.             gameBody(rNum);
  47.             break;
  48.         case 2:
  49.             //1-100
  50.             rNum = rGen.nextInt(99) + 1;
  51.             gameBody(rNum);
  52.             break;
  53.         default:
  54.             rNum = rGen.nextInt(9) + 1;
  55.             gameBody(rNum);
  56.         }
  57.     }
  58.    
  59.    
  60.     public static void gameBody(int rNum){
  61.         //number they have to guess is rNum
  62.         System.out.println("\nA number has been chosen!");
  63.         while(guesses < 5 && guessNum != rNum){
  64.             System.out.printf("Guess(%d): ",guesses);
  65.             guessNum = input.nextInt();
  66.             if(guessNum > rNum){
  67.                 guesses+=1;
  68.                 System.out.printf("That guess was too high!\nGuesses remaining: %d\n", 5-guesses);
  69.             }else if(guessNum < rNum){
  70.                 guesses+=1;
  71.                 System.out.printf("That guess was too low!\nGuesses remaining: %d\n", 5-guesses);
  72.             }else if(guessNum == rNum){
  73.                 guesses+=1;
  74.                 gameOver(guesses);
  75.             }
  76.         }
  77.         gameOver(guesses);
  78.     }
  79.    
  80.     public static void gameOver(int guesses){
  81.         if(guesses == 5){
  82.             System.out.println("Oh no! You lost!");
  83.             System.out.println("What would you like to do?");
  84.             System.out.println("Type the number of the option you choose.");
  85.             System.out.println("0. Play Again\n1. Choose Another Game\n2. Quit");
  86.             System.out.print("Choice: ");
  87.             choice = input.nextInt();
  88.             if(choice == 0){
  89.                 getDifficulty();
  90.             }else if(choice == 1){
  91.                 Main.whatGame();
  92.             }else if(choice == 2){
  93.                 System.exit(0);
  94.             }
  95.         }else{
  96.             System.out.println("Congrats! You won " + Main.name + " !\nYou beat the game in " + guesses + "/5 guesses!");
  97.             System.out.println("What would you like to do?");
  98.             System.out.println("Type the number of the option you choose.");
  99.             System.out.println("0. Play Again\n1. Choose Another Game\n2. Quit");
  100.             System.out.print("Choice: ");
  101.             choice = input.nextInt();
  102.             if(choice == 0){
  103.                 getDifficulty();
  104.             }else if(choice == 1){
  105.                 Main.whatGame();
  106.             }else if(choice == 2){
  107.                 System.exit(0);
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement