Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. 'use strict'
  2.  
  3. /**
  4. * Using the Fisher Yates Algorithm. Inspiration taken from https://www.youtube.com/watch?v=myL4xmtAVtw
  5. * @param {array} text Shuffles the elements of an array.
  6. * @returns {array} A shuffled array.
  7. */
  8.  
  9. class CardDeck {
  10. constructor () {
  11. this.shuffleddeck = this.shuffleCards()
  12. this.cardValues = {
  13. 'A♥': [1, 14],
  14. '2♥': 2,
  15. '3♥': 3,
  16. '4♥': 4,
  17. '5♥': 5,
  18. '6♥': 6,
  19. '7♥': 7,
  20. '8♥': 8,
  21. '9♥': 9,
  22. '10♥': 10,
  23. 'J♥': 11,
  24. 'Q♥': 12,
  25. 'K♥': 13,
  26. 'A♦': [1, 14],
  27. '2♦': 2,
  28. '3♦': 3,
  29. '4♦': 4,
  30. '5♦': 5,
  31. '6♦': 6,
  32. '7♦': 7,
  33. '8♦': 8,
  34. '9♦': 9,
  35. '10♦': 10,
  36. 'J♦': 11,
  37. 'Q♦': 12,
  38. 'K♦': 13,
  39. 'A♠': [1, 14],
  40. '2♠': 2,
  41. '3♠': 3,
  42. '4♠': 4,
  43. '5♠': 5,
  44. '6♠': 6,
  45. '7♠': 7,
  46. '8♠': 8,
  47. '9♠': 9,
  48. '10♠': 10,
  49. 'J♠': 11,
  50. 'Q♠': 12,
  51. 'K♠': 13,
  52. 'A♣': [1, 14],
  53. '2♣': 2,
  54. '3♣': 3,
  55. '4♣': 4,
  56. '5♣': 5,
  57. '6♣': 6,
  58. '7♣': 7,
  59. '8♣': 8,
  60. '9♣': 9,
  61. '10♣': 10,
  62. 'J♣': 11,
  63. 'Q♣': 12,
  64. 'K♣': 13
  65. }
  66. }
  67.  
  68. shuffleCards () {
  69. const carddeck = ['A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥',
  70. 'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦',
  71. 'A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠',
  72. 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣']
  73.  
  74. let newCardPos
  75. let temp = []
  76. for (let i = carddeck.length - 1; i > 0; i -= 1) {
  77. newCardPos = Math.floor(Math.random() * (i + 1))
  78. temp = carddeck[i]
  79. carddeck[i] = carddeck[newCardPos]
  80. carddeck[newCardPos] = temp
  81. }
  82. return carddeck
  83. }
  84. }
  85.  
  86. module.exports = CardDeck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement