/* * ;;;Created by manish */ import java.util.*; public class NumberCruncher { static int score, rounds, lastround, target; static int[] nums = new int[6]; static int m; public static void main(String s[]) { System.out.println("Welcome to Number Cruncher! "); choice(); } static void choice() { System.out.println("Main Menu:- "); System.out.println("| 1. Play | 2. Rules | 3. Quit "); int choice = new Scanner(System.in).nextInt(); if (choice == 1) { first(); } else if (choice == 2) { rules(); } else if (choice == 3) { return; } else { System.out.println("Invalid Choice. "); choice(); } } static void rules() { 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. "); 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. "); System.out.println("Enter anything to return back to main menu."); String menu = new Scanner(System.in).nextLine(); choice(); } static void first() { score = 0; lastround = 0; rounds = 0; System.out.println(" Get ready to begin. Score as much as you can in 12 rounds!Get ready..."); System.out.println("Enter anything to continue."); String okj = new Scanner(System.in).nextLine(); nextq(); } public static void nextq() { m = 0; int x[] = {10, 25, 50, 75, 100, 6, 7, 8, 9, 10}; nums[0] = (int) (Math.random() * 7) + 4; nums[1] = (int) (Math.random() * 9) + 2; nums[2] = (int) (Math.random() * 6) + 5; nums[3] = (int) x[(int) (Math.random() * 5)]; nums[4] = (int) x[(int) (Math.random() * 10)]; nums[5] = (int) x[(int) (Math.random() * 3) + 2]; target = (int) (Math.random() * 900) + 100; System.out.println(" Round " + (rounds + 1) + ": "); System.out.println(" Your Numbers Are: " + nums[5] + "," + nums[4] + "," + nums[3] + "," + nums[0] + "," + nums[2] + "," + nums[1] + "." + " Your Target Is: " + target); another(); } static void another() { String s = new Scanner(System.in).nextLine(); s = s.replace(" ", ""); if (s.compareTo("S") == 0) { System.out.println("You decided to surrender and so you receive " + m + " points. "); score = score + m; System.out.println(" Current score: " + score); lastround = m; rounds += 1; if (rounds == 12) { System.out.print(" You have finished the game and won with the score " + score + "."); } else { System.out.println("Enter anything to continue."); String ax = new Scanner(System.in).nextLine(); nextq(); } } else if (verify(check(s)) == true) { double a = eval(s); if (a % 1 != 0) { System.out.println("Your expression evaluates to " + a + ". Decimal answers are not allowed. Try again, or type S to submit."); another(); } else if (Math.abs(a - target) <= 4 && Math.abs(a - target) != 0 && 5 - (int) Math.abs(a - target) < m) { System.out.println(" Your expression results to " + (int) a + "( " + (int) Math.abs(a - target) + " away ). But you are already " + (5 - m) + " away."); System.out.println(" Try again for a better score, or S to submit."); another(); } else if (Math.abs(a - target) <= 4 && Math.abs(a - target) != 0) { m = 5 - (int) Math.abs(a - target); 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. "); System.out.println(" Press S to submit, or try again."); another(); } else if (Math.abs(a - target) == 0) { score += 5; if (lastround >= 5) { lastround += 1; score = score + lastround - 5; } else { lastround = 5; } if (lastround >= 6) { System.out.println(" For " + (lastround - 5 + 1) + " consecutive target hits, you receive " + (lastround) + " points in this round. "); System.out.println(" Current score: " + score); System.out.println("Enter anything to continue."); } ++rounds; if (lastround == 5) { System.out.println("You guessed target number (" + target + ") correctly and get 5 points!!! "); System.out.println(" Current score: " + score); System.out.println(" Enter anything to continue."); } String anyt = new Scanner(System.in).nextLine(); if (rounds != 12) { nextq(); } else { System.out.print(" You have finished the game and won with the score " + score + "."); } } else { System.out.println(" Your expression evaluates to " + (int) a + " which is out of range. "); System.out.println(" Try again."); another(); } } else { System.out.println(" Invalid numbers or letters used. Try again."); another(); } } static String check(String x) { x = x.replace("(", "#"); x = x.replace(")", "#"); x = x.replace("+", "#"); x = x.replace("-", "#"); x = x.replace("*", "#"); x = x.replace("/", "#"); x = x.replace("^", "#"); return "#" + x + "#"; } static boolean verify(String x) { for (int i = 0; i < nums.length; i++) { if (x.contains(String.valueOf(nums[i]) + "#")) { x = x.replaceFirst("#" + String.valueOf(nums[i]) + "#", "#"); } } for (int i = 0; i < x.length(); i++) { if (x.charAt(i) != '#') { return false; } } return true; } public static double eval(final String str) { return new Object() { int pos = -1, ch; void nextChar() { ch = (++pos < str.length()) ? str.charAt(pos) : -1; } boolean eat(int charToEat) { while (ch == ' ') { nextChar(); } if (ch == charToEat) { nextChar(); return true; } return false; } double parse() { nextChar(); double x = parseExpression(); if (pos < str.length()) { throw new RuntimeException("Unexpected: " + (char) ch); } return x; } double parseExpression() { double x = parseTerm(); for (;;) { if (eat('+')) { x += parseTerm(); // addition } else if (eat('-')) { x -= parseTerm(); // subtraction } else { return x; } } } double parseTerm() { double x = parseFactor(); for (;;) { if (eat('*')) { x *= parseFactor(); // multiplication } else if (eat('/')) { x /= parseFactor(); // division } else { return x; } } } double parseFactor() { if (eat('+')) { return parseFactor(); // unary plus } if (eat('-')) { return -parseFactor(); // unary minus } double x; int startPos = this.pos; if (eat('(')) { // parentheses x = parseExpression(); eat(')'); } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers while ((ch >= '0' && ch <= '9') || ch == '.') { nextChar(); } x = Double.parseDouble(str.substring(startPos, this.pos)); } else if (ch >= 'a' && ch <= 'z') { // functions while (ch >= 'a' && ch <= 'z') { nextChar(); } String func = str.substring(startPos, this.pos); x = parseFactor(); } else { throw new RuntimeException("Unexpected: " + (char) ch); } if (eat('^')) { x = Math.pow(x, parseFactor()); // exponentiation } return x; } }.parse(); } }