我可以使此代码更高效/更短至 运行 吗?
Can i make this code more efficient / shorter to run?
所以我对 java 脚本和一般编码还很陌生。我制作一个 Wordle 算法只是为了好玩,以培养我的编码技能。在制作这个算法时,我意识到我将不得不重新创建 Wordle。所以我在网上寻找 java 脚本中的 wordle 娱乐,我发现所有这些我都看到了一个我不想拥有的缺陷。
我看到的缺陷是在 IF 语句中检查 wordle 答案中的字母。特别是当字母在单词中但不在正确的位置时。我看到的游戏有一个 IF 语句,看起来像这样。
IF (answerWord.includes(guessLetter[i])) {
guessLetter[i] = color.(yellow)
}
(这不是他们写的,但它是主要思想)
我主要关注 .includes
。这是行不通的,因为假设我们的猜测词是“同意”而答案是“准备好”。 “同意”中的 2 个 E 是黄色的,我们不希望这样,因为它们在“准备就绪”中只有 1 个 E。所以我们只希望“同意”中的第一个 E 是黄色而不是第二个 E.
所以我决定自己想办法解决这个问题,我想出了这个办法,而且效果很好。至少我很确定它是有效的,基于我测试过的许多词。
所以我的问题是,由于我正在制作一个算法,我将进行大量计算,这是我能写出的最有效的算法还是我可以改进它?
let guessWord = "agree"; // the first word thats inputed
let answerWord = "ready"; // the answer to the wordle
/*
'letterCheck' is an array that tells what condition each letter in 'startLetter' is, based on the answer word
2 = the letter is in the word and in the correct place (GREEN)
1 = the letter is in the word but not in the correct place (YELLOW)
0 = the letter in not in the word (GREY)
*/
var letterCheck = ["0", "0", "0", "0", "0"];
var guessLetter = ["A", "A", "A", "A", "A"]; // the separated letters of 'startword' in a array
var answerLetter = ["A", "A", "A", "A", "A"]; // the separated letters of 'answord' in a array
//adds the start word and the answer world to the arrays
for (var i = 0; i < 5; i++) {
guessLetter[i] = guessWord.substring(i, i + 1);
answerLetter[i] = answerWord.substring(i, i + 1);
}
console.log(guessLetter);
console.log(letterCheck);
console.log(answerLetter);
//this loops goes though every letter one by one
for (var i = 0; i < 5; i++) {
//checks if the letter is in the right spot
if (guessLetter[i] == answerLetter[i]) {
letterCheck[i] = "2";
console.log(guessLetter[i]);
console.log(letterCheck);
} else if (answerWord.includes(guessLetter[i])) {
//checks if there is more than one letter in start word or its the first letter
if (guessWord.split(guessLetter[i]).length - 1 == 1 || i == 0) {
letterCheck[i] = "1";
console.log(guessLetter[i]);
console.log(letterCheck);
//checks if the the amount of same letters in start words is equel to the amount of same letters in answer word
} else if (guessWord.split(guessLetter[i]).length - 1 == answerWord.split(guessLetter[i]).length - 1) {
letterCheck[i] = "1";
console.log(guessLetter[i]);
console.log(letterCheck);
//opposite of above
} else if (guessWord.split(guessLetter[i]).length - 1 != answerWord.split(guessLetter[i]).length - 1) {
letterCheck[i] = "1";
console.log(guessLetter[i]);
console.log(letterCheck);
// checks if any of the letters infront of it are the same as it
for (var j = 0; j < i; j++) {
if (guessLetter[i] == guessLetter[j]) {
letterCheck[i] = "0";
console.log(guessLetter[i]);
console.log(letterCheck);
}
}
}
} else {
letterCheck[i] = "0";
console.log(guessLetter[i]);
console.log(letterCheck);
}
}
console.log(guessLetter);
console.log(letterCheck);
console.log(answerLetter);
(稍后我将删除 console.log
,他们只是帮助我调试。)
抱歉,如果我的代码可能令人困惑,我不是保持代码整洁的最佳人选。如果您有任何困惑或疑问,我会尽力消除任何困惑。
好吧,在简化过程中需要考虑以下事项。您可以使用简单的 MAP 循环来确定哪些字母不正确,然后在单词中突出显示这些字母。
let guessWord = "agree"; // the first word thats inputed
let answerWord = "ready"; // the answer to the wordle
let tmpGuess = guessWord.split('');
let incorrects = answerWord.split('').map(e => {
let i = tmpGuess.indexOf(e);
if (i > -1) { // found it!
tmpGuess.splice(i, 1); // remove
return '';
}
return e;
}).filter(f => f);
console.log(incorrects);
要获得所有状态(按优先顺序:正确,wrong-position,不正确),您应该检查该优先顺序,并在设置该字母位置的状态后将字母从争用中移除.
function check(guess, targetWord) {
const checkStates = new Array(5).fill("incorrect");
const letters = guess.split('');
const targetLetters = targetWord.split('');
for (let i=0; i<5; i++) {
if (targetLetters[i] === letters[i]) {
checkStates[i] = "correct";
targetLetters[i] = '';
letters[i] = '';
}
}
for (let i=0; i<5; i++) {
if (!letters[i]) continue;
let fi = targetLetters.findIndex(t => t===letters[i]);
if (fi > -1) {
checkStates[i] = "contains";
targetLetters[fi] = '';
}
}
return checkStates;
}
let solution = "ready"
console.log(`solution is ${solution}`);
let guess = "agree";
console.log(`checking ${guess}... ${check(guess, solution)}`);
guess = "roars";
console.log(`checking ${guess}... ${check(guess, solution)}`);
guess = "boars";
console.log(`checking ${guess}... ${check(guess, solution)}`);
solution = "beast"
console.log(`\nsolution is ${solution}`);
guess = "treat";
console.log(`checking ${guess}... ${check(guess, solution)}`);
所以我对 java 脚本和一般编码还很陌生。我制作一个 Wordle 算法只是为了好玩,以培养我的编码技能。在制作这个算法时,我意识到我将不得不重新创建 Wordle。所以我在网上寻找 java 脚本中的 wordle 娱乐,我发现所有这些我都看到了一个我不想拥有的缺陷。
我看到的缺陷是在 IF 语句中检查 wordle 答案中的字母。特别是当字母在单词中但不在正确的位置时。我看到的游戏有一个 IF 语句,看起来像这样。
IF (answerWord.includes(guessLetter[i])) {
guessLetter[i] = color.(yellow)
}
(这不是他们写的,但它是主要思想)
我主要关注 .includes
。这是行不通的,因为假设我们的猜测词是“同意”而答案是“准备好”。 “同意”中的 2 个 E 是黄色的,我们不希望这样,因为它们在“准备就绪”中只有 1 个 E。所以我们只希望“同意”中的第一个 E 是黄色而不是第二个 E.
所以我决定自己想办法解决这个问题,我想出了这个办法,而且效果很好。至少我很确定它是有效的,基于我测试过的许多词。
所以我的问题是,由于我正在制作一个算法,我将进行大量计算,这是我能写出的最有效的算法还是我可以改进它?
let guessWord = "agree"; // the first word thats inputed
let answerWord = "ready"; // the answer to the wordle
/*
'letterCheck' is an array that tells what condition each letter in 'startLetter' is, based on the answer word
2 = the letter is in the word and in the correct place (GREEN)
1 = the letter is in the word but not in the correct place (YELLOW)
0 = the letter in not in the word (GREY)
*/
var letterCheck = ["0", "0", "0", "0", "0"];
var guessLetter = ["A", "A", "A", "A", "A"]; // the separated letters of 'startword' in a array
var answerLetter = ["A", "A", "A", "A", "A"]; // the separated letters of 'answord' in a array
//adds the start word and the answer world to the arrays
for (var i = 0; i < 5; i++) {
guessLetter[i] = guessWord.substring(i, i + 1);
answerLetter[i] = answerWord.substring(i, i + 1);
}
console.log(guessLetter);
console.log(letterCheck);
console.log(answerLetter);
//this loops goes though every letter one by one
for (var i = 0; i < 5; i++) {
//checks if the letter is in the right spot
if (guessLetter[i] == answerLetter[i]) {
letterCheck[i] = "2";
console.log(guessLetter[i]);
console.log(letterCheck);
} else if (answerWord.includes(guessLetter[i])) {
//checks if there is more than one letter in start word or its the first letter
if (guessWord.split(guessLetter[i]).length - 1 == 1 || i == 0) {
letterCheck[i] = "1";
console.log(guessLetter[i]);
console.log(letterCheck);
//checks if the the amount of same letters in start words is equel to the amount of same letters in answer word
} else if (guessWord.split(guessLetter[i]).length - 1 == answerWord.split(guessLetter[i]).length - 1) {
letterCheck[i] = "1";
console.log(guessLetter[i]);
console.log(letterCheck);
//opposite of above
} else if (guessWord.split(guessLetter[i]).length - 1 != answerWord.split(guessLetter[i]).length - 1) {
letterCheck[i] = "1";
console.log(guessLetter[i]);
console.log(letterCheck);
// checks if any of the letters infront of it are the same as it
for (var j = 0; j < i; j++) {
if (guessLetter[i] == guessLetter[j]) {
letterCheck[i] = "0";
console.log(guessLetter[i]);
console.log(letterCheck);
}
}
}
} else {
letterCheck[i] = "0";
console.log(guessLetter[i]);
console.log(letterCheck);
}
}
console.log(guessLetter);
console.log(letterCheck);
console.log(answerLetter);
console.log
,他们只是帮助我调试。)
抱歉,如果我的代码可能令人困惑,我不是保持代码整洁的最佳人选。如果您有任何困惑或疑问,我会尽力消除任何困惑。
好吧,在简化过程中需要考虑以下事项。您可以使用简单的 MAP 循环来确定哪些字母不正确,然后在单词中突出显示这些字母。
let guessWord = "agree"; // the first word thats inputed
let answerWord = "ready"; // the answer to the wordle
let tmpGuess = guessWord.split('');
let incorrects = answerWord.split('').map(e => {
let i = tmpGuess.indexOf(e);
if (i > -1) { // found it!
tmpGuess.splice(i, 1); // remove
return '';
}
return e;
}).filter(f => f);
console.log(incorrects);
要获得所有状态(按优先顺序:正确,wrong-position,不正确),您应该检查该优先顺序,并在设置该字母位置的状态后将字母从争用中移除.
function check(guess, targetWord) {
const checkStates = new Array(5).fill("incorrect");
const letters = guess.split('');
const targetLetters = targetWord.split('');
for (let i=0; i<5; i++) {
if (targetLetters[i] === letters[i]) {
checkStates[i] = "correct";
targetLetters[i] = '';
letters[i] = '';
}
}
for (let i=0; i<5; i++) {
if (!letters[i]) continue;
let fi = targetLetters.findIndex(t => t===letters[i]);
if (fi > -1) {
checkStates[i] = "contains";
targetLetters[fi] = '';
}
}
return checkStates;
}
let solution = "ready"
console.log(`solution is ${solution}`);
let guess = "agree";
console.log(`checking ${guess}... ${check(guess, solution)}`);
guess = "roars";
console.log(`checking ${guess}... ${check(guess, solution)}`);
guess = "boars";
console.log(`checking ${guess}... ${check(guess, solution)}`);
solution = "beast"
console.log(`\nsolution is ${solution}`);
guess = "treat";
console.log(`checking ${guess}... ${check(guess, solution)}`);