Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class GuessingGame {
- //First text based game. Guess the random number that will be generated, and gather points!
- public static void main(String[] args) {
- try (Scanner store = new Scanner(System.in)){ //This < is used to create a new scanner variable . Used the try method because Eclipse was giving a 'resource leak'
- Random rnd = new Random();
- //Only 4 rounds so 4 random number ints.
- int guess; //Will store the number that you will guess.
- int totalpoints = 0;
- char readytoplay;
- int randomnumber1 = rnd.nextInt(1)+1; //First level of random numbers
- int randomnumber2 = rnd.nextInt(2)+1;
- int randomnumber3 = rnd.nextInt(4)+1;
- int randomnumber4 = rnd.nextInt(9)+1;
- System.out.println("Welcome to Guessing game!");
- System.out.println("In this game, you will guess a number to see how lucky you are.");
- System.out.println("As you progress, it gets harder and harder. ");
- System.out.println("You will gain points as you go. ");
- System.out.println("Are you ready to play? Y/N");
- readytoplay = store.next().charAt(0); //This will store a CHAR
- System.out.println("You chose: " + readytoplay);
- while(true){
- if(readytoplay == 'y' ||readytoplay == 'Y'){ //Possible response types for Yes.
- System.out.println("Okay! Great! Lets get started.");
- break;
- }
- else if(readytoplay == 'n' ||readytoplay == 'N'){ //Possible response types for No.
- System.out.println("If you don't want to play, please exit.");
- readytoplay = store.next().charAt(0);
- break;
- }
- else{
- System.out.println("You must answer EITHER (Y)es or (n)o"); //Letting them know they didn't answer either Y, y, N, n so it was a invalid response.
- readytoplay = store.next().charAt(0); // This will store their answer for them to re submit a valid response.
- continue;
- }
- }
- System.out.println("Okay. So let's explain how this works. ");
- try { // This will delay the Strings being typed. 1500 = 1.5 secs.
- Thread.sleep(900); //
- } // I added these to make the text easy to read for the user
- catch (InterruptedException ex) // To not spam them with everything to read all at once.
- { // Not sure of an easier way to do this so maybe a TODO: getting a easier method..
- //
- } //
- System.out.println("You will be given a 'Range' of numbers to guess for the answer.");
- try {
- Thread.sleep(900);
- }
- catch (InterruptedException ex)
- {
- }
- System.out.println("FOR EXAMPLE:");
- try {
- Thread.sleep(900);
- }
- catch (InterruptedException ex)
- {
- }
- System.out.println("1-3");
- try {
- Thread.sleep(900);
- }
- catch (InterruptedException ex)
- {
- }
- System.out.println("You can guess either 1, 2, or 3.");
- try {
- Thread.sleep(900);
- }
- catch (InterruptedException ex)
- {
- }
- System.out.println("The machine will generate a random number between either of those, if yours is correct, you advance!");
- try {
- Thread.sleep(900);
- }
- catch (InterruptedException ex)
- {
- }
- System.out.println("Let's try this out.");
- try {
- Thread.sleep(900);
- }
- catch (InterruptedException ex)
- {
- }
- System.out.println("The first set is 1-2. Pick 1, or 2.");
- guess = store.nextInt();
- //ROUND 1
- if (guess == randomnumber1) {
- System.out.println("You were correct.");
- System.out.println("You guessed "+guess);
- System.out.println("The random number was "+randomnumber1);
- totalpoints = +1; //This will add points to their total points.
- System.out.println("You gained 1 point, Your total points are now: " +totalpoints); //Obviously letting them know how many points they now have.
- }
- else {
- System.out.println("You didn't guess correctly.");
- System.out.println("Your number:" +guess);
- System.out.println("The random number:" +randomnumber1);
- System.exit(0); //Exiting the game, they did not have any points at the time so not displaying any points .
- }
- {
- //ROUND 2
- System.out.println("Good job , by the way! You got lucky! Literally!");
- try { //
- Thread.sleep(1000); //
- } //
- catch (InterruptedException ex) //Pausing for a second to not spam.. Letting them know round 2 is starting.
- { //
- //
- } //
- System.out.println("Next round. Your numbers are 1-3. 1,2, or 3 ");
- System.out.println("Pick your number:");
- guess = store.nextInt();
- if (guess == randomnumber2) {
- System.out.println("You were correct. ");
- System.out.println("You guessed "+guess);
- System.out.println("The random number was " +randomnumber2);
- totalpoints = +2;
- System.out.println("You gain 2 points for that one! you now have: " +totalpoints+ "points!");
- }
- else {
- System.out.println("You were not correct, sorry..");
- System.out.println("You guessed: " +guess);
- System.out.println("The random number was " +randomnumber2);
- System.out.println("Game over. You ended with: " +totalpoints+ " point");
- System.exit(0);
- }
- try { //
- Thread.sleep(1000); //
- } //
- catch (InterruptedException ex) //Pausing for a second to not spam.. Letting them know round 3 is starting.
- { //
- } //
- //ROUND 3
- System.out.println("Round 3. You will need to guess 1-5. Good luck on this one...");
- System.out.println("Guess now. 1,2,3,4, or 5.");
- guess = store.nextInt();
- if (guess == randomnumber3) {
- System.out.println("You were correct. ");
- System.out.println("You guessed "+guess);
- System.out.println("The random number was " +randomnumber3);
- totalpoints = +3;
- System.out.println("You gain 3 points for that one! you now have: " +totalpoints+ " points!");
- }
- else {
- System.out.println("You were not correct, sorry..");
- System.out.println("You guessed: " +guess);
- System.out.println("The random number was " +randomnumber3);
- System.out.println("Game over. You ended with: " +totalpoints+ " point");
- System.exit(0);
- }
- try { //
- Thread.sleep(1000); //
- }
- catch (InterruptedException ex) //Pausing for a second to not spam.. Letting them know round 4 is starting.
- { //
- //
- } //
- //ROUND 4
- System.out.println("You are really lucky. Maybe you should play the lottery");
- System.out.println("Although, I believe this will be a little more difficult.");
- System.out.println("You will be given a random number 1-10. 1,2,3,4,5,6,7,8,9, or 10.");
- System.out.println("You have a 10% chance. Goodluck.);");
- System.out.println("Guess now:");
- guess = store.nextInt();
- if (guess == randomnumber4) {
- System.out.println("You were correct. ");
- System.out.println("You guessed " +guess);
- System.out.println("The random number was " +randomnumber4);
- totalpoints = + 4;
- System.out.println("You gain 4 points. You now have: " +totalpoints+ "points!");
- }
- else {
- System.out.println("You were wrong. You guessed " +guess);
- System.out.println("The random number was: " +randomnumber4);
- System.out.println("You ended the game with " +totalpoints+ "points.");
- System.exit(0);
- }
- try { //
- Thread.sleep(500); //
- } //
- catch (InterruptedException ex) //Pausing for a second to not spam..
- { //
- //
- } //
- System.out.println("CONGRATULATIONS! You beat all the odds, somehow..");
- System.out.println("You ended the game with: " +totalpoints);
- System.exit(0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement