Created
April 15, 2012 23:37
-
-
Save thefrontender/2395346 to your computer and use it in GitHub Desktop.
Hearts card game in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use Dealer class from https://gist.github.com/1149151