Advertisement
Guest User

Java issue

a guest
Feb 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. interface I_GG {
  4.    final int MIN_NUMBER = 1;
  5.    final int MAX_NUMBER = 205;
  6.    final int BACKDOOR_NUMBER = -314;
  7.    final int DFLT_NUMBER = 60;
  8.    final int WON_VALUE = 0;
  9.    final int QUIT_VALUE = -1;
  10.    final int LOSE_VALUE = -2;
  11.    int MAX_GAMES = 4;
  12.    final int MAX_GUESSES = 10;
  13.    int HINT_THRESHOLD = 5;
  14.    char NO = 'n', NO_UPPER = 'N';
  15.    char YES = 'y', YES_UPPER = 'Y';
  16.  
  17.    String NOPE_MSG = "nope...";
  18.    String NOPE_NOPE_MSG = "you've already guessed that wrong guess...";
  19.    String INVALID_INPUT_BEGIN = "*** invalid input -- ";
  20.    String INPUT_TOO_LOW_MSG = INVALID_INPUT_BEGIN + "must be greater than " +
  21.                               (MIN_NUMBER - 1);
  22.    String INPUT_TOO_HIGH_MSG = INVALID_INPUT_BEGIN + "must be less than " +
  23.                                (MAX_NUMBER + 1);
  24.    String INPUT_NOT_INT_MSG = INVALID_INPUT_BEGIN + "must be an whole number";
  25.    String NOT_YN_MSG = INVALID_INPUT_BEGIN + "must be " + NO + " or " + YES;
  26.    String WINNER_MSG = "you're a winner... # of guesses: ";
  27.    String LOSER_MSG = "too many guesses entered... the number was ";
  28.    String QUITTER_MSG = "you're a quitter... the number was ";
  29.    String MAX_GAMES_PLAYED_MSG = "\nMaximum number (" + MAX_GAMES +
  30.                                  ") of games played.";
  31.    String ENTER_GUESS_PROMPT = "enter a guess between " + MIN_NUMBER +
  32.                                " and " + MAX_NUMBER + " (" + QUIT_VALUE +
  33.                                " to quit): ";
  34.    String PLAY_AGAIN_PROMPT = "\nDo you want to play again (" + NO +
  35.                               " or " + YES + ")? ";
  36.  
  37.    String BOLD_BEGIN = "*** ";
  38.    String BOLD_END = " ***";
  39.    String PLAY_MSG = " playing the CSC205AA" + " guessing game." + BOLD_END;
  40.    String WELCOME_MSG = BOLD_BEGIN + "Hello! Have fun" + PLAY_MSG;
  41.    String THANKS_MSG = BOLD_BEGIN + "Thanks for" + PLAY_MSG;
  42.    String GUESSES_DUMP = "...guesses in ascending order: ";
  43.    String WINNER_RESULT = "Won";
  44.    String QUITTER_RESULT = "Quit";
  45.    String LOSER_RESULT = "Lost";
  46.    // printf() format strings...
  47.    String GAME_STATS_FMT = "games played: %d; won: %d; lost: %d; quit: %d;" +
  48.                            " winning pct.: %.2f%%\n";
  49.    String GAME_DUMP_FMT = "game %d: %s; the number was: %d; " +
  50.                       "#guesses: %d; backdoored: %s\n";
  51. }
  52.  
  53. public class GuessingGame
  54. {
  55.     public static void main(String[] argv)
  56.     {
  57.         Random rand = new Random(System.nanoTime());
  58.         Scanner scanner = new Scanner(System.in);
  59.         int numberToGuess = rand.nextInt(I_GG.MAX_NUMBER - I_GG.MIN_NUMBER) + I_GG.MIN_NUMBER;
  60.         int numberOfTries = 0;
  61.         int Wins = 0;
  62.         int inputGuess = 0;
  63.         int previousGuess = 0;
  64.         boolean previousGuessSame = false;
  65.          
  66.         System.out.println(I_GG.WELCOME_MSG);
  67.         while(true)
  68.         {
  69.  
  70.             numberOfTries = 0;
  71.             while (numberOfTries < I_GG.MAX_GUESSES)
  72.             {
  73.                 System.out.println(I_GG.ENTER_GUESS_PROMPT + "The number of guesses is: " + numberOfTries);
  74.                 inputGuess = scanner.nextInt();
  75.                 if(numberOfTries > 1)
  76.                 {
  77.                     previousGuess = inputGuess;
  78.                 }
  79.                 if (inputGuess < 1 || inputGuess > 205)
  80.                 {
  81.                     if(inputGuess == I_GG.QUIT_VALUE)
  82.                     {
  83.                         break;
  84.                     }
  85.                     else if(inputGuess == I_GG.BACKDOOR_NUMBER)
  86.                     {
  87.                         System.out.println("... number is " + numberToGuess);
  88.                     }else
  89.                     {
  90.                         System.out.println("*** invalid input -- must be greater than " + (I_GG.MIN_NUMBER - I_GG.MIN_NUMBER)
  91.                                 + " and must be less than " + (I_GG.MAX_NUMBER + I_GG.MIN_NUMBER + " ***"));
  92.                     }
  93.                 }
  94.                 else if (inputGuess == numberToGuess)
  95.                 {
  96.                     System.out.println(I_GG.WINNER_MSG + numberOfTries);
  97.                 }
  98.                 else if (inputGuess != numberToGuess && numberOfTries >= I_GG.HINT_THRESHOLD)
  99.                 {
  100.                     if(inputGuess < numberToGuess)
  101.                     {
  102.                         System.out.println(I_GG.INPUT_TOO_LOW_MSG);
  103.                     }
  104.                     else
  105.                     {
  106.                         System.out.println(I_GG.INPUT_TOO_HIGH_MSG);
  107.                     }
  108.                 }  
  109.                 else if (inputGuess == previousGuess)
  110.                 {
  111.                 System.out.println(I_GG.NOPE_NOPE_MSG);
  112.                 }
  113.                 else
  114.                 {
  115.                     System.out.println(I_GG.NOPE_MSG);
  116.                 }
  117.                 numberOfTries = numberOfTries++;
  118.                
  119.             }
  120.  
  121.             System.out.println(I_GG.PLAY_AGAIN_PROMPT);
  122.             //char c = scanner.next().trim().charAt(0);
  123.            
  124.             //if(c)
  125.            
  126.            
  127.             //if(scanner.next() == I_GG. || scanner.next() == I_GG.NO_UPPER)
  128.             //{
  129.             //  break;
  130.             //}
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement