import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args){
int count = 0;
char[] input = null;
int[] result = new int[2];
Scanner sc = new Scanner(System.in);
System.out.println("猜字母游戏:");
char[] chs = generate();
System.out.println("游戏开始输入5个字母:");
while(true){
String inputer = sc.next().trim().toUpperCase();
if("EXIT".equalsIgnoreCase(inputer)){
System.out.println("掰掰");
break;
}
input = inputer.toCharArray();
result = check(chs, input);
if(result[0]==chs.length){
int score = 100*chs.length-count*10;
System.out.println("猜对得分:"+score);
break;
}else{
count++;
System.out.println("猜对了"+result[1]+"个字符,其中"+result[0]+"个字符的位置正确!(总次数="+count+",exit");
}
}
sc.close();
}
public static char[] generate(){
char[] letters = {'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[] flags = new boolean[letters.length];
char[] chs = new char[5];
for (int i=0;i<chs.length;i++){
int index;
do{
index = (int)(Math.random()*(letters.length));
}while(flags[index]);
chs[i] = letters[index];
flags[index] = true;
}
StringBuffer sb = new StringBuffer();
for(int i =0;i<chs.length;i++){
sb.append(chs[i]);
}
String newchs = sb.toString();
System.out.println("newchs:"+newchs);
return chs;
}
public static int[] check(char[] chs,char[] input){
int[] result = new int[2];
for(int i=0;i<input.length;i++){
for(int j=0;j<chs.length;j++){
if(input[i] == chs[j]){
result[1]++;
if(i==j){
result[0]++;
}
break;
}
}
}
return result;
}
}