Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Joo_Dennis_HiLow.java
- Description: With the three modes for the game, the user must guess the computer's number following the rules for the selected mode
- Date Created: April 18, 2019
- Date Modified: April 24, 2019
- */
- // The "HiLowMethods" class.
- import java.awt.*;
- import hsa.Console;
- import java.util.Random;
- public class HiLowMethods
- {
- static Console c; // The output console
- public static void main (String[] args)
- {
- c = new Console ();
- int min = 0;
- int max = 0;
- int choice = 1;
- Random rand = new Random ();
- int compnum = 0;
- int answer = compnum;
- double totalwins = 0; //total wins
- double totalgames = 0; //total games played
- c.println ("Hello! Please choose what level difficulty you want to play : ");
- c.println ("Type 1 for Easy Option : Number range is from 1 - 100, five guesses.");
- c.println ("Type 2 for Difficult Option: Number range is from 1 - 1000, ten guesses.");
- c.println ("Type 3 for Customized: Number range and amount of guesses in from your choice.");
- c.print ("Input here : ");
- choice = c.readInt ();
- Guess (choice, min, max);
- ending (totalwins, totalgames);
- int endchoice = 1;
- c.print ("Do you want to play again? Y - 1, N - 2 : ");
- endchoice = c.readInt ();
- totalgames++;
- }
- // Place your program here. 'c' is the output console
- // main method
- public static int min (int choice)
- {
- int min = 0;
- if (choice == 1 || choice == 2)
- {
- min = 1;
- }
- if (choice == 3)
- {
- c.print ("Okay user! Please input a range. Start with your minimum number : ");
- min = c.readInt ();
- }
- return min;
- }
- public static int max (int choice)
- {
- int max = 0;
- if (choice == 1)
- {
- max = 100;
- }
- else if (choice == 2)
- {
- max = 1000;
- }
- else if (choice == 3)
- {
- c.print ("Please input maximum number : ");
- max = c.readInt ();
- }
- return max;
- }
- public static void Guess (int choice, int min, int max)
- {
- int endchoice = 1; //option where the player can either play again or quit the game
- Random rand = new Random ();
- int num; //number guess
- int guess, compnum = 0; //guess is the amount of guesses they have left, minimum number, maximum number, computer's number
- double totalwins = 0; //total wins
- double totalgames = 0; //total games played
- if (choice == 1)
- {
- guess = 0;
- compnum = rand.nextInt (max - min + 1) + min;
- c.print ("I'm thinking of a number between 1 to 100, please guess a number! : ");
- while (guess < 5)
- {
- num = c.readInt ();
- if (num < 0 || num > 100)
- {
- c.print ("Invalid number, please input a number between 1 - 100 : ");
- guess++;
- }
- else if (num > compnum)
- {
- c.print ("Number is too high! ");
- guess++;
- }
- else if (num < compnum)
- {
- c.print ("Number is too low! ");
- guess++;
- }
- else
- {
- c.print ("Correct number! The number was : " + compnum + " ");
- totalwins++;
- guess = 6;
- }
- if (guess == 5)
- {
- c.print ("Sorry! The number was : " + compnum + " ");
- }
- else
- {
- c.print (" Try again. : ");
- }
- }
- c.print ("Do you want to play again? Y - 1, N - 2 : ");
- endchoice = c.readInt ();
- totalgames++;
- }
- else if (choice == 2)
- {
- guess = 0;
- compnum = rand.nextInt (max - min + 1) + min;
- c.print ("I'm thinking of a number between 1 to 1000, please guess a number! : ");
- while (guess < 10)
- {
- num = c.readInt ();
- if (num < 0 || num > 1000)
- {
- c.print ("Invalid number, please input a number between 1 - 100 : ");
- guess++;
- }
- else if (num > compnum)
- {
- c.print ("Number is too high! ");
- guess++;
- }
- else if (num < compnum)
- {
- c.print ("Number is too low! ");
- guess++;
- }
- else
- {
- c.print ("Correct number! The number was : " + compnum);
- totalwins++;
- guess = 11;
- }
- if (guess == 10)
- {
- c.print ("Sorry! The number was : " + compnum + " ");
- }
- else
- {
- c.print (" Try again : ");
- }
- }
- c.print ("Do you want to play again? Y - 1, N - 2 : ");
- endchoice = c.readInt ();
- totalgames++;
- }
- else if (choice == 3)
- {
- guess = 0;
- c.print ("Input amount of guesses : ");
- int newguess = c.readInt ();
- compnum = rand.nextInt (max - min + 1) + min;
- c.print ("I'm thinking of a number between " + min + " and " + max + ". Input a guess : ");
- while (guess < newguess)
- {
- num = c.readInt ();
- if (num < min || num > max)
- {
- c.print ("Please input a valid number within range : ");
- guess++;
- }
- else if (num > compnum)
- {
- c.print ("Number is too high! ");
- guess++;
- }
- else if (num < compnum)
- {
- c.print ("Number is too low! ");
- guess++;
- }
- else
- {
- c.print ("Correct number! The number was : " + compnum + " ");
- totalwins++;
- guess = newguess + 1;
- }
- if (guess == newguess)
- {
- c.print ("Sorry! The number was : " + compnum + " ");
- }
- else
- {
- c.print (" Try again. : ");
- }
- }
- }
- // return guess;
- }
- public static void ending (double totalwins, double totalgames)
- {
- c.println ("Total wins : " + totalwins);
- c.println ("Total games : " + totalgames);
- c.print ("Win percentage : ");
- c.print ((totalwins / totalgames) * 100, 0, 2);
- c.print ("%");
- }
- }
- // HiLowMethods class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement