0

I've been working on a "BlackJack" command which is a minigame. Now, I'm pretty sure simply adding the different math generators for each embed would work, and if the addition equation equals over 21, it tells you you've lost! I could do all that myself if I knew how to assign each string within the "cards" array a different value.

For example... Ace of Spades = 11

then I'd be able to use maths... 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!');
              });
          });
      }
    }
2
  • Couldn't you just make it an object, and assign each value within that object? Commented Feb 21, 2018 at 19:55
  • @Jason I wouldn't know how to go about that Commented Feb 21, 2018 at 21:09

1 Answer 1

2

There are at least two approaches you could take. The first thing I note is that you are treating an ace as equal to 11, but the rules allow for either 1 or 11.

Positional

Because the content of this array follows a pattern we can use math to look at the position within the array and determine the value for the card. If index holds our array offset we can do the following to it:

  • take the modulus of the number by 13, this will separate the suits with values of zero through 12
  • add one to that to get the numbers 1-13 within each suit
  • take the minimum of either that number or 10 to turn the face cards into 10s
  • go back and work with the value if it is 1 and could also be 11

This might look like:

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

which covers all but the last step of handling the two possible values for Ace.

Objects

You can change the way you define cards to:

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

and access the name of the card as cards[index].name and the value as cards[index].value where index is the offset within the array. Note that the array is declared with just square brackets instead of Array(.

Sign up to request clarification or add additional context in comments.

4 Comments

So, I've just changed the array to the one you gave me under objects. I'm now unsure as to how i'm supposed to add the results of the randomCards generator, and what to do with the cards[index].name and cards[index].value, considering when I input it into my code it comes back as saying index isn't defined :/ New Code: hastebin.com/feziqayuli.cs
Index needs to be defined, and likely will be need to be renamed. How are you planning on getting the card names and values for the additional cards the player chooses via hit? Your current structure shows card one and card two hard coded, but a player could end up with many cards. Setting that aside index would be set as a new var with your random selection of a card code you are using inside randomCard1.
Thank you so much for your help, but I am still fairly new to everything and don't understand much of what you've helped me with. I think I may postpone the creation of this level command, due to me just remembering that hard-coding won't work.
Man, cheers buddy! I figured it out and now it's working! All I need to do now is figure out how to wait for a response, and respond according to the response! :) thank you so much though @jason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.