maurogestoso
9/4/2017 - 6:48 PM

AutoBlackJack.js

const _ = require('underscore');

class BlackJackGame {
  constructor (deck) {
    deck.shuffle();
    this.deck = deck;
    this.playersHand = [];
    this.dealersHand = [];
    this.playersScore = 0;
    this.dealersScore = 0;
  }
  dealPlayersHand () {
    // Deal initial hand (2 cards)
    this.playersHand.push(this.deck.dealCard())
    this.playersScore += this.playersHand[this.playersHand.length - 1].value;
    this.playersHand.push(this.deck.dealCard())
    this.playersScore += this.playersHand[this.playersHand.length - 1].value;

    // Keep dealing cards while score is less than 15
    while (this.playersScore < 15) {
      this.playersHand.push(this.deck.dealCard())
      this.playersScore += this.playersHand[this.playersHand.length - 1].value;
    }
  }
  dealDealersHand () {
    // Deal initial hand (2 cards)
    this.dealersHand.push(this.deck.dealCard())
    this.dealersScore += this.dealersHand[this.dealersHand.length - 1].value;
    this.dealersHand.push(this.deck.dealCard())
    this.dealersScore += this.dealersHand[this.dealersHand.length - 1].value;

    // Keep dealing cards while score is less than 17
    while (this.dealersScore < 17) {
      this.dealersHand.push(this.deck.dealCard())
      this.dealersScore += this.dealersHand[this.dealersHand.length - 1].value;
    }
  }

  determineWinner () {

    if (isBust(this.playersScore) || this.dealersScore > this.playersScore) {
      return 'The House wins!'
    } else if (isBust(this.dealersScore) || this.dealersScore < this.playersScore) {
      return 'Player wins!'
    } else {
      return 'It\'s a tie';
    }

    function isBust (score) {
      return score > 21;
    }

  }

}

class Deck {
  constructor () {
    this.cards = [];

    const faces = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'];
    const suits = ['H', 'D', 'C', 'S'];

    suits.forEach(function (suit) {
      faces.forEach(function (face) {
        var card = new Card(face, suit);
        this.cards.push(card);
      }, this)
    }, this)
  }
  shuffle () {
    this.cards = _.shuffle(this.cards);
  }
  dealCard () {
    return this.cards.shift();
  }
}

class Card {
  constructor (face, suit) {
    // this = {} with prototypal link to Card.prototype
    var value;
    switch (face) {
      case 'A':
        value = 1;
        break;
      case 'J':
      case 'Q':
      case 'K':
        value = 10;
        break;
      default:
        value = face;
    }
    this.value = value;
    this.face = face;
    this.suit = suit;
    // return this
  }

  logValue () {
    console.log(this.value);
  }

}

var game = new BlackJackGame(new Deck());

game.dealPlayersHand();
game.dealDealersHand();
console.log('Player\s score:', game.playersScore)
console.log('Dealer\s score:', game.dealersScore)
console.log(game.determineWinner());



module.exports = {
  Card: Card,
  Deck: Deck
}

// function forEach (list, func, context) {
//   for () {
//     func.call(context, list[i], i, list)
//   }
// }