|
| 1 | +import java.util.*; |
| 2 | +import java.io.*; |
| 3 | +public class Nerdle { |
| 4 | + public static int calculate(int a, int b, char op) { |
| 5 | + if (op == '/' && (b == 0 || a % b != 0)) { |
| 6 | + return -1; |
| 7 | + } |
| 8 | + int c; |
| 9 | + switch (op) { |
| 10 | + case '+': |
| 11 | + c = a + b; |
| 12 | + break; |
| 13 | + case '-': |
| 14 | + c = a - b; |
| 15 | + break; |
| 16 | + case '*': |
| 17 | + c = a * b; |
| 18 | + break; |
| 19 | + default: |
| 20 | + c = a / b; |
| 21 | + } |
| 22 | + if (c > 9 || c < 0) { |
| 23 | + return -1; |
| 24 | + } |
| 25 | + return c; |
| 26 | + } |
| 27 | + public static void main(String[] args) throws IOException { |
| 28 | + System.out.println(""" |
| 29 | + This is a Java implementation of Nerdle in USTC LUG Carnival from Java_Herobrine. |
| 30 | + Y | y for help. |
| 31 | + """); |
| 32 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 33 | + String str = br.readLine(); |
| 34 | + if (str.equals("Y") || str.equals("y")) { |
| 35 | + System.out.println( |
| 36 | + """ |
| 37 | + In Nerdle, Mr.Kali will generate an equation like this: |
| 38 | + a opt b = c |
| 39 | + a, b and c are 3 integers from 0 to 9, and opt is one of + - *(multiplication) and /(division) |
| 40 | + Evidently, this equation can be represented as a string containing 5 characters. |
| 41 | + You are going to guess what the equation is. |
| 42 | + Each time, you are supposed to input an equation, and Mr.Kali will tell you the result. |
| 43 | + -If your equation is illegal (a opt b isn't c, a b or c is less than 0 or greater than 9 or not a number at all, = is replaced by other characters, opt isn't + - * or /, |
| 44 | + he will tell \"Invalid Input\" . |
| 45 | + -Otherwise, he will tell you the total time consumption, and the result as a string of 5 characters. |
| 46 | + If you guess the i-th character right, the i-th character of result is O |
| 47 | + If you guess the i-th character wrong, but this character appeared in other position(s), the i-th character of result is X |
| 48 | + Otherwise, the i-th character of result is - |
| 49 | + You have 6 chances to guess the equation |
| 50 | + For example, if the generated equation is 9-7=2, and you guess it's 2+7=9, the result is |
| 51 | + X-OOX |
| 52 | + If you guess it's 114514+1919810=2147483647, the result is |
| 53 | + Invalid Input |
| 54 | + """); |
| 55 | + } |
| 56 | + Random random = new Random(System.currentTimeMillis()); |
| 57 | + char opt[] = { '+', '-', '*', '/' }; |
| 58 | + char op = opt[random.nextInt(0, 4)]; |
| 59 | + int a = -1, b = -1, c = -1; |
| 60 | + while (c == -1) { |
| 61 | + a = random.nextInt(0, 10); |
| 62 | + b = random.nextInt(0, 10); |
| 63 | + c = calculate(a, b, op); |
| 64 | + } |
| 65 | + System.out.println("Game Start!"); |
| 66 | + long time = System.currentTimeMillis(); |
| 67 | + for (int i = 1; i <= 6; ++i) { |
| 68 | + int aI, bI, cI; |
| 69 | + char optI; |
| 70 | + String input = br.readLine(); |
| 71 | + System.out.println("Input: " + input); |
| 72 | + if (input.length() != 5) { |
| 73 | + System.out.println("Invalid Input"); |
| 74 | + --i; |
| 75 | + continue; |
| 76 | + } |
| 77 | + if (!Character.isDigit(input.charAt(0))) { |
| 78 | + System.out.println("Invalid Input"); |
| 79 | + --i; |
| 80 | + continue; |
| 81 | + } else { |
| 82 | + aI = input.charAt(0) ^ 48; |
| 83 | + } |
| 84 | + if (!Character.isDigit(input.charAt(2))) { |
| 85 | + System.out.println("Invalid Input"); |
| 86 | + --i; |
| 87 | + continue; |
| 88 | + } else { |
| 89 | + bI = input.charAt(2) ^ 48; |
| 90 | + } |
| 91 | + if (!Character.isDigit(input.charAt(4))) { |
| 92 | + System.out.println("Invalid Input"); |
| 93 | + --i; |
| 94 | + continue; |
| 95 | + } else { |
| 96 | + cI = input.charAt(4) ^ 48; |
| 97 | + } |
| 98 | + optI = input.charAt(1); |
| 99 | + if (optI != '+' && optI != '-' && optI != '*' && optI != '/') { |
| 100 | + System.out.println("Invalid Input"); |
| 101 | + --i; |
| 102 | + continue; |
| 103 | + } |
| 104 | + if (input.charAt(3) != '=') { |
| 105 | + --i; |
| 106 | + System.out.println("Invalid Input"); |
| 107 | + continue; |
| 108 | + } |
| 109 | + int calc = calculate(aI, bI, optI); |
| 110 | + if (calc == -1 || calc != cI) { |
| 111 | + System.out.println("Invalid Input"); |
| 112 | + --i; |
| 113 | + continue; |
| 114 | + } |
| 115 | + long t1 = System.currentTimeMillis() - time; |
| 116 | + System.out.println("Time consumption=" + t1 / 1000 + "." + t1 % 1000 + "s"); |
| 117 | + if (aI == a) { |
| 118 | + System.out.print('O'); |
| 119 | + } else if (aI == b || aI == c) { |
| 120 | + System.out.print('X'); |
| 121 | + } else { |
| 122 | + System.out.print('-'); |
| 123 | + } |
| 124 | + if (optI == op) { |
| 125 | + System.out.print('O'); |
| 126 | + } else { |
| 127 | + System.out.print('-'); |
| 128 | + } |
| 129 | + if (bI == b) { |
| 130 | + System.out.print('O'); |
| 131 | + } else if (bI == a || bI == c) { |
| 132 | + System.out.print('X'); |
| 133 | + } else { |
| 134 | + System.out.print('-'); |
| 135 | + } |
| 136 | + System.out.print('O'); |
| 137 | + if (cI == c) { |
| 138 | + System.out.println('O'); |
| 139 | + } else if (cI == a || cI == b) { |
| 140 | + System.out.println('X'); |
| 141 | + } else { |
| 142 | + System.out.println('-'); |
| 143 | + } |
| 144 | + if (aI == a && bI == b && cI == c && optI == op) { |
| 145 | + System.out.println("Succeeded with " + (i) + " Attempts!"); |
| 146 | + return; |
| 147 | + } |
| 148 | + } |
| 149 | + System.out.println("Failed! The answer is " + a + op + b + '=' + c); |
| 150 | + } |
| 151 | +} |
0 commit comments