If you are ever want to create a card game in Javascript you are going to need a way to build a deck of cards. In this quick tutorial we'll show you how to create a card and deck object and how to shuffle the deck of cards.

First thing is first, let's create a simple Card object.

function card(value, name, suit){
	this.value = value;
	this.name = name;
	this.suit = suit;
}

Above we have a new card object that will accept a value, name, and suit for each card. Ok, we've got our card object. Let's create a simple deck object that will return an array of 52 cards.


function deck(){
	this.names = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
	this.suits = ['Hearts','Diamonds','Spades','Clubs'];
	var cards = [];
    
    for( var s = 0; s < this.suits.length; s++ ) {
        for( var n = 0; n < this.names.length; n++ ) {
            cards.push( new card( n+1, this.names[n], this.suits[s] ) );
        }
    }

    return cards;
}

Now, let's create a new deck of cards... Signup for free to gain access to the rest of this tutorial :)

Sorry, this full post is only available to Subscribers


Login

or Sign Up

Sign Up

or Login