import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String random2;
int count = 1;
int numLength;
int keyLength;
Scanner scanner = new Scanner(System.in);
System.out.println("Please, enter the secret code's length:");
try {
numLength = scanner.nextInt();
if (numLength <= 0) {
System.out.println("Error: \" \" isn't a valid number.");
return;
}
} catch (InputMismatchException e) {
System.out.print("Error: \" \" isn't a valid number.");
return;
}
System.out.println("Input the number of possible symbols in the code:");
try {
keyLength = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.print("Error: \" \" isn't a valid number.");
return;
}
if (numLength > keyLength) {
System.out.println("Error: it's not possible to generate a code " +
"with a length of 6 with 5 unique symbols.");
return;
} else if (keyLength > 36) {
System.out.println("Error: maximum number of possible symbols in " +
"the code is 36 (0-9, a-z).");
return;
}
do {
random2 = realNum(numLength,keyLength);
}while (random2 == null);
while (true) {
System.out.println("Turn " + count + ":");
if (givenNumber(random2)) {
System.out.println("Congratulations! You guessed the secret code.");
break;
}
count ++;
}
}
public static String realNum (int numLength,int keyLength) {
String KeyString = "0123456789abcdefghijklmnopqrstuvwxyz";
String keyString = KeyString.substring(0,keyLength);
boolean check = true;
String randomNum = null;
if (numLength < 10) {
while (check) {
//long guessNum = System.nanoTime();
randomNum = getRandomString(keyString,numLength);
char[] random1 = randomNum.toCharArray();
int i = 0;
int j;
int check1 = 0;
while (i < numLength - 1) {
int numericValue = Character.getNumericValue(random1[i]);
j = i + 1;
i ++;
while (j < numLength) {
int numericValue1 = Character.getNumericValue(random1[j]);
if (numericValue == numericValue1) {
check1 = 1;
}
j ++;
}
}
if ((check1 == 0)){
check = false;
}
}
char[] starNum = new char[numLength];
for (int i = 0; i < numLength; i ++) {
starNum[i] = '*';
}
if (keyLength > 11) {
System.out.println("The secret is prepared: " +
String.valueOf(starNum) + " (0-9, a-" + keyString.charAt(keyLength - 1) +").");
} else if (keyLength == 10) {
System.out.println("The secret is prepared: " +
String.valueOf(starNum) + " (0-9, a).");
} else {
System.out.println("The secret is prepared: " +
String.valueOf(starNum) + " (0-" + (keyLength - 1) + ").");
}
System.out.println("Okay, let's start a game!");
return randomNum;
} else {
System.out.println("error");
return null;
}
}
//获取指定位数的随机字符串(包含小写字母、大写字母、数字,0<length)
public static String getRandomString(String KeyString,int length) {
//随机字符串的随机字符库
StringBuilder sb = new StringBuilder();
int len = KeyString.length();
for (int i = 0; i < length; i++) {
sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));
}
return sb.toString();
}
public static boolean givenNumber (String rN) {
Scanner scanner = new Scanner(System.in);
String b = scanner.nextLine();
char[] aA = rN.toCharArray();
char[] bB = b.toCharArray();
int countCow = 0;
int countBull = 0;
for (int j = 0; j < rN.length(); j++) {
if (aA[j] == bB[j]) {
countBull++;
} else {
for (int i = 0; i < rN.length(); i++) {
if (aA[j] == bB[i]) {
countCow++;
}
}
}
}
if ((countBull != 0) && (countCow != 0)) {
if ((countBull == 1) && (countCow == 1)) {
System.out.println("Grade: " + countBull + " bull and "
+ countCow + " cow");
} else if (countBull == 1){
System.out.println("Grade: " + countBull + " bull and "
+ countCow + " cows");
} else if (countCow == 1) {
System.out.println("Grade: " + countBull + " bulls and "
+ countCow + " cow");
} else {
System.out.println("Grade: " + countBull + " bulls and "
+ countCow + " cows");
}
} else if (countBull != 0) {
if (countBull == 1) {
System.out.println("Grade: " + countBull + " bull");
} else {
System.out.println("Grade: " + countBull + " bulls");
}
} else if (countCow != 0) {
if (countCow == 1) {
System.out.println("Grade: " + countCow + " cow");
} else {
System.out.println("Grade: " + countCow + " cows");
}
} else {
System.out.println("Grade: None.");
}
boolean check = false;
if (countBull == rN.length()) {
check = true;
}
return check;
}
}
11-17
02-25
3821