Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. 'use strict'
  2. const PlayCard = require('./PlayCard')
  3.  
  4. /**
  5. * Represent a deck of card, with base module Array
  6. */
  7. class Deck extends Array {
  8. constructor () {
  9. super()
  10.  
  11. /**
  12. * initialize the Deck, pushes 52 PlayCard to super{array}
  13. */
  14. for (let i = 0; i < 52; i++) {
  15. super.push(new PlayCard(i + 1))
  16. }
  17.  
  18. /**
  19. * @type {Array} is an array of hands
  20. */
  21. this._hands = []
  22. }
  23.  
  24. /**
  25. * shuffles the order of the array
  26. */
  27. shuffle () {
  28. const shuffled = []
  29.  
  30. do {
  31. const randomIndex = Math.floor(Math.random() * this.length)
  32.  
  33. shuffled.push(this[randomIndex])
  34. this.splice(randomIndex, 1) // removes at index
  35. } while (this.length > 0)
  36.  
  37. this.push(...shuffled)
  38. }
  39.  
  40. /**
  41. * collects all the card from all hands that have at least one card
  42. */
  43. collect () {
  44. this._hands.forEach(hand => {
  45. do {
  46. // collection of all cards from every hand
  47. this.push(hand.pop())
  48. } while (hand.length > 0)
  49. })
  50.  
  51. this._hands = []
  52. }
  53.  
  54. /**
  55. * @param {array} hand it removes the last card from the deck, and puts it in the hand of the caller
  56. */
  57. pickACard (hand) {
  58. if (this.length === 0) {
  59. throw Error('No more cards in the deck, to many players?')
  60. }
  61. hand.push(this.pop())
  62.  
  63. if (!this._hands.includes(hand)) {
  64. this._hands.push(hand)
  65. }
  66. }
  67. }
  68.  
  69. module.exports = Deck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement