A simple JavaScript library to manage "generic" decks of cards.
To download Cardeck.js, you can do it via npm, using the following command:
npm install cardeck
If you want to save the dependency into the package.json
file of your project, use:
npm install --save cardeck
when you download the package via npm, by default, it will be saved under the node_modules
folder.
Cardeck.js works in both Node.js and browser JavaScript.
var cardeck = require('cardeck');
var Card = cardeck.Card;
var Deck = cardeck.Deck;
// Now you can use both Card and Deck classes
<html>
<head>...</head>
<body>
...
<script src="path_to_node_modules_folder/cardeck.js"></script>
<script>
var Card = cardeck.Card;
var Deck = cardeck.Deck;
// Now you can use both Card and Deck classes
</script>
</html>
Constructor. Creates an instance of Card.
Parameter Description value
Value of the card. (Optional, default value: 0
)type
Type of the card. (Optional, default value: 'default'
)imgPath
Path to the image which represents the card. (Optional, default value: './card.png'
)Example:
var myFirstCard = new Card('A', 'Aces', 'img/aces/A.png'); // myFirstCard = Card { value: 'A', type: 'Aces', imgPath: 'img/aces/A.png' }
Sets the value of a card.
Parameter Description v
Value of the card. Example:
var myFirstCard = new Card(); // myFirstCard = Card { value: 0, type: 'default', imgPath: './card.png' } myFirstCard.setValue(4); // myFirstCard = Card { value: 4, type: 'default', imgPath: './card.png' }
Gets the value of the card.
Example:
var myFirstCard = new Card(6); // myFirstCard = Card { value: 6, type: 'default', imgPath: './card.png' } myFirstCard.getValue(); // 6
Sets the type of a card.
Parameter Description t
Type of the card. Example:
var myFirstCard = new Card(); // myFirstCard = Card { value: 0, type: 'default', imgPath: './card.png' } myFirstCard.setType('Diamonds'); // myFirstCard = Card { value: 0, type: 'Diamonds', imgPath: './card.png' }
Gets the type of the card.
Example:
var myFirstCard = new Card(6, 'Clubs'); // myFirstCard = Card { value: 6, type: 'Clubs', imgPath: './card.png' } myFirstCard.getType(); // 'Clubs'
Sets the path to the image which represents a card.
Parameter Description i
The path to the image which represents the card. Example:
var myFirstCard = new Card(); // myFirstCard = Card { value: 0, type: 'default', imgPath: './card.png' } myFirstCard.setImgPath('./img/my-card.png'); // myFirstCard = Card { value: 0, type: 'Diamonds', imgPath: './img/my-card.png' }
Gets the path to the image which represents a card.
Example:
var myFirstCard = new Card(6, 'Clubs', './img/my-card.png'); // myFirstCard = Card { value: 6, type: 'Clubs', imgPath: './img/my-card.png' } myFirstCard.getImgPath(); // './img/my-card.png'
Constructor. Creates an instance of Deck.
Parameter Description name
Name of the deck. (Optional, default value: 'default'
)Example:
var myFirstDeck = new Deck('The best deck'); // myFirstDeck = Deck { name: 'The best deck', cards: [], size: 0 }
Sets the name of a deck.
Parameter Description n
Name of the deck. Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.setName('The very best deck'); // myFirstDeck = Deck { name: 'The very best deck', cards: [], size: 0 }
Gets the name of the deck.
Example:
var myFirstDeck = new Card('Just a normal deck'); // myFirstDeck = Deck { name: 'Just a normal deck', cards: [], size: 0 } myFirstDeck.getName(); // 'Just a normal deck'
Gets the size (number of cards) of the deck.
Example:
var myFirstDeck = new Card('Just another deck'); // myFirstDeck = Deck { name: 'Just another deck', cards: [], size: 0 } myFirstDeck.getSize(); // 0
Adds a new card at the top of the deck.
Parameter Description c
The object or Card
to insert on the deck.Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToTop(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToTop(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 2 }
Adds a new card at the end of the deck.
Parameter Description c
The object or Card
to insert on the deck.Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToBottom(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToBottom(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 }
Draws a card from the top of the deck.
Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToTop(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToTop(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 2 } myFirstDeck.drawCardFromTop(); // Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }
Draws a card from the bottom of the deck.
Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToTop(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToTop(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 2 } myFirstDeck.drawCardFromBottom(); // Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }
Adds a new card at a random position of the deck.
Parameter Description c
The object or Card
to insert on the deck.Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 } myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 }
Draws a card from a random position of the deck.
Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 } myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 } myFirstDeck.drawCardFromRandom(); // Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }
Shuffles the order of the cards in a deck.
Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 } myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 } myFirstDeck.shuffle(); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' } ], size: 3 }
xchange the order of the first half of cards of the deck with the second one.
Example:
var myFirstDeck = new Deck(); // myFirstDeck = Deck { name: 'default', cards: [], size: 0 } myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 } myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 } myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg')); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 } myFirstDeck.cut(); // myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 3 }
Eliasib García, 2017