Advertisement
Nickster258

CSCI Project 1

Jun 25th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. /*      Author:         Nicholas Stonecipher
  2. **      Date:           6.27.2016
  3. **      Professor:      ####
  4. **      Project:        #1 - Random Number Game
  5. **      Version:        1.3 More bug fixes...
  6. **      Disclaimer:
  7. **      My compiler did not like splitting System.out lines into multiple lines.
  8. **      Be it either because of Java 7 or 8, I do not know, but it would not
  9. **      compile if I split up the print statement.
  10. **
  11. **      Points are not influenced if the number is invalid, out of range, or part of a command.
  12. **
  13. */
  14. import java.util.Random;
  15. import java.util.Scanner;
  16.  
  17. public class randomnumbergame {
  18.   public static void main (String[] args) {
  19.     Scanner scan = new Scanner (System.in);
  20.  
  21. // Generation of the infamous random number
  22.     Random rand = new Random ();
  23.     int random = (rand.nextInt(1000)+1);
  24.  
  25. // General variables
  26.     int wins = 0;           // Keeps track of the total wins
  27.     int points = 100;       // Keeps tract of the total points
  28.     int guesses = 0;        // Keeps track of total guesses
  29.     int rounds = 1;         // Keeps track of total rounds played
  30.     int upperLimit = 1000;  // Sets the upper limit of the guess
  31.     int lowerLimit = 1;     // Sets the lower limit of the guess
  32.     int pointsLost = 0;     // Keeps track of the total points lost
  33.     int pointsWon = 0;      // Keeps track of the total points gained
  34.     Boolean over = false;   // Boolean that keeps the while going unless otherwise stated
  35.    
  36.     System.out.println("\n\tTo play the game, enter a random number from 1 to 1000.\n\tThe user starts out with 100 points. Every incorrect\n\tresponse is a deduction of 2 points while with\n\tevery correct response there is an award of 10 points.\n\tThe program will keep track of your guesses and other statistics.\n\tYou can exit the game at any time by typing \'done\'.\n");
  37.  
  38.     while (!over) {
  39.  
  40.     // Multiple statements for better pint out flow
  41.       if (guesses == 0) {
  42.         System.out.print("Guess a number from 1 to 1000: ");
  43.       }
  44.       else {
  45.         System.out.print("Guess a number from " + lowerLimit + " to " + upperLimit + " (score=" + points + "): ");
  46.       }
  47.  
  48.       String input = scan.nextLine();
  49.  
  50.     // Quick validation if the user wants to quit the game
  51.       if (input.equalsIgnoreCase ("done")){
  52.         over = true;
  53.       }
  54.       else {
  55.  
  56.     // Put into a try block so improper inputs can be handled without the game crashing
  57.         try {
  58.           int guess = Integer.parseInt(input);
  59.  
  60.       // Win detection
  61.           if (guess == random){
  62.             System.out.print("Congragulations, you guessed correctly! Go for round " + (rounds+1) + "? (y/n): ");
  63.             String temp = scan.nextLine();
  64.             if (temp.equalsIgnoreCase ("n")){
  65.               over = true;
  66.             }
  67.             else {
  68.               upperLimit = 1000;
  69.               lowerLimit = 1;
  70.               random = (rand.nextInt(1000)+1);
  71.               rounds++;
  72.             }
  73.             points = points + 10;
  74.             pointsWon = pointsWon + 10;
  75.             wins++;
  76.             guesses++;
  77.           }
  78.  
  79.       // Loss detection
  80.           else if ((points-2) <= 0) {
  81.             System.out.print("Oh no, you ran out of points! Would you like to reset and try again? (y/n): ");
  82.             String temp = scan.nextLine();
  83.             if (temp.equalsIgnoreCase ("n")){
  84.               over = true;
  85.               points = points - 2;
  86.             }
  87.             else if (temp.equalsIgnoreCase("y")){
  88.               upperLimit = 1000;
  89.               lowerLimit = 1;
  90.               points = 100;
  91.               wins = 0;
  92.               rounds = 0;
  93.               guesses = 0;
  94.               pointsLost = 0;
  95.               pointsWon = 0;
  96.               random = (rand.nextInt(1000)+1);
  97.             }
  98.             else {
  99.               System.out.println("\'" + temp + "\' is not a valid input!");
  100.             }
  101.           }
  102.  
  103.       // Outside of range detection
  104.           else if (guess > upperLimit || guess < lowerLimit){
  105.             System.out.println("That number is outside of the range. Try again.");
  106.           }
  107.  
  108.       // Detecting if user guessed previously guessed limits
  109.           else if (guess == upperLimit || guess == lowerLimit){
  110.             System.out.println("That number was already guessed and is not within the range. Try again.");
  111.           }
  112.  
  113.       // Detecting and setting upperLimit
  114.           else if (guess > random){
  115.             upperLimit = guess;
  116.             System.out.println("You guessed higher than the number! ");
  117.             points = points - 2;
  118.             pointsLost = pointsLost + 2;
  119.             guesses++;
  120.           }
  121.  
  122.       // Detecting and setting lowerLimit
  123.           else {
  124.             lowerLimit = guess;
  125.             System.out.println("You guessed lower than the number! ");
  126.             points = points - 2;
  127.             pointsLost = pointsLost + 2;
  128.             guesses++;
  129.           }
  130.         }
  131.  
  132.     // Prevents the game from crashing
  133.         catch(NumberFormatException exception){
  134.           System.out.println ("\'" + input + "\' is not a valid input!");
  135.         }
  136.       }
  137.     }
  138.  
  139.   // Clean output of global statistics collected throughout the game
  140.     System.out.println("\nHere are some global statistics for you!\n\tRounds played:\t" + rounds + "\n\tTotal wins:\t" + wins + "\n\tTotal guesses:\t" + guesses + "\n\tTotal points lost:\t" + pointsLost + "\n\tTotal points won:\t" + pointsWon + "\n");
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement