【问题标题】:About randomization (JavaScript)关于随机化 (JavaScript)
【发布时间】:2020-12-29 13:11:20
【问题描述】:

所以我正在制作一个不和谐的机器人并决定实现一个简单的琐事 api,在返回结果时它显示如下:

[
  {
    category: 'Entertainment: Video Games',
    type: 'multiple',
    difficulty: 'medium',
    question: 'What is the main character of Metal Gear Solid 2?',
    correct_answer: 'Raiden',
    incorrect_answers: [ 'Solidus Snake', 'Big Boss', 'Venom Snake' ]
  }
]

因为这是我当前的代码

 //creates an embed to make it more aestheticaly pleasing
    const triviaEmbed = new Discord.MessageEmbed()
      .setColor(0x6e7175)
      .setTitle('Trivia!')
      .addFields({
        name: 'Category',
        value: triviaValue.results[0].category
      }, {
        name: 'Type',
        value: triviaDisplayType
      }, {
        name: 'Difficulty',
        value: triviaDisplayDifficulty
      }, {
        name: 'Question',
        value: triviaValue.results[0].question
      }, {
        name: 'Answers',
        value: `${triviaValue.results[0].incorrect_answers}, ${triviaValue.results[0].correct_answer}`
      })
    // sends the embed
    message.channel.send(triviaEmbed);

正确的答案永远是最后一个,让这个过程随机的最有效的方法是什么,我能想到的最好的东西似乎非常多余。

【问题讨论】:

    标签: javascript api random discord.js


    【解决方案1】:

    由于incorrect_answers 是一个数组,您可以创建它的副本并将正确答案插入这个新数组中,然后使用数组洗牌算法将其洗牌。

    我们可以使用 ES6 扩展运算符轻松复制incorrect_answers。如果您想了解更多,请点击here

    // Creates an array with the values of incorrect answers + the correct answer
    const result = triviaValue.results[0];
    const answers = [...result.incorrect_answers, result.correct_answer];
    

    您可以点击here查看一些数组混洗算法的示例。

    在本例中,我们将使用链接中的 Durstenfeld shuffle。 我建议您创建一个 utils 文件夹并将其作为一个模块,以便您可以在需要时使用它。但为了不让事情变得过于复杂,我将向您展示具有上述功能的示例代码。

    你可以这样做:

    // Declare the array shuffler - best if it was a module and you
    // got it by using require() or import
    function shuffleArray(array) {
      for (let i = array.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        let temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      }
    }
    
    // Generate the array
    const result = triviaValue.results[0];
    const answers = [...result.incorrect_answers, result.correct_answer];
    
    // Shuffle It
    shuffleArray(answers);
    
    // Create the Embed
    const triviaEmbed = new Discord.MessageEmbed()
      .setColor(0x6e7175)
      .setTitle('Trivia!')
      .addFields({
        name: 'Category',
        value: triviaValue.results[0].category
      }, {
        name: 'Type',
        value: triviaDisplayType
      }, {
        name: 'Difficulty',
        value: triviaDisplayDifficulty
      }, {
        name: 'Question',
        value: triviaValue.results[0].question
      }, {
        name: 'Answers',
        // this will break the line for every answer
        // you could go ahead and use ', ' as parameter
        // if you wish them to be in a single line
        value: answers.join('\n')
      });
    
    // Send the embed
    message.channel.send(triviaEmbed);
    
    

    【讨论】:

      【解决方案2】:

      您可以尝试在错误答案中的随机点推送正确答案。

      例子:

      const answers = triviaValue.results[0].incorrect_answers
      answers.splice(Math.floor(Math.random() * answers), 0, triviaValue.results[0].correct_answer)
      

      代码使用splice方法将正确答案插入到answers数组中,Math.floor(Math.random() * answers)只选择一个随机索引插入。

      【讨论】:

        【解决方案3】:

        如何将所有答案(包括正确答案)放入一个名为answers 的数组中,然后用一个整数表示正确答案的索引(0 到 3)?

        这将是最简单的方法,IMO。

        【讨论】:

        • 这似乎更像是一个建议而不是一个答案。
        猜你喜欢
        • 2011-01-27
        • 2015-01-24
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 1970-01-01
        相关资源
        最近更新 更多