Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- /**
- * Using the Fisher Yates Algorithm. Inspiration taken from https://www.youtube.com/watch?v=myL4xmtAVtw
- * @param {array} text Shuffles the elements of an array.
- * @returns {array} A shuffled array.
- */
- class CardDeck {
- constructor () {
- this.shuffleddeck = this.shuffleCards()
- this.cardValues = {
- 'A♥': [1, 14],
- '2♥': 2,
- '3♥': 3,
- '4♥': 4,
- '5♥': 5,
- '6♥': 6,
- '7♥': 7,
- '8♥': 8,
- '9♥': 9,
- '10♥': 10,
- 'J♥': 11,
- 'Q♥': 12,
- 'K♥': 13,
- 'A♦': [1, 14],
- '2♦': 2,
- '3♦': 3,
- '4♦': 4,
- '5♦': 5,
- '6♦': 6,
- '7♦': 7,
- '8♦': 8,
- '9♦': 9,
- '10♦': 10,
- 'J♦': 11,
- 'Q♦': 12,
- 'K♦': 13,
- 'A♠': [1, 14],
- '2♠': 2,
- '3♠': 3,
- '4♠': 4,
- '5♠': 5,
- '6♠': 6,
- '7♠': 7,
- '8♠': 8,
- '9♠': 9,
- '10♠': 10,
- 'J♠': 11,
- 'Q♠': 12,
- 'K♠': 13,
- 'A♣': [1, 14],
- '2♣': 2,
- '3♣': 3,
- '4♣': 4,
- '5♣': 5,
- '6♣': 6,
- '7♣': 7,
- '8♣': 8,
- '9♣': 9,
- '10♣': 10,
- 'J♣': 11,
- 'Q♣': 12,
- 'K♣': 13
- }
- }
- shuffleCards () {
- const carddeck = ['A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥',
- 'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦',
- 'A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠',
- 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣']
- let newCardPos
- let temp = []
- for (let i = carddeck.length - 1; i > 0; i -= 1) {
- newCardPos = Math.floor(Math.random() * (i + 1))
- temp = carddeck[i]
- carddeck[i] = carddeck[newCardPos]
- carddeck[newCardPos] = temp
- }
- return carddeck
- }
- }
- module.exports = CardDeck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement