├── README.md └── deck.js /README.md: -------------------------------------------------------------------------------- 1 | # deckLibraryJS 2 | JS library for a deck of cards. 3 | 4 | Import library. 5 | 6 | const deck = require('./deck.js') 7 | 8 | Initialize the Deck object. 9 | 10 | let Deck = new deck() 11 | 12 | Generate the deck of cards. 13 | 14 | Deck.generate_deck() 15 | 16 | Print the array of card objects in the deck. 17 | 18 | Deck.print_deck() 19 | 20 | Shuffle the deck randomly. 21 | 22 | Deck.shuffle() 23 | 24 | Deal a card - returns card object. 25 | 26 | Deck.deal() 27 | 28 | Put most recently dealt card back on top of deck. 29 | 30 | Deck.replace() 31 | 32 | Remove the current deck. 33 | 34 | Deck.clear_deck() 35 | 36 | -------------------------------------------------------------------------------- /deck.js: -------------------------------------------------------------------------------- 1 | // deck of cards library 2 | 3 | // deck class for shuffling, dealing 4 | module.exports = class Deck { 5 | constructor() { 6 | this.deck = [] 7 | this.dealt_cards = [] 8 | } 9 | 10 | // generates a deck of cards 11 | generate_deck () { 12 | 13 | // creates card generator function 14 | let card = (suit, value) => { 15 | let name = value + ' of ' + suit; 16 | //returns key and values into each instance of the this.deck array 17 | return {'name': name, 'suit': suit, 'value':value} 18 | } 19 | 20 | let values = ['2', '3','4','5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] 21 | let suits = ['Clubs', 'Diamonds', 'Spades', 'Hearts'] 22 | 23 | for ( let s = 0; s < suits.length; s++ ) { 24 | for ( let v = 0; v < values.length; v++ ) { 25 | this.deck.push(card(suits[s], values[v])) 26 | } 27 | } 28 | } 29 | 30 | // prints the deck of card objects 31 | print_deck () { 32 | if (this.deck.length === 0) { 33 | console.log('Deck has not been generated. Call generate_deck() on deck object before continuing.') 34 | } 35 | else { 36 | for ( let c = 0; c < this.deck.length; c++ ) { 37 | console.log(this.deck[c]) 38 | } 39 | } 40 | } 41 | 42 | // shuffle the deck 43 | shuffle () { 44 | for( let c = this.deck.length -1; c >= 0; c--){ 45 | let tempval = this.deck[c]; 46 | let randomindex = Math.floor(Math.random() * this.deck.length); 47 | 48 | //ensures that the randome index isn't the same as the current index. It runs the function again if this returns as true 49 | while(randomindex == c){ randomindex = Math.floor(Math.random() * this.deck.length)} 50 | this.deck[c] = this.deck[randomindex]; 51 | this.deck[randomindex] = tempval; 52 | } 53 | } 54 | 55 | // deal a number cards 56 | deal (num_cards) { 57 | 58 | let cards = [] 59 | 60 | for ( let c = 0; c < num_cards; c++ ) { 61 | let dealt_card = this.deck.shift() 62 | cards.push(dealt_card) 63 | this.dealt_cards.push(dealt_card) 64 | } 65 | 66 | return cards 67 | } 68 | 69 | replace () { 70 | this.deck.unshift(this.dealt_cards.shift()) 71 | } 72 | 73 | clear_deck () { 74 | this.deck = [] 75 | } 76 | } 77 | --------------------------------------------------------------------------------