Home Programs JavaScript Program to Shuffle Deck of Cards

JavaScript Program to Shuffle Deck of Cards

In this article we show you a program that will simulate the shuffling of a deck of cards

 

Code

 

(function () {

    var cardWeights = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
    var cardTypes = ["Spades", "Hearts", "Diamonds", "Clubs"];
    var cards = new Array();

    function card(cardType, weight) {
        this.cardType = cardType;
        this.weight = weight;
    };

    function CreateDeck() {
        cardTypes.forEach(function (type) {
            cardWeights.forEach(function (weight) {
                cards.push(new card(type, weight));
            });
        });
    };

    function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function shuffleDeck(shuffleCnt) {
        for(var i = 0; i < shuffleCnt; i++) {
            var rndNo = getRandomInt(0, 51);
            var card = cards[i];
            cards[i] = cards[rndNo];
            cards[rndNo] = card;
        }
    }


    CreateDeck();
    console.log(cards);
    shuffleDeck(getRandomInt(10, 25));
    console.log(cards);
})();

In the javascript console you will see something like  this

 

Array(52) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
debugger eval code:35:13
(52) […]
0: Object { cardType: "Clubs", weight: "7" }
1: Object { cardType: "Clubs", weight: "2" }
2: Object { cardType: "Hearts", weight: "1" }
3: Object { cardType: "Diamonds", weight: "J" }
4: Object { cardType: "Hearts", weight: "2" }
5: Object { cardType: "Spades", weight: "J" }
6: Object { cardType: "Hearts", weight: "J" }
7: Object { cardType: "Spades", weight: "9" }
8: Object { cardType: "Diamonds", weight: "Q" }
9: Object { cardType: "Diamonds", weight: "5" }
10: Object { cardType: "Hearts", weight: "K" }
11: Object { cardType: "Diamonds", weight: "8" }
12: Object { cardType: "Spades", weight: "K" }
13: Object { cardType: "Spades", weight: "3" }
14: Object { cardType: "Spades", weight: "5" }
15: Object { cardType: "Hearts", weight: "3" }
16: Object { cardType: "Hearts", weight: "4" }
17: Object { cardType: "Hearts", weight: "5" }
18: Object { cardType: "Hearts", weight: "6" }
19: Object { cardType: "Hearts", weight: "7" }
20: Object { cardType: "Hearts", weight: "8" }
21: Object { cardType: "Hearts", weight: "9" }
22: Object { cardType: "Hearts", weight: "10" }
23: Object { cardType: "Spades", weight: "7" }
24: Object { cardType: "Hearts", weight: "Q" }
25: Object { cardType: "Spades", weight: "6" }
26: Object { cardType: "Diamonds", weight: "1" }
27: Object { cardType: "Diamonds", weight: "2" }
28: Object { cardType: "Diamonds", weight: "3" }
29: Object { cardType: "Diamonds", weight: "4" }
30: Object { cardType: "Spades", weight: "10" }
31: Object { cardType: "Diamonds", weight: "6" }
32: Object { cardType: "Diamonds", weight: "7" }
33: Object { cardType: "Spades", weight: "Q" }
34: Object { cardType: "Diamonds", weight: "9" }
35: Object { cardType: "Diamonds", weight: "10" }
36: Object { cardType: "Spades", weight: "4" }
37: Object { cardType: "Spades", weight: "8" }
38: Object { cardType: "Diamonds", weight: "K" }
39: Object { cardType: "Clubs", weight: "1" }
40: Object { cardType: "Spades", weight: "2" }
41: Object { cardType: "Clubs", weight: "3" }
42: Object { cardType: "Clubs", weight: "4" }
43: Object { cardType: "Clubs", weight: "5" }
44: Object { cardType: "Clubs", weight: "6" }
45: Object { cardType: "Spades", weight: "1" }
46: Object { cardType: "Clubs", weight: "8" }
47: Object { cardType: "Clubs", weight: "9" }
48: Object { cardType: "Clubs", weight: "10" }
49: Object { cardType: "Clubs", weight: "J" }
50: Object { cardType: "Clubs", weight: "Q" }
51: Object { cardType: "Clubs", weight: "K" }

 

You may also like