我可以让这段代码运行起来更高效/更短吗?

发布于 2025-01-09 00:12:24 字数 3385 浏览 0 评论 0原文

所以我对 javascript 和一般编码还很陌生。我制作 Wordle 算法只是为了好玩,以培养我的编码技能。在制作这个算法时,我意识到我将不得不重新创建 Wordle。所以我在网上寻找java脚本中的wordle娱乐,我发现的所有这些我都看到了一个我不想有的缺陷。

我发现的缺陷是在用于检查单词答案中的字母的 IF 语句中。特别是当字母在单词中但不在正确的位置时。我看到的游戏有一个 IF 语句,看起来像这样。

IF (answerWord.includes(guessLetter[i])) {
        guessLetter[i] = color.(yellow)
}

(这并不完全是他们的写法,但它是主要思想)

我的主要焦点是 .includes。这是行不通的,因为假设我们的猜测词是“同意”而答案词是“准备好”。 “agree”中的 2 个 E 是黄色的,我们不希望这样,因为它们只有“ready”中的 1 个 E。所以我们只希望“agree”中的第一个 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);
( I will remove the console.log's later they just helped my to debug.)

抱歉,如果我的代码可能令人困惑,我不是最擅长保持代码整洁的。如果您有任何困惑或疑问,我会尽力消除任何困惑。

So Im pretty new to javascript and coding in general. Im making a Wordle algorithm just for fun to build my skills in coding. and while making this algorithm i realized that i was going to have to kind of recreate Wordle. so i looked online for wordle recreations in java script, and all the ones I found I saw one flaw that I didn't want to have.

The flaw that I saw was in the IF statement for checking a letter in the wordle answer. Specifically when the letter is in the word but not in the right spot. The recreations that i saw had an IF statement that looked something like this.

IF (answerWord.includes(guessLetter[i])) {
        guessLetter[i] = color.(yellow)
}

(this isnt exactly how they wrote it, but its the main idea)

My main focus is on the .includes. This would not work because say our guess word is "agree" and the answer word "ready". the 2 E's in "agree" would be yellow, and we dont want that because their is is only 1 E in "ready. So we only want the first E in "agree" to be yellow and not the second E.

So i decided to figure it out on my own, and i was able to come up with this and it works. At least Im pretty sure it works, base on the the many words i tested it on.

So my question is, since Im making an algorithm im going to be making alot of calculations, is this the most efficient i could write this or can i make it better?

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);

( I will remove the console.log's later they just helped my to debug.)

Sorry if my code might be confusing, im not the best at keeping my code clean. if you have any confusion or questions, I'll try my best in clear up any confusion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂人 2025-01-16 00:12:24

要获取所有状态(按优先顺序:正确、位置错误、不正确),您应该检查该优先顺序,并在设置该字母位置的状态后从争用中删除字母。

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)}`);

To get all of the states (in precedence order: correct, wrong-position, incorrect), you should check in that precedence order, and remove letters from contention once the state in that letter's position has been set.

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)}`);

热血少△年 2025-01-16 00:12:24

嗯,在简化过程中需要考虑一些事情。您可以使用简单的 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);

Well, here's something to consider in simplifying. You can use a simple MAP loop to determine which letters are incorrect, then later highlight those in the word.

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文