猜数字
规则:系统生成一个不重复的四位数1-9之间,然后开始猜,A表示数字正确位置也对,B代表数字正确位置不对。
let state = true;
let temp = 0;
let count = 10;
function getRandomNum(min, max = 0) {
if (min > max) {
[min, max] = [max, min];
}
return parseInt(((max - min + 1)) * Math.random() + min);
};
while (state) {
//随机生成电脑数字
// let num1 = [getRandomNum(Num[0], Num[1]), getRandomNum(Num[0], Num[1]), getRandomNum(Num[0], Num[1]), getRandomNum(Num[0], Num[1])];
let num1 = [getRandomNum(1, 9), getRandomNum(1, 9), getRandomNum(1, 9), getRandomNum(1, 9)];
// let num1 = [getRandomNum(inputNum, inputNum1), getRandomNum(inputNum, inputNum1), getRandomNum(inputNum, inputNum1), getRandomNum(inputNum, inputNum1)];
let num2 = new Set(num1);
if (num2.size == 4) {
num1 = [...num2];
console.log(num1);
state = false;
temp = num1;
}
}
state=true;
console.log();
console.log();
console.log();
console.log();
console.log();
console.log();
console.log();
console.log();
console.log();
console.log();
console.log();
console.log(`系统已经生成一个四位不重复的1-9数字,10次机会猜中有奖励`);
while(state){
let countB = 0;
let countA = 0;
count--;
console.log(`请输入4位不重复的数字`);
let inputNum1 = rs.question();
let Num1 = inputNum1.split("");
for (i = 0; i < temp.length; i++) {
for (j = 0; j < Num1.length; j++) {
if (temp[i] == Num1[j]) {
if (i == j) {
countA++;
} else {
countB++;
}
if ((i == temp.length - 1)&&(j=temp.length-1)) {
console.log(`${countA}A ${countB}B`);
if (countA != 4) {
if(count<=0){
console.log(`10次机会已用完!瓜娃子!!!正确数字${temp}`);
state=false;
}else{
console.log(`您还有${count}次机会`);
}
} else {
console.log(`恭喜你!猜对了!!真棒!!!`);
state=false;
}
}
}else if((i==temp.length-1)&&(j==temp.length-1)){
console.log(`${countA}A ${countB}B`);
if(count==0){
console.log(`10次机会已用完!瓜娃子!!!正确数字${temp}`);
state=false;
}else{
console.log(`您还有${count}次机会`);
}
}
}
}
}