Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************
- * Deck Class
- *
- * A new deck has 52 cards, each with a value and suit.
- *
- * shuffle()
- * draw()
- * score()
- * getWinner()
- *
- **************************************/
- // creates a deck with 52 cards. Each card has a value and suit
- class Deck {
- constructor() {
- this.deck = [],
- this.rankings = { 'Straight Flush' : 9, 'Four of a Kind' : 8, 'Full House' : 7,
- 'Flush' : 6, 'Straight' : 5, 'Three of a Kind' : 4,
- 'Two Pair' : 3, 'Pair' : 2, 'High Card' : 1 },
- this.cardValues = {'2' : 2, '3' : 3, '4' : 4, '5' : 5, '6' : 6, '7' : 7, '8' : 8,
- '9' : 9, '10' : 10, 'J' : 11, 'Q' : 12, 'K' : 13, 'A' : 14},
- this.faceCardValues = { 'J': 11, 'Q': 12, 'K': 13, 'A': 14 },
- this.highCardTieBreakers = ['Straight Flush', 'Flush', 'Straight', 'High Card'];
- var suits = ['hearts', 'spades', 'diamonds', 'clubs'],
- values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
- for (let suit of suits) {
- for (let value of values) {
- this.deck.push({ 'value': value, 'suit': suit });
- }
- }
- }
- }
- // removes 5 cards from deck
- // return a hand with 5 cards
- Deck.prototype.draw = function () {
- return this.deck.splice(0, 5);
- }
- // shuffle up cards using Fisher-Yates algorithm
- // there's a lodash function, _.shuffle(). to do this more easily
- Deck.prototype.shuffle = function () {
- var current = this.deck.length,
- temp, random;
- while (0 !== current) {
- random = Math.floor(Math.random() * current);
- current -= 1;
- temp = this.deck[current];
- this.deck[current] = this.deck[random];
- this.deck[random] = temp;
- }
- return 0;
- }
- // helper for score function
- Deck.prototype.flushOrStraight = function (hand) {
- var isFlush = false,
- isStraight = false,
- values = [];
- // see if hand is flush
- isFlush = hand.filter(function(item, i, arr) {
- return item.suit == arr[0].suit;
- }).length === hand.length;
- // see if hand is straight
- values = hand.map(function(card, i, arr) {
- if ( isNaN(parseInt(card.value)) ) {
- return this.faceCardValues[card.value];
- } else {
- return parseInt(card.value);
- }
- }, this);
- values.sort(function(a,b) { return a-b; });
- if (isFlush && isStraight) {
- return 'Straight Flush';
- } else if (isFlush) {
- return 'Flush';
- } else if (isStraight) {
- return 'Straight';
- } else {
- return 'High Card';
- }
- }
- // determines what type of hand it is
- Deck.prototype.score = function (hand) {
- var rank = '',
- values = [],
- numberOfAKind = [];
- values = hand.map(function(card, i, arr) {
- if ( isNaN(parseInt(card['value'])) ) {
- return this.faceCardValues[card.value];
- } else {
- return parseInt(card['value']);
- }
- }, this);
- values.sort(function(a,b) { return a-b; });
- // load numberOfAKind array. This tell us how many of each card is in a hand
- // values array is already sorted
- for (let i = 0; i < 5; i++) {
- if (i == 0) {
- numberOfAKind.push({'value': values[i], 'count': 1});
- } else {
- if ( numberOfAKind[numberOfAKind.length - 1]['value'] == values[i] ) {
- numberOfAKind[numberOfAKind.length - 1]['count'] += 1;
- } else {
- numberOfAKind.push({'value': values[i], 'count': 1});
- }
- }
- }
- // If only two types of cards then it's eith four of a kind or full housr.
- // Return whichever it is
- if (numberOfAKind.length == 2) {
- if (numberOfAKind[0].count == 4 || numberOfAKind[1].count == 4) {
- return {'hand' : 'Four of a Kind', 'numberOfAKind' : numberOfAKind};
- } else {
- return {'hand' : 'Full House', 'numberOfAKind' : numberOfAKind};
- }
- }
- // If only three types of cards we have either three of a kind or two pair
- // Return whichever it is
- if (numberOfAKind.length == 3) {
- for (let kind of numberOfAKind) {
- if (kind.count == 3) {
- return {'hand' : 'Three of a Kind', 'numberOfAKind' : numberOfAKind};
- }
- }
- return {'hand' : 'Two Pair', 'numberOfAKind' : numberOfAKind};
- }
- // If four types of cards then there must be a pair
- if (numberOfAKind.length == 4) {
- return {'hand' : 'Pair', 'numberOfAKind' : numberOfAKind};
- }
- // If 5 types of cards then mush have flush, straight or nothing
- if (numberOfAKind.length == 5) {
- return {'hand' : this.flushOrStraight(hand), 'numberOfAKind' : numberOfAKind};
- }
- }
- // helper for getWinner function to get high Card Value
- Deck.prototype.getHighCard = function (hand) {
- var highCardValue = hand.map(function(card) {
- if ( isNaN(parseInt(card.value)) ) {
- return this.faceCardValues[card.value];
- } else {
- return parseInt(card.value);
- }
- }, this).sort(function(a,b) { return a-b; });
- return highCardValue[highCardValue.length - 1];
- }
- // compares two hands and determines the winner
- Deck.prototype.getWinner = function (hand1, hand2) {
- var score1 = {},
- score2 = {},
- highCount = 0;
- highScore = 0;
- score1 = this.score(hand1);
- score2 = this.score(hand2);
- if (this.rankings[score1['hand']] > this.rankings[score2['hand']]) {
- return 'Hand 1 is the winner with a ' + score1['hand'] + '!';
- } else if (this.rankings[score1['hand']] < this.rankings[score2['hand']]) {
- return 'Hand 2 is the winner with a ' + score2['hand'] + '!';
- }
- // if neither of above checks result in a winner then both players have the
- // same hand. Find out if one is higher or we end in a draw
- // this block is only for sraight flush, flush, straight and high cards
- if ( this.highCardTieBreakers.indexOf(score1['hand']) > -1 ) {
- let highCardValue1 = this.getHighCard(hand1);
- let highCardValue2 = this.getHighCard(hand2);
- if ( highCardValue1 > highCardValue2 ) {
- return 'Hand 1 is the winner with a ' + score1['hand'] + '!';
- } else if (highCardValue1 < highCardValue2) {
- return 'Hand 2 is the winner with a ' + score2['hand'] + '!';
- } else {
- return 'We have a draw! Both players have a ' + score2['hand'];
- }
- }
- // for 4 of kind, full house, 3 of kind, two pair, pair
- for (let kind of score1.numberOfAKind) {
- if (kind.count > highCount) {
- highCount = kind.count;
- highScore = kind.value;
- }
- }
- for (let kind of score2.numberOfAKind) {
- if (kind.count == highCount && kind.value == highScore) {
- return 'We have a draw! Both players have a ' + score2['hand'];
- } else if (kind.count == highCount && kind.value > highScore) {
- return 'Hand 2 is the winner with a ' + score2['hand'] + '!'; }
- }
- return 'Hand 1 is the winner with a ' + score1['hand'] + '!';
- }
- var deck = new Deck();
- console.log("There are " + deck.deck.length + " cards in the deck. Let's play a hand.");
- deck.shuffle();
- var hand = deck.draw();
- var hand2 = deck.draw();
- console.log('Hand 1 is:');
- console.log(hand);
- console.log('Hand 2 is: ');
- console.log(hand2);
- console.log('There are now ' + deck.deck.length + ' cards in the deck.');
- console.log('Aaaaand...........');
- console.log(deck.getWinner(hand, hand2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement