Skip to content

Instantly share code, notes, and snippets.

@tlhunter
Created March 9, 2014 05:50
Show Gist options
  • Save tlhunter/9443406 to your computer and use it in GitHub Desktop.
Save tlhunter/9443406 to your computer and use it in GitHub Desktop.
Node.js Blackjack using the Prototype and Constructor Approach
var readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// CARD CLASS
var Card = function(face, suite) {
if (this.faces.indexOf(face) == -1) {
throw new Error("Invalid Face: " + face);
}
if (this.suites.indexOf(suite) == -1) {
throw new Error("Invalid Suite: " + suite);
}
this.face = face;
this.suite = suite;
};
Card.prototype.faces = [ 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K' ];
Card.prototype.suites = [ 'Hearts', 'Spades', 'Diamonds', 'Clubs' ];
// CARD COLLECTION CLASS
var CardCollection = function() {
this.cards = []; // Array of Card instances
};
// HAND CLASS
var Hand = function() { };
Hand.prototype = new CardCollection();
Hand.prototype.constructor = Hand;
Hand.prototype.limit = 21;
Hand.prototype.add = function(card) {
if (!card instanceof Card) {
throw new Error("Tried adding a non card to hand!");
}
this.cards.push(card);
};
Hand.prototype.score = function() {
var score = 0, aces = 0;
this.cards.forEach(function(card) {
if (card.face == 'A') {
aces++;
}
if (card.face == 'J' || card.face == 'Q' || card.face == 'K') {
score += 10;
} else if (card.face == 'A') { // Optimistically assume high values for aces
score += 11;
} else {
score += parseInt(card.face, 10);
}
});
while (score > 21 && aces > 0) {
score -= 10;
aces --;
}
return score;
};
Hand.prototype.toString = function() {
console.log("Your Hand:");
this.cards.forEach(function(card) {
console.log(" " + card.face + " of " + card.suite);
});
console.log("Total Points: " + this.score() + "\n");
};
Hand.prototype.bust = function() {
return this.score() > this.limit;
};
// DECK CLASS
var Deck = function() {
var self = this;
Card.prototype.suites.forEach(function(suite) {
Card.prototype.faces.forEach(function(face) {
self.cards.push(new Card(face, suite));
});
});
};
Deck.prototype = new CardCollection();
Deck.prototype.constructor = Deck;
Deck.prototype.draw = function() {
return this.cards.pop();
};
Deck.prototype.shuffle = function() {
for (var j, x, i = this.cards.length; i; j = Math.floor(Math.random() * i), x = this.cards[--i], this.cards[i] = this.cards[j], this.cards[j] = x);
};
// PROCEDURAL GAME CODE
var deck = new Deck();
deck.shuffle();
var player1 = new Hand();
player1.add(deck.draw());
player1.add(deck.draw());
var play = function() {
console.log(player1.toString());
readline.question("[H]it or [S]tay? ", function(answer) {
if (answer == 'h' || answer == 'H') {
player1.add(deck.draw());
if (player1.bust()) {
console.log("BUST!");
console.log(player1.toString());
readline.close();
} else {
console.log("HITTING!");
play();
}
} else if (answer == 's' || answer == 'S') {
console.log("STAYING!");
console.log(player1.toString());
readline.close();
}
});
}
play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment