Skip to content

Instantly share code, notes, and snippets.

@thefrontender
Created April 15, 2012 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefrontender/2395346 to your computer and use it in GitHub Desktop.
Save thefrontender/2395346 to your computer and use it in GitHub Desktop.
Hearts card game in JS
/*
Timing and asynchronus events
http://javascript.info/tutorial/events-and-timing-depth
PubSubJS
https://github.com/mroderick/PubSubJS
http://jsperf.com/pubsubjs-vs-jquery-custom-events/6
http://maxpert.tumblr.com/post/2658433646/puny-standalone-javascript-custom-pubsub-events-library
http://higginsforpresident.net/js/static/jq.pubsub.js
http://darcyclarke.me/development/library-agnostic-pubsub-publish-subscribe/
CSS Playing Cards
https://github.com/selfthinker/CSS-Playing-Cards
*/
var Hearts = (function () {
var dealer = new Dealer(),
parties = 4,
tricks = 0,
Player,
game = {
players: [],
heartBroken: false,
gameOver: false
},
i = parties;
// player class
Player = function (pos) {
return {
index: pos,
hand: [],
score: 0,
play: function (trick, playCallback) {
var that = this,
suit = trick.cards.length ? trick.getSuit() : null,
filterSuit = function (suit) {
return that.hand.filter(function (element){
return (element[0] === suit);
});
},
suitCards = filterSuit(suit),
playableCards = suit && suitCards.length ? suitCards : this.hand,
theCard;
console.log('I need to play ', suit ? suit : 'anything');
console.log('I have ', suitCards.length ? suitCards : 'none');
// Not first player, and can follow suit
if (suit && suitCards.length > 0) {
// First trick of the hand or Hearts hasn't broken yet
} else if (!game.heartBroken || tricks % 13 === 1) {
// Get rid of
// Play anything
} else {
// Check for scoring cards, try to get rid of them
}
theCard = this.hand.splice(this.hand.indexOf(suitCards[0]), 1)[0];
// play one card
playCallback(theCard);
}
};
};
// initialise game
while (i--) {
game.players.push(new Player(i));
}
// the game loop
game.play = function () {
var trick = {
cards: [],
score: 0,
getSuit: function () {
return this.cards[0][0];
},
getWinner: function () {
var winner = 0,
cards = this.cards,
card,
i;
// reset score
this.score = 0;
for (i = 0; i < cards.length; i++) {
card = cards[i];
// return the index of the highest value card
if (card[0] === this.getSuit() && parseInt(card.slice(1), 10) > parseInt(cards[winner].slice(1), 10))
winner = i;
// calculate score
if (card[0] === '♥') {
this.score += 1;
if (!game.heartBroken) {
game.heartBroken = true;
console.log('Hearts broke!');
}
}
if (card === '♠12') this.score += 13;
}
return winner;
}
},
winner;
do {
// deal a new hand if needed
if (!(tricks % 13)) {
dealer.resetDeck();
game.heartBroken = false;
console.warn('Dealing again');
i = parties;
while (i--) {
game.players[i].hand = dealer.deal(13);
}
}
// reset counter, container
i = parties;
trick.cards.length = 0;
// play the cards for the trick
console.log('Trick', tricks)
while (i--) {
game.players[i].play(trick, function (card) {
trick.cards.push(card);
console.log(i, '. Player', game.players[i].index, '(', game.players[i].score, ') played', card);
});
}
// score the trick
winner = game.players[trick.getWinner()];
winner.score += trick.score;
if (winner.score > 99) {
game.over = true;
console.info('Game over! Player', winner.index, 'bombs out with a score of', winner.score);
} else {
// splice player array so that winner has first move next trick
game.players = game.players.concat(game.players.splice(0, trick.getWinner() + 1));
console.log(', Suit: ', trick.getSuit(), ', Winner: Player ', winner.index, ', Score: ', trick.score);
}
} while (!game.over && ++tricks)
}
return game;
}());
@thefrontender
Copy link
Author

Use Dealer class from https://gist.github.com/1149151

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment