lundi 29 juillet 2019

How do I create a large array using a deck of 52 cards, and sets of 2 cards as each element?

I would like to shuffle the deck, and then filter the array to return two cards. The two cards represent the two dealt cards from the original 52. The problem I am having is creating a loop, if statement, or switch statement that will speed up the process of filtering through all the combinations of two so that I may apply pictures to each possibility for a fluid representation of poker with markup.

I would like to keep my original deck as an array of 52, rather than splitting the suits and numbers. As that is my thought process from the beginning. I am using the Fisher-Yates shuffle algorithm to shuffle my original deck of cards.

let deck = ["2 Club","2 Spade","2 Diamond","2 Heart","3 Club","3 Spade","3 Diamond","3 Heart","4 Club","4 Spade","4 Diamond","4 Heart","5 Club","5 Spade","5 Diamond","5 Heart","6 Club","6 Spade","6 Diamond","6 Heart","7 Club","7 Spade","7 Diamond","7 Heart","8 Club","8 Spade","8 Diamond","8 Heart","9 Club","9 Spade","9 Diamond","9 Heart","10 Club","10 Spade","10 Diamond","10 Heart","Jack Club","Jack Spade","Jack Diamond","Jack Heart","Queen Club","Queen Spade","Queen Diamond","Queen Heart","King Club","King Spade","King Diamond","King Heart","Ace Club","Ace Spade","Ace Diamond","Ace Heart"];

let originaldeck = [...deck];

const shuffle = arr => arr.reduceRight((res, _, __, arr) => [...res, arr.splice(~~(Math.random() * arr.length), 1)[0]], []);

let PlayPoker = {

  setGameStart: function(classType) {
    this.dealPlayers(classType);
  },

  dealPlayers: function(classType) {
    let freshdeck01 = shuffle(deck);
    let p1Deal = freshdeck01.filter(function(value, index, arr) {
      return index < 2;
    });
    console.log(p1Deal);
  }
}

I have created one random set of two, but I need every possible set of two within an array, and set each element to the corresponding picture.(i.e. king hearts-queen heartspicture of the king of hearts called, and then the picture of queen of hearts called from markup within my cpu)

Aucun commentaire:

Enregistrer un commentaire