manish

Number Cruncher by manish

Aug 30th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.61 KB | None | 0 0
  1. /*
  2.  * ;;;Created by manish
  3.  */
  4.  
  5. import java.util.*;
  6.  
  7. public class NumberCruncher {
  8.  
  9.     static int score, rounds, lastround, target;
  10.     static int[] nums = new int[6];
  11.     static int m;
  12.  
  13.     public static void main(String s[]) {
  14.         System.out.println("Welcome to Number Cruncher! ");
  15.  
  16.         choice();
  17.     }
  18.  
  19.     static void choice() {
  20.         System.out.println("Main Menu:- ");
  21.         System.out.println("|  1. Play  |  2. Rules  |  3. Quit ");
  22.         int choice = new Scanner(System.in).nextInt();
  23.         if (choice == 1) {
  24.             first();
  25.         } else if (choice == 2) {
  26.             rules();
  27.         } else if (choice == 3) {
  28.             return;
  29.         } else {
  30.             System.out.println("Invalid Choice. ");
  31.             choice();
  32.         }
  33.     }
  34.  
  35.     static void rules() {
  36.         System.out.println("Each time, a series of target numbers will given, along with 6 smaller numbers. The closer you come to the number, the more points you get. ");
  37.         System.out.println("  Target number: 5 points , 1 away: 4 points , 2 away: 3 points , 3 away: 2 points , 4 away: 1 point :: Score as much as you can in 12 rounds. ");
  38.         System.out.println("Enter anything to return back to main menu.");
  39.         String menu = new Scanner(System.in).nextLine();
  40.         choice();
  41.     }
  42.  
  43.     static void first() {
  44.         score = 0;
  45.         lastround = 0;
  46.         rounds = 0;
  47.         System.out.println(" Get ready to begin. Score as much as you can in 12 rounds!Get ready...");
  48.         System.out.println("Enter anything to continue.");
  49.         String okj = new Scanner(System.in).nextLine();
  50.         nextq();
  51.     }
  52.  
  53.     public static void nextq() {
  54.         m = 0;
  55.         int x[] = {10, 25, 50, 75, 100, 6, 7, 8, 9, 10};
  56.  
  57.         nums[0] = (int) (Math.random() * 7) + 4;
  58.         nums[1] = (int) (Math.random() * 9) + 2;
  59.         nums[2] = (int) (Math.random() * 6) + 5;
  60.         nums[3] = (int) x[(int) (Math.random() * 5)];
  61.         nums[4] = (int) x[(int) (Math.random() * 10)];
  62.         nums[5] = (int) x[(int) (Math.random() * 3) + 2];
  63.  
  64.         target = (int) (Math.random() * 900) + 100;
  65.         System.out.println("           Round " + (rounds + 1) + ":  ");
  66.         System.out.println("   Your Numbers Are: "
  67.                 + nums[5] + "," + nums[4] + "," + nums[3] + ","
  68.                 + nums[0] + "," + nums[2] + "," + nums[1] + "."
  69.                 + " Your Target Is: " + target);
  70.         another();
  71.  
  72.     }
  73.  
  74.     static void another() {
  75.         String s = new Scanner(System.in).nextLine();
  76.         s = s.replace(" ", "");
  77.         if (s.compareTo("S") == 0) {
  78.             System.out.println("You decided to surrender and so you receive " + m + " points. ");
  79.             score = score + m;
  80.             System.out.println("            Current score: " + score);
  81.             lastround = m;
  82.             rounds += 1;
  83.             if (rounds == 12) {
  84.                 System.out.print(" You have finished the game and won with the score " + score + ".");
  85.             } else {
  86.                 System.out.println("Enter anything to continue.");
  87.                 String ax = new Scanner(System.in).nextLine();
  88.                 nextq();
  89.             }
  90.         } else if (verify(check(s)) == true) {
  91.             double a = eval(s);
  92.             if (a % 1 != 0) {
  93.                 System.out.println("Your expression evaluates to " + a + ". Decimal answers are not allowed. Try again, or type S to submit.");
  94.                 another();
  95.             } else if (Math.abs(a - target) <= 4 && Math.abs(a - target) != 0 && 5 - (int) Math.abs(a - target) < m) {
  96.                 System.out.println(" Your expression results to " + (int) a + "( " + (int) Math.abs(a - target) + " away ). But you are already " + (5 - m) + " away.");
  97.                 System.out.println(" Try again for a better score, or S to submit.");
  98.                 another();
  99.             } else if (Math.abs(a - target) <= 4 && Math.abs(a - target) != 0) {
  100.                 m = 5 - (int) Math.abs(a - target);
  101.                 System.out.println(" Your expression results to " + (int) a + " ( " + (int) Math.abs(a - target) + " number(s) away from the target number), and so you get at least " + m + " points. ");
  102.                 System.out.println(" Press S to submit, or try again.");
  103.                 another();
  104.             } else if (Math.abs(a - target) == 0) {
  105.                 score += 5;
  106.                 if (lastround >= 5) {
  107.                     lastround += 1;
  108.                     score = score + lastround - 5;
  109.                 } else {
  110.                     lastround = 5;
  111.                 }
  112.                 if (lastround >= 6) {
  113.                     System.out.println(" For " + (lastround - 5 + 1) + " consecutive target hits, you receive " + (lastround) + " points in this round. ");
  114.                     System.out.println("                Current score: " + score);
  115.                     System.out.println("Enter anything to continue.");
  116.                 }
  117.  
  118.                 ++rounds;
  119.                 if (lastround == 5) {
  120.                     System.out.println("You guessed target number (" + target + ") correctly and get 5 points!!! ");
  121.                     System.out.println("              Current score: " + score);
  122.                     System.out.println(" Enter anything to continue.");
  123.                 }
  124.                 String anyt = new Scanner(System.in).nextLine();
  125.                 if (rounds != 12) {
  126.                     nextq();
  127.                 } else {
  128.                     System.out.print(" You have finished the game and won with the score " + score + ".");
  129.                 }
  130.             } else {
  131.                 System.out.println(" Your expression evaluates to " + (int) a + " which is out of range. ");
  132.                 System.out.println(" Try again.");
  133.                 another();
  134.             }
  135.         } else {
  136.             System.out.println(" Invalid numbers or letters used. Try again.");
  137.             another();
  138.         }
  139.     }
  140.  
  141.     static String check(String x) {
  142.  
  143.         x = x.replace("(", "#");
  144.         x = x.replace(")", "#");
  145.         x = x.replace("+", "#");
  146.         x = x.replace("-", "#");
  147.         x = x.replace("*", "#");
  148.         x = x.replace("/", "#");
  149.         x = x.replace("^", "#");
  150.  
  151.         return "#" + x + "#";
  152.     }
  153.  
  154.     static boolean verify(String x) {
  155.         for (int i = 0; i < nums.length; i++) {
  156.             if (x.contains(String.valueOf(nums[i]) + "#")) {
  157.                 x = x.replaceFirst("#" + String.valueOf(nums[i]) + "#", "#");
  158.             }
  159.         }
  160.         for (int i = 0; i < x.length(); i++) {
  161.             if (x.charAt(i) != '#') {
  162.                 return false;
  163.             }
  164.         }
  165.         return true;
  166.     }
  167.  
  168.     public static double eval(final String str) {
  169.         return new Object() {
  170.             int pos = -1, ch;
  171.  
  172.             void nextChar() {
  173.                 ch = (++pos < str.length()) ? str.charAt(pos) : -1;
  174.             }
  175.  
  176.             boolean eat(int charToEat) {
  177.                 while (ch == ' ') {
  178.                     nextChar();
  179.                 }
  180.                 if (ch == charToEat) {
  181.                     nextChar();
  182.                     return true;
  183.                 }
  184.                 return false;
  185.             }
  186.  
  187.             double parse() {
  188.                 nextChar();
  189.                 double x = parseExpression();
  190.                 if (pos < str.length()) {
  191.                     throw new RuntimeException("Unexpected: " + (char) ch);
  192.                 }
  193.                 return x;
  194.             }
  195.  
  196.             double parseExpression() {
  197.                 double x = parseTerm();
  198.                 for (;;) {
  199.                     if (eat('+')) {
  200.                         x += parseTerm(); // addition
  201.                     } else if (eat('-')) {
  202.                         x -= parseTerm(); // subtraction
  203.                     } else {
  204.                         return x;
  205.                     }
  206.                 }
  207.             }
  208.  
  209.             double parseTerm() {
  210.                 double x = parseFactor();
  211.                 for (;;) {
  212.                     if (eat('*')) {
  213.                         x *= parseFactor(); // multiplication
  214.                     } else if (eat('/')) {
  215.                         x /= parseFactor(); // division
  216.                     } else {
  217.                         return x;
  218.                     }
  219.                 }
  220.             }
  221.  
  222.             double parseFactor() {
  223.                 if (eat('+')) {
  224.                     return parseFactor(); // unary plus
  225.                 }
  226.                 if (eat('-')) {
  227.                     return -parseFactor(); // unary minus
  228.                 }
  229.                 double x;
  230.                 int startPos = this.pos;
  231.                 if (eat('(')) { // parentheses
  232.                     x = parseExpression();
  233.                     eat(')');
  234.                 } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
  235.                     while ((ch >= '0' && ch <= '9') || ch == '.') {
  236.                         nextChar();
  237.                     }
  238.                     x = Double.parseDouble(str.substring(startPos, this.pos));
  239.                 } else if (ch >= 'a' && ch <= 'z') { // functions
  240.                     while (ch >= 'a' && ch <= 'z') {
  241.                         nextChar();
  242.                     }
  243.                     String func = str.substring(startPos, this.pos);
  244.                     x = parseFactor();
  245.  
  246.                 } else {
  247.                     throw new RuntimeException("Unexpected: " + (char) ch);
  248.                 }
  249.  
  250.                 if (eat('^')) {
  251.                     x = Math.pow(x, parseFactor()); // exponentiation
  252.                 }
  253.                 return x;
  254.             }
  255.         }.parse();
  256.     }
  257. }
Add Comment
Please, Sign In to add comment