Last active
August 4, 2018 17:10
-
-
Save pedropmota/5d43a1335692862dbf747b1e925deb64 to your computer and use it in GitHub Desktop.
An example of a deck of poker cards. Contains the Enum and Class definitions. At the end, there's an example of how to use the deck.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Helper function that creates an object based on an array of values, | |
* setting each key/value with an item from the array. | |
* @param {Array} array The array of values | |
*/ | |
function createEnum(array) { | |
return array.reduce((object, value) => { | |
object[value] = value; | |
return object; | |
}, {}) | |
} | |
/** | |
* Enum of the 4 card suits | |
*/ | |
const Suits = createEnum([ | |
'HEART', 'SPADE', 'CLUB', 'DIAMOND' | |
]) | |
/** | |
* Enum of card values | |
*/ | |
const Values = createEnum([ | |
'A', '1', '2', '3', '4', '5', '6', '7', '8', '9', | |
'J', 'Q', 'K' | |
]) | |
/** | |
* Represents a poker card, with a "value" property and a "suit" property. | |
*/ | |
class Card { | |
constructor(value, suit) { | |
this.value = value; | |
this.suit = suit; | |
} | |
} | |
/** | |
* Represents a poker Deck. | |
* A newly instanciated Deck contains all the 52 cards. | |
* Contains methods for shuffling and dealing the deck's cards. | |
*/ | |
class Deck { | |
/** | |
* Creates a new Deck containing all 52 cards. | |
*/ | |
constructor() { | |
this.cards = this._createDeck(); | |
} | |
/** | |
* Shuffles the deck's cards. | |
*/ | |
shuffle() { | |
const shuffledCards = this.cards.slice(0); | |
for (let i = 0; i < shuffledCards.length; i++) { | |
const randomIndex = Math.floor(Math.random() * shuffledCards.length) | |
const temp = shuffledCards[i]; | |
shuffledCards[i] = shuffledCards[randomIndex]; | |
shuffledCards[randomIndex] = temp; | |
} | |
this.cards = shuffledCards; | |
} | |
/** | |
* Removes and returns the first card on the deck. | |
*/ | |
dealOneCard() { | |
if (!this.cards.length) | |
console.warn('No more cards in the deck! You should start a new game :)') | |
return this.cards.shift(); | |
} | |
/** | |
* Returns all 52 possible cards | |
*/ | |
_createDeck() { | |
const cards = []; | |
Object.values(Values).forEach(value => { | |
Object.values(Suits).forEach(suit => { | |
cards.push(new Card(value, suit)) | |
}) | |
}) | |
return cards; | |
} | |
} | |
//Playing around with the deck: | |
/** | |
* Deals and returns a specified number of cards from a deck | |
*/ | |
function drawCards(deck, numberOfCards) { | |
return [...Array(numberOfCards)].map(() => | |
deck.dealOneCard() | |
) | |
} | |
const deck = new Deck(); | |
deck.shuffle(); | |
const pedrosInitialCards = drawCards(deck, 5); | |
const opponent1InitialCards = drawCards(deck, 5); | |
const opponent2InitialCards = drawCards(deck, 5); | |
console.log("Pedro's cards:") | |
pedrosInitialCards.forEach((card) => { | |
console.log(card) | |
}) | |
console.log("Opponent 1's cards:") | |
opponent1InitialCards.forEach((card) => { | |
console.log(card) | |
}) | |
console.log("Opponent 2's cards:") | |
opponent2InitialCards.forEach((card) => { | |
console.log(card) | |
}) | |
console.log("Starting a new game! The opponents are still having a hard time beating Pedro, the all-time favorite!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment