Discord.js - 如何设置数组字符串的值?

时间:2018-02-21 15:12:08

标签: javascript arrays math discord discord.js

我一直在研究一个小型游戏“BlackJack”命令。现在,我很确定简单地为每个嵌入添加不同的数学生成器都可以工作,如果加法等式超过21,它会告诉你丢失了!如果我知道如何为“cards”数组中的每个字符串分配不同的值,我可以自己完成所有这些。

例如......黑桃王牌= 11

然后我就可以使用数学... randomCard1 + randomCard 2 sorta thing

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!');
              });
          });
      }
    }

1 个答案:

答案 0 :(得分:1)

您可以采取至少两种方法。我注意到的第一件事是你将ace等同于11,但规则允许1或11。

位移说明

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

  • 将数字的模数乘以13,这将将套装与0到12
  • 的值分开
  • 添加一个以获得每件诉讼中的数字1-13
  • 取该数字或10的最小值将面部卡片变成10s
  • 返回并使用值,如果它是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(声明。