Skip to content

Instantly share code, notes, and snippets.

@LukeTully
Created December 1, 2016 04:45
Show Gist options
  • Save LukeTully/f2033f2fe61c1ce22d273a8967e0fc51 to your computer and use it in GitHub Desktop.
Save LukeTully/f2033f2fe61c1ce22d273a8967e0fc51 to your computer and use it in GitHub Desktop.
class Card {
constructor(suit, face, worth) {
this.suit = suit;
this.face = face;
this.worth = worth;
}
}
class CardDeck {
deck = []
addCard(card) {
this.deck.push(card);
}
getRandomCard() {
debugger;
// Return a random card from the deck
return this.deck[getRandomInt(1, this.deck.length)];
}
seedDeck() {
const suits = ["clubs", "hearts", "diamonds", "spades"];
const faces = [{
name: "king",
worth: 13
}, {
name: "queen",
worth: 12
}, {
name: "jack",
worth: 11
}, {
name: "ten",
worth: 10
}, {
name: "nine",
worth: 9
}, {
name: "eight",
worth: 8
}, {
name: "seven",
worth: 7
}, {
name: "six",
worth: 6
}, {
name: "five",
worth: 5
}, {
name: "four",
worth: 4
}, {
name: "three",
worth: 3
}, {
name: "two",
worth: 2
}, {
name: "ace",
worth: 0
}, ];
faces.forEach((face) => {
suits.forEach((suit) => {
this.addCard(
new Card(suit, face.name, face.worth)
);
});
});
}
}
let deck1 = new CardDeck();
deck1.seedDeck();
let newCard = deck1.getRandomCard();
console.log(newCard.worth);
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
export {Card, CardDeck}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment