Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 15th, 2012  |  syntax: None  |  size: 7.82 KB  |  views: 2,410  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Number{
  6.         static int input = 20;
  7.         static int input1 = 20;
  8.         static int input2 = 20;
  9.         static String finish = "";
  10.         static int e = 9;
  11.         static int f = 0;
  12.        
  13.         public static String readLine(String prompt) {
  14.                     String input = "";
  15.                     System.out.print(prompt);
  16.                     InputStreamReader isr = new InputStreamReader(System.in);
  17.                     BufferedReader br = new BufferedReader(isr);
  18.                     try {
  19.  
  20.                       input = br.readLine();
  21.  
  22.                     } catch (IOException ioe) {
  23.                     }
  24.                     return input;
  25.                   }
  26.  
  27.                    public static int readInteger(String prompt) {
  28.                     try {
  29.  
  30.                       return (int) Integer.parseInt(readLine(prompt));
  31.  
  32.                     } catch (NumberFormatException nfe) {
  33.                     }
  34.                     return input = 999999;
  35.                   }
  36.        
  37.         public static void Player1(String finish, int x, int f, int score1, int lastdiff1, int input, int d, int e, boolean test, int input2, int y, int input1) {
  38.                 do {
  39.                         if (test == true) {
  40.                                 input = readInteger("I'm thinking of an a integer between 0 and " + d + " inclusively, enter a guess: ");
  41.                                 score1++;
  42.                         } else if (test == false) {
  43.                                 input = readInteger("Unfortunately that's wrong, guess another integer between 0 and " + d + " inclusively: ");
  44.                                 score1++;
  45.                         }
  46.                         if ((input > d) || (input < 0)) {
  47.                                 do {
  48.                                         input = readInteger("Very funny, I'll ignore that. Guess an integer between 0 and " + d + " inclusively: ");
  49.                                 } while ((input > d) || (input < 0));
  50.                         }
  51.                        
  52.                         if (input == x) {
  53.                                 System.out.println("Well done! The number is: " + x + ". You did it in " + score1 + " tries!");
  54.                                 Win(finish, f, input2, y, input1, x);
  55.                         } else if (((Math.abs(input - x)) >= lastdiff1) && (test == false)) {
  56.                                 System.out.println("Colder or the same as last time, however.....");
  57.                         } else if (test == false) {
  58.                                 System.out.println("Warmer, however......");
  59.                         }
  60.                         test = false;
  61.                         lastdiff1 = Math.abs(input - x);
  62.                 } while (input != x);
  63.         }
  64.  
  65.         public static void Player2(boolean test, int x, int d, int f, int lastdiff1, int lastdiff2, int input1, int input2, int score1, int score2, int y, String finish) {
  66.                 do {
  67.                         if (test == true) {
  68.                                 System.out.println("I'm thinking of an a integer between 0 and " + d + " inclusively......");
  69.                                 input1 = readInteger("Player 1, please enter an integer between 0 and " + d + ", inclusivley: ");
  70.                                 score1++;
  71.                         } else if (test == false) {
  72.                                 input1 = readInteger("Player 1, please enter another integer between 0 and " + d + ", inclusivley: ");
  73.                                 score1++;
  74.                         }
  75.                         if ((input1 > d) || (input1 < 0)) {
  76.                                 do {
  77.                                         input1 = readInteger("Very funny, I'll ignore that. Player 1, guess an integer between 0 and " + d + " inclusively: ");
  78.                                 } while ((input1 > d) || (input1 < 0));
  79.                         }
  80.                         if (input1 == x) {
  81.                                 System.out.println("Player 1 is right! Your number is: " + x + ". You did it in " + score1 + " tries!");
  82.                         } else if (((Math.abs(input1 - x)) >= lastdiff1) && (test == false)) {
  83.                                 System.out.println("Player 1. That was colder or the same as your last guess...");
  84.                         } else if (test == false) {
  85.                                 System.out.println("Player 1. That was Warmer than your last guess!");
  86.                         }
  87.                         lastdiff1 = Math.abs(input1 - x);
  88.                         if (test == true) {
  89.                                 System.out.println("I'm thinking of a different integer for player 2 between 0 and " + d + " inclusively.....");
  90.                                 input2 = readInteger("Player 2, please enter an integer between 0 and " + d + ", inclusivley: ");
  91.                                 score2++;
  92.                         } else if (test == false) {
  93.                                 input2 = readInteger("Player 2, please enter another integer between 0 and " + d + ", inclusivley: ");
  94.                                 score2++;
  95.                         }
  96.                         if ((input2 > d) || (input1 < 0)) {
  97.                                 do {
  98.                                         input2 = readInteger("Very funny, I'll ignore that. Player 2, guess an integer between 0 and " + d + " inclusively: ");
  99.                                 } while (input2 > d);
  100.                         }
  101.                         if (input2 == y) {
  102.                                 System.out.println("Player 2 is is right! Your number is: " + y + ". You did it in " + score1 + " tries!");
  103.                         }
  104.                         if ((input1 == x) && (input2 == y)) {
  105.                                 System.out.println("Players 1 and 2 drew!");
  106.                         } else if (((Math.abs(input2 - y)) >= lastdiff2) && (test == false)) {
  107.                                 System.out.println("Player 2. That was colder or the same as last your last guess.....");
  108.                         } else if (test == false) {
  109.                                 System.out.println("Player 2. That was warmer than last your last guess!");
  110.                         }
  111.                         test = false;
  112.                         lastdiff2 = Math.abs(input2 - y);
  113.                 } while ((input1 != x) && (input2 != y));
  114.                 Win(finish, f, input2, y, input1, x);
  115.         }
  116.  
  117.         public static void Win(String finish, int f, int input2, int y, int input1, int x) {
  118.                 if ((input2 != y) && (input1 == x)) {
  119.                         System.out.println("Player 1 wins overall!");
  120.                         System.out.println("Player 2's number was "+y+"!");
  121.                 }
  122.                 if ((input2 == y) && (input1 != x)) {
  123.                         System.out.println("Player 2 wins overall!");
  124.                         System.out.println("Player 1's number was "+x+"!");
  125.                 }
  126.                 finish = readLine("Would you like to play again? Enter yes or no: ");
  127.                 if ((false == finish.equals("yes")) && (false == finish.equals("no"))) {
  128.                         do {
  129.                                 finish = readLine("Sorry, I didn't get that. Would you like to play again? Enter yes or no without any typos: ");
  130.                         } while ((false == finish.equals("yes"))
  131.                                         && (false == finish.equals("no")));
  132.                 }
  133.                 if (finish.equals("yes")) {
  134.                         main(null);
  135.                 } else if (finish.equals("no")) {
  136.                         System.out.println("Goodbye! I hope you enjoyed the Number Guessing game by Greg Cawthorne!");
  137.                 }
  138.         }
  139.  
  140.         public static void main(String[] args) {
  141.                 int lastdiff1 = 9999;
  142.                 int lastdiff2 = 9999;
  143.                 double rand1 = Math.random();
  144.                 double rand2 = Math.random();
  145.                 boolean test = true;
  146.                 int score1 = 0;
  147.                 int score2 = 0;
  148.             int d = 9;
  149.                 System.out.println("Welcome to the Number Guessing game by Greg Cawthorne!:");
  150.                 f = readInteger("Please enter the number of players (1 or 2): ");
  151.                 while ((f < 1) || (f > 2)){
  152.                         f = readInteger("Please enter the number of players as an integer 1 or 2: ");
  153.                 }
  154.                 System.out.println("Please choose an option:");
  155.                 System.out.println("Press 0 for: Instructions");
  156.                 System.out.println("Press 1 for: Easy difficulty   (upto 10)");
  157.                 System.out.println("Press 2 for: Medium difficulty (upto 100)");
  158.                 System.out.println("Press 3 for: Hard   difficulty (upto 1000)");
  159.                 do {
  160.                         e = readInteger("Please enter an integer 0, 1, 2 or 3: ");
  161.                 } while ((e > 3.1) || (e < 0));
  162.                 if (e == 3) {
  163.                         d = 1000;
  164.                 } else if (e == 2) {
  165.                         d = 100;
  166.                 } else if (e == 1) {
  167.                         d = 10;
  168.                 } else if (e == 0) {
  169.                         System.out.println("Instructions: The aim of the Random Number game is to guess a random number in the least amount of guesses possible.\nFirst you will be asked to select a difficulty by pressing 1 (which will make the random number between 0 and 10 inclusive), 2 (between 0 and 100), or 3 (between 0 and 1000).\nThen you will be asked to make a guess (must be an integer) between your 0 and your selected difficulty number.\nOn your second guess it will tell you if you are warmer (nearer) or colder (further away) from the random number compared to your last valid guess.\nIf you select more than one player you will all be guessing different numbers!\nEnjoy!");
  170.                 }
  171.                 if (e == 0) {
  172.                         System.out.println("Please choose a difficulty:");
  173.                         System.out.println("Press 1 for: Easy   (upto 10)");
  174.                         System.out.println("Press 2 for: Medium (upto 100)");
  175.                         System.out.println("Press 3 for: Hard   (upto 1000)");
  176.                         do {
  177.                                 e = readInteger("Please enter an integer 1, 2 or 3: ");
  178.                         } while ((e > 3.1) || (e < 0));
  179.                 }
  180.                 if (e == 3) {
  181.                         d = 1000;
  182.                 } else if (e == 2) {
  183.                         d = 100;
  184.                 } else if (e == 1) {
  185.                         d = 10;
  186.                 }
  187.                 int x = (int) (rand1 * d);
  188.                 int y = (int) (rand2 * d);
  189.                 if (f == 1) {
  190.                         Player1(finish,x, f, score1, lastdiff1, input, d, e, test, input2, y, input1);
  191.                 }
  192.                 if (f != 1) {
  193.                         Player2(test, x, d, f, lastdiff1, lastdiff2, input1, input2, score1, score2, y, finish);
  194.                 }
  195.         }
  196. }
clone this paste RAW Paste Data