Guest User

Untitled

a guest
Mar 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. class Deck {
  2. constructor() {
  3. this.deck = this.newDeck();
  4. }
  5.  
  6. // --- Generate a fresh, unshuffled deck! ---
  7. newDeck() {
  8. const suits = ["Clubs", "Diamonds", "Hearts", "Spades"],
  9. values = ["Two", "Three", "Four", "Five", "Six", "Seven",
  10. "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
  11. ];
  12. let cards = [];
  13.  
  14. suits.forEach(suit => {
  15. values.forEach((value, i) => {
  16. const card = {
  17. "suit": suit,
  18. "value": value,
  19. "worth": i
  20. }
  21. cards.push(card);
  22. });
  23. });
  24.  
  25. return cards;
  26. }
  27.  
  28. // --- Shuffle a deck n number of times! ---
  29. shuffle(n = 1) {
  30. while (n-- > 0) {
  31. let remainingCards = this.deck.length + 1,
  32. shuffledCards = [];
  33. while (remainingCards-- > 0) {
  34. const position = Math.floor(Math.random() * remainingCards),
  35. card = this.deck.splice(position, 1);
  36. shuffledCards.push(...card);
  37. }
  38. this.deck = shuffledCards;
  39. }
  40. }
  41.  
  42. }
Add Comment
Please, Sign In to add comment