Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const PlayCard = require('./PlayCard')
- /**
- * Represent a deck of card, with base module Array
- */
- class Deck extends Array {
- constructor () {
- super()
- /**
- * initialize the Deck, pushes 52 PlayCard to super{array}
- */
- for (let i = 0; i < 52; i++) {
- super.push(new PlayCard(i + 1))
- }
- /**
- * @type {Array} is an array of hands
- */
- this._hands = []
- }
- /**
- * shuffles the order of the array
- */
- shuffle () {
- const shuffled = []
- do {
- const randomIndex = Math.floor(Math.random() * this.length)
- shuffled.push(this[randomIndex])
- this.splice(randomIndex, 1) // removes at index
- } while (this.length > 0)
- this.push(...shuffled)
- }
- /**
- * collects all the card from all hands that have at least one card
- */
- collect () {
- this._hands.forEach(hand => {
- do {
- // collection of all cards from every hand
- this.push(hand.pop())
- } while (hand.length > 0)
- })
- this._hands = []
- }
- /**
- * @param {array} hand it removes the last card from the deck, and puts it in the hand of the caller
- */
- pickACard (hand) {
- if (this.length === 0) {
- throw Error('No more cards in the deck, to many players?')
- }
- hand.push(this.pop())
- if (!this._hands.includes(hand)) {
- this._hands.push(hand)
- }
- }
- }
- module.exports = Deck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement