mercredi 27 mai 2020

How to delete a card from the deck after dealt

I want to deal a card and after that I want that specific card to pop from the deck but right now my code is always 51 cards it does not go to 0.

Thank you very much in advance

//Variable with the four suits of a deck

const suits = ['Corazon', 'Diamante', 'Trebol', 'Espada'];

//Variable with all the possible card values

const values =[2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jota', 'Reina', 'Rey', 'Az'];

//Create a class for the cards

class Card {
  constructor(suit, value) {
    this.suit = suit;
    this.value = value;
  };
};

//Create a class for the deck

class Deck{
  constructor(){
    this.deck = [];
  }

  //Creates the deck
  createDeck(suits, values) {
    for (let suit of suits) {
      for (let value of values) {
        this.deck.push(new Card(suit, value));
      }
    }
  return this.deck;
  }

//Shuffles the deck

shuffle () {
    let counter = this.deck.length, temp, i;

    while(counter) {
      i = Math.floor(Math.random() * counter--);
      temp = this.deck[counter];
      this.deck[counter] = this.deck[i];
      this.deck[i] = temp;
    }
    return this.deck;
  }

//Deals a card and discards it from the deck

deal () {
    let hand = [];
    while (hand.length < 1) {
      hand.push(this.deck.pop());
    }
    return hand;

  }

//Assigns game to the dealt card

game () {
    this.createDeck(suits, values);
    this.shuffle();
  }
};


let deck = new Deck();
deck.createDeck(suits, values);
deck.shuffle();
console.log(deck.deal());



Aucun commentaire:

Enregistrer un commentaire