Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Deck {
- constructor() {
- this.deck = this.newDeck();
- }
- // --- Generate a fresh, unshuffled deck! ---
- newDeck() {
- const suits = ["Clubs", "Diamonds", "Hearts", "Spades"],
- values = ["Two", "Three", "Four", "Five", "Six", "Seven",
- "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
- ];
- let cards = [];
- suits.forEach(suit => {
- values.forEach((value, i) => {
- const card = {
- "suit": suit,
- "value": value,
- "worth": i
- }
- cards.push(card);
- });
- });
- return cards;
- }
- // --- Shuffle a deck n number of times! ---
- shuffle(n = 1) {
- while (n-- > 0) {
- let remainingCards = this.deck.length + 1,
- shuffledCards = [];
- while (remainingCards-- > 0) {
- const position = Math.floor(Math.random() * remainingCards),
- card = this.deck.splice(position, 1);
- shuffledCards.push(...card);
- }
- this.deck = shuffledCards;
- }
- }
- }
Add Comment
Please, Sign In to add comment