import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class GuseeLetter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int level;
System.out.println("GuessintGame>欢迎尝试猜字母游戏!");
do {
System.out.print("GuessintGame>请输入游戏级别(5,7,9)?");
level = scanner.nextInt();
if (level == 5 || level == 7 || level == 9) { // 选择游戏级别
break;
}
} while (true);
// int level = 5;
char[] ans = answer(level); // 生成n个元素的字符串
System.out.println("GuessintGame>游戏开始,请输入你所猜的"+level+"个字母序列:(exit-退出)");
String input;
int count = 0;
Scanner scan = new Scanner(System.in);
int score = level * 100; // 每一个字母一百分
do {
input = scan.nextLine().trim().toUpperCase();// 接受输入的字符串
char[] inputArr = input.toCharArray(); // 将字符串转换为字符数组
int[] check = compare(inputArr, ans);
count++; // 计算次数
int total = score - count * 10; // 计算总分
if (input.equals("EXIT")) {
System.out.println("GuessintGame>谢谢你的尝试,再见!");
//System.out.println("GuessintGame>正确答案是:" + Arrays.toString(ans));
break;
}
// System.out.println(Arrays.toString(ans));
if (check[0] == level && check[1] == level) {
System.out.println("GuessintGame>恭喜你猜对了!你的得分是" + total);
break;
}
System.out.println("GuessintGame>猜对了" + check[0] + "个字符,其中"
+ check[1] + "个位置正确!(总次数=" + count + ",exit-退出)");
} while (true);
}
public static char[] answer(int n) {
char[] arr = new char[n];
Random random = new Random();
char[] allLetters = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', };
boolean[] flag = new boolean[allLetters.length]; // 默认false
int j = 0;
do {
int num = random.nextInt(allLetters.length);
if (flag[num]) {
continue;
}
arr[j++] = allLetters[num];
flag[num] = true;
} while (j != n);
return arr;
}
public static int[] compare(char[] a, char[] b) {
int[] arr = new int[2];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
if (a[i] == b[j]) {
arr[0]++;
if (i == j) {
arr[1]++;
}
break;
} else {
continue;
}
}
}
return arr;
}
}