Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- interface I_GG {
- final int MIN_NUMBER = 1;
- final int MAX_NUMBER = 205;
- final int BACKDOOR_NUMBER = -314;
- final int DFLT_NUMBER = 60;
- final int WON_VALUE = 0;
- final int QUIT_VALUE = -1;
- final int LOSE_VALUE = -2;
- int MAX_GAMES = 4;
- final int MAX_GUESSES = 10;
- int HINT_THRESHOLD = 5;
- char NO = 'n', NO_UPPER = 'N';
- char YES = 'y', YES_UPPER = 'Y';
- String NOPE_MSG = "nope...";
- String NOPE_NOPE_MSG = "you've already guessed that wrong guess...";
- String INVALID_INPUT_BEGIN = "*** invalid input -- ";
- String INPUT_TOO_LOW_MSG = INVALID_INPUT_BEGIN + "must be greater than " +
- (MIN_NUMBER - 1);
- String INPUT_TOO_HIGH_MSG = INVALID_INPUT_BEGIN + "must be less than " +
- (MAX_NUMBER + 1);
- String INPUT_NOT_INT_MSG = INVALID_INPUT_BEGIN + "must be an whole number";
- String NOT_YN_MSG = INVALID_INPUT_BEGIN + "must be " + NO + " or " + YES;
- String WINNER_MSG = "you're a winner... # of guesses: ";
- String LOSER_MSG = "too many guesses entered... the number was ";
- String QUITTER_MSG = "you're a quitter... the number was ";
- String MAX_GAMES_PLAYED_MSG = "\nMaximum number (" + MAX_GAMES +
- ") of games played.";
- String ENTER_GUESS_PROMPT = "enter a guess between " + MIN_NUMBER +
- " and " + MAX_NUMBER + " (" + QUIT_VALUE +
- " to quit): ";
- String PLAY_AGAIN_PROMPT = "\nDo you want to play again (" + NO +
- " or " + YES + ")? ";
- String BOLD_BEGIN = "*** ";
- String BOLD_END = " ***";
- String PLAY_MSG = " playing the CSC205AA" + " guessing game." + BOLD_END;
- String WELCOME_MSG = BOLD_BEGIN + "Hello! Have fun" + PLAY_MSG;
- String THANKS_MSG = BOLD_BEGIN + "Thanks for" + PLAY_MSG;
- String GUESSES_DUMP = "...guesses in ascending order: ";
- String WINNER_RESULT = "Won";
- String QUITTER_RESULT = "Quit";
- String LOSER_RESULT = "Lost";
- // printf() format strings...
- String GAME_STATS_FMT = "games played: %d; won: %d; lost: %d; quit: %d;" +
- " winning pct.: %.2f%%\n";
- String GAME_DUMP_FMT = "game %d: %s; the number was: %d; " +
- "#guesses: %d; backdoored: %s\n";
- }
- public class GuessingGame
- {
- public static void main(String[] argv)
- {
- Random rand = new Random(System.nanoTime());
- Scanner scanner = new Scanner(System.in);
- int numberToGuess = rand.nextInt(I_GG.MAX_NUMBER - I_GG.MIN_NUMBER) + I_GG.MIN_NUMBER;
- int numberOfTries = 0;
- int Wins = 0;
- int inputGuess = 0;
- int previousGuess = 0;
- boolean previousGuessSame = false;
- System.out.println(I_GG.WELCOME_MSG);
- while(true)
- {
- numberOfTries = 0;
- while (numberOfTries < I_GG.MAX_GUESSES)
- {
- System.out.println(I_GG.ENTER_GUESS_PROMPT + "The number of guesses is: " + numberOfTries);
- inputGuess = scanner.nextInt();
- if(numberOfTries > 1)
- {
- previousGuess = inputGuess;
- }
- if (inputGuess < 1 || inputGuess > 205)
- {
- if(inputGuess == I_GG.QUIT_VALUE)
- {
- break;
- }
- else if(inputGuess == I_GG.BACKDOOR_NUMBER)
- {
- System.out.println("... number is " + numberToGuess);
- }else
- {
- System.out.println("*** invalid input -- must be greater than " + (I_GG.MIN_NUMBER - I_GG.MIN_NUMBER)
- + " and must be less than " + (I_GG.MAX_NUMBER + I_GG.MIN_NUMBER + " ***"));
- }
- }
- else if (inputGuess == numberToGuess)
- {
- System.out.println(I_GG.WINNER_MSG + numberOfTries);
- }
- else if (inputGuess != numberToGuess && numberOfTries >= I_GG.HINT_THRESHOLD)
- {
- if(inputGuess < numberToGuess)
- {
- System.out.println(I_GG.INPUT_TOO_LOW_MSG);
- }
- else
- {
- System.out.println(I_GG.INPUT_TOO_HIGH_MSG);
- }
- }
- else if (inputGuess == previousGuess)
- {
- System.out.println(I_GG.NOPE_NOPE_MSG);
- }
- else
- {
- System.out.println(I_GG.NOPE_MSG);
- }
- numberOfTries = numberOfTries++;
- }
- System.out.println(I_GG.PLAY_AGAIN_PROMPT);
- //char c = scanner.next().trim().charAt(0);
- //if(c)
- //if(scanner.next() == I_GG. || scanner.next() == I_GG.NO_UPPER)
- //{
- // break;
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement