kbjr/node-cards

How can I encode the deck?

Closed this issue · 2 comments

Hey,

First off thanks for the amazing script!.

I have been having a few problems trying to add something in. I need to encode each card in the deck from the following string:

123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ

var encodes = [
'1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'
];

Foreach card that is created I need to add in the corresponding encode.

Ace of Heart = 1
2 of Hearts = 2
3 of Hearts = 3
......
King of Diamonds = Q

I am going to need to pull the single char 'Encode' to provably shuffle the deck at a later date.

Any help would be great, I have tried various methods but cannot seem to do it - I have a strong php background but my javascript sucks.

I need this so I can provably shuffle the deck of cards and later prove it was fair.

kbjr commented

Your best bet would be to add a property to each Card instance immediately after creating the deck, before shuffling, and then pull check them after shuffling, something like this:

var encodes = [
    '1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j',
    'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C',
    'D','E','F','G','H','I','J','K','L','M','N','O','P','Q'
];

// Create a new 52 card poker deck
var deck = new cards.PokerDeck();

// Add the encode property
deck.deck.forEach(function(card) {
    card.encode = encodes.shift();
});

// Shuffle the deck
deck.shuffleAll();

// Get your shuffled list of encodes
encodes = deck.deck.map(function(card) {
    return card.encode;
});

kbjr,

You are a life safe, one thing I have noticed in javascript is things are never as hard as you first imagine.

Thanks very much, Ill make sure to leave a mention the site is using this package when its complete.