Skip to content

Instantly share code, notes, and snippets.

@kthakore
Last active May 25, 2017 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kthakore/f6d9117fd4f0f1369ba59ad0b5713d01 to your computer and use it in GitHub Desktop.
Save kthakore/f6d9117fd4f0f1369ba59ad0b5713d01 to your computer and use it in GitHub Desktop.
Backjack in JS OOP
const _ = require('lodash');
/*
* Card
*/
class Card {
constructor(game, deck, options) {
this.game = game;
this.deck = deck;
// TODO: move game descriptions to a config file
if (game === "BLACKJACK") {
//CARDS in blackjack have Suits defined by the SUIT enum.
//Only one suit and number is allowed per dec
//WHICH HAS 4 SUITS
//Get number of cards left. Cards in 52 deck have
// face value and suit and color
// console.log(options.face_value, options.suit);
if (!options || !options.suit || !options['face_value']) {
throw new Error(`Missing suit or face_value`);
} else {
this.suit = options.suit;
this.value = options.face_value;
}
} else {
throw new Error(`${game} is not supported, yet`);
}
}
get color() {
// TODO: abstract out to a descriptor class
if (this.suit == "SPADE" || this.suit == "CLOVER") {
return "BLACK"
} else {
return "RED"
}
}
}
/*
* Deck
*/
class Deck {
constructor(game) {
this.game = game;
this._cards = [];
if (game === "BLACKJACK") {
//BLACK JACK IS PLAYED WITH A DECK OF 52 CARDS
//WHICH HAS 4 SUITS
_.each(["SPADE", "HEART", "CLOVER", "DIAMOND"], (suit) => {
_.times(13, (face_value) => {
this.createCard({
suit: suit,
face_value: face_value + 1
});
});
});
} else {
throw new Error(`${game} is not supported, yet`);
}
}
get cards() {
return this._cards
}
get length() {
return this._cards.length
}
createCard(options) {
// console.log(`Create (${this.length}) card for the same game ${this.game} with ${options}`);
let card = new Card(this.game, this, options);
this.cards.push(card);
}
PickCard() {
if (this.length > 0 ) {
var idx = Math.floor(Math.random()*this.length);
return this.cards.splice(idx, 1)[0];
}
}
}
class BlackJackHand {
constructor(player) {
this.cards = [];
this.player = player
}
addCard(card) {
// TODO: Check Card is of type CARD
this.cards.push(card);
this.checkScore();
}
checkScore() {
if (this.isBust) {
console.log(`Game Over, ${this.player} busted`);
}
if (this.isBlackJack) {
console.log(`Game Over, ${this.player} Won!`);
}
}
get isGameOver() {
return this.isBust || this.isBlackJack;
}
get score() {
var score = 0;
_.each(this.cards, (card) => {
//console.log(card, score, "FACE", card.value);
score = score + card.value;
});
// console.log(`${this.player} score: ${score} `);
return score;
}
get isBust() {
return this.score > 21;
}
get isBlackJack() {
return this.score == 21;
}
}
var blackJackDeck = new Deck("BLACKJACK");
var playerHand = new BlackJackHand("Player");
var dealerHand = new BlackJackHand("Dealer");
while(!(playerHand.isGameOver || dealerHand.isGameOver)) {
if (blackJackDeck.length > 0) {
var dealerCard = blackJackDeck.PickCard();
dealerHand.addCard(dealerCard);
console.log(`Dealer's card is: ${dealerCard.value} of ${dealerCard.suit} and score is ${dealerHand.score}`);
var playerCard = blackJackDeck.PickCard();
playerHand.addCard(playerCard);
console.log(`player's card is: ${playerCard.value} of ${playerCard.suit} and score is ${playerHand.score}`);
} else {
console.log('Out of cards, tie!');
}
}
{
"name": "bj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment