【问题标题】:Discord.js - How do I set a value to an array string?Discord.js - 如何为数组字符串设置值?
【发布时间】:2018-08-01 05:35:51
【问题描述】:

我一直在研究“BlackJack”命令,它是一个迷你游戏。现在,我很确定只需为每个嵌入添加不同的数学生成器就可以了,如果加法方程等于 21,它就告诉你你输了!如果我知道如何为“cards”数组中的每个字符串分配不同的值,我就可以自己完成所有这些工作。

例如... 黑桃 A = 11

然后我就可以使用数学了... randomCard1 + randomCard 2 有点东西

const { CommandoClient, SQLiteProvider, Command } = require('discord.js-commando');
const { RichEmbed } = require('discord.js');

const client = new CommandoClient({
    commandPrefix: 'w!',
    unknownCommandResponse: false,
    owner: ['254323224089853953', '121222156121014272'],
    disableEveryone: true
});

module.exports = class BlackJackCommand extends Command {
    constructor(client) {
        super(client, {
            name: 'blackjack',
            group: 'ping',
            memberName: 'blackjack',
            description: 'Use w!blackjack [bet] to bet on blackjack! Use w!blackjackhelp to find out more!',
            examples: ['w!blackjack 20'],
            args: [
                {
                    key: 'ping',
                    prompt: 'How much ping do you want to bet?',
                    type: 'integer'
                }
            ]
        });    
    }


    async run(message, args) {

        var responses = Array('Stand','Hit','Double Down')
        var cards = Array('Ace of Clubs','2 of Clubs','3 of Clubs','4 of Clubs','5 of Clubs','6 of Clubs','7 of Clubs','8 of Clubs','9 of Clubs','10 of Clubs','Jack of Clubs','Queen of Clubs','King of Clubs','Ace of Diamonds','2 of Diamonds','3 of Diamonds','4 of Diamonds','5 of Diamonds','6 of Diamonds','7 of Diamonds','8 of Diamonds','9 of Diamonds','10 of Diamonds','Jack of Diamonds','Queen of Diamonds','King of Diamonds','Ace of Hearts','2 of Hearts','3 of Hearts','4 of Hearts','5 of Hearts','6 of Hearts','7 of Hearts','8 of Hearts','9 of Hearts','10 of Hearts','Jack of Hearts','Queen of Hearts','King of Hearts','Ace of Spades','2 of Spades','3 of Spades','4 of Spades','5 of Spades','6 of Spades','7 of Spades','8 of Spades','9 of Spades','10 of Spades','Jack of Spades','Queen of Spades','King of Spades');
        var joker = ('<:joker:415835828770570240>')
        const randomCard1 = cards[Math.floor(Math.random()*cards.length)];
        const randomCard2 = cards[Math.floor(Math.random()*cards.length)];

        const randomDealer = responses[Math.floor(Math.random()*responses.length)];


        const initial = new RichEmbed()
        .setTitle(`**${joker} Blackjack! ${joker}**`)
        .setAuthor(message.author.tag, message.author.displayAvatarURL)
        .setThumbnail('https://pbs.twimg.com/profile_images/1874281601/BlackjackIcon.png')
        .addField('**Initial Deal:**', `Your Cards:\n- ${randomCard1}\n- ${randomCard2}`)
        .setColor(0xAE0086)

        const dealer1 = new RichEmbed()
        .setTitle(`**${joker} Blackjack! ${joker}**`)
        .setAuthor(message.author.tag, message.author.displayAvatarURL)
        .setThumbnail('https://pbs.twimg.com/profile_images/1874281601/BlackjackIcon.png')
        .addField('**Initial Deal:**', `Your Cards:\n- ${randomCard1}\n- ${randomCard2}`)
        .addField('**Dealer\'s Turn 1:**', `Choice: ${randomDealer}`)
        .setColor(0xAE0086)

        message.embed(initial);

        const filter = message => message.content.includes('stand');

        message.reply('Your turn to choose: ``stand`` ``hit`` ``surrender`` ``double down`` ``cancel``')
        .then(function(){
            message.channel.awaitMessages(response => filter, {
              max: 1,
              time: 300000000,
              errors: ['time'],
            })
            .then((collected) => {
                message.embed(dealer1);
              })
              .catch(function(){
                message.channel.send('You didnt respond in time!');
              });
          });
      }
    }

【问题讨论】:

  • 你不能把它变成一个对象,然后在那个对象中分配每个值吗?
  • @Jason 我不知道该怎么做

标签: javascript arrays math discord discord.js


【解决方案1】:

您至少可以采取两种方法。我首先要注意的是,您将 ace 视为等于 11,但规则允许 1 或 11。

定位

因为这个数组的内容遵循一个模式,我们可以使用数学来查看数组中的位置并确定卡片的值。如果index 持有我们的数组偏移量,我们可以对其执行以下操作:

  • 将数字的模数乘以 13,这会将值从 0 到 12 的花色分开
  • 加一,得到每套花色中的数字 1-13
  • 取该数字或 10 中的最小值,将面卡变成 10 秒
  • 返回并使用该值,如果它是 1,也可能是 11

这可能看起来像:

value = Math.min((index % 13) + 1, 10)

它涵盖了处理 Ace 的两个可能值的最后一步之外的所有步骤。

对象

您可以将定义卡片的方式更改为:

var cards = [
    {value: 1, name: 'Ace of Clubs'},
    {value: 2, name: 'Two of Clubs'},
    {value: 3, name: 'Three of Clubs'}
];

并以cards[index].name 访问卡的名称,以cards[index].value 访问值,其中index 是数组中的偏移量。请注意,该数组仅使用方括号而不是 Array( 声明。

【讨论】:

  • 所以,我刚刚将数组更改为您在对象下给我的数组。我现在不确定我应该如何添加 randomCards 生成器的结果,以及如何处理cards[index].name 和cards[index].value,考虑到当我将它输入到我的代码中时回来说索引未定义:/新代码:hastebin.com/feziqayuli.cs
  • 索引需要定义,并且可能需要重命名。你打算如何获取玩家通过命中选择的附加牌的牌名和牌值?您当前的结构显示硬编码的卡片一和卡片二,但玩家最终可能会得到许多卡片。通过随机选择您在 randomCard1 中使用的卡代码,将该索引设置为新变量。
  • 非常感谢您的帮助,但我对所有事情还是很陌生,不太了解您为我提供的帮助。我想我可能会推迟创建这个级别命令,因为我只记得硬编码是行不通的。
  • 伙计,干杯!我想通了,现在它正在工作!我现在需要做的就是弄清楚如何等待响应,并根据响应进行响应! :) 非常感谢@jason
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2021-04-02
  • 2019-10-17
  • 2011-03-26
相关资源
最近更新 更多