mr-squishy-cardmaster
Keep track of card games with players and current hands.
installation
node:
$ npm install mr-squishy-cardmaster
component:
$ component install kitto1/mr-squishy-cardmaster
usage
You pass mr-squishy an array of 'hands'.
Each hand represents a player in the card game and is an array of cards in the hand.
mr-squishy keeps track of the hands so you can:
- get the next card from the top of a players hand
- put a card onto the bottom of a players hand
- ask how many cards are in a players hand
Each card is an object with any properties you want - mr-squishy just looks after the dealing.
creating hands
First - lets create some hands using decked and scruffy-shuffle
var decked = var scruffyShuffle = var cardmaster = // get a deck of cards with aces lowvar deck = // get cards that exclude face cardsvar cards = // split the deck into 2 playersvar hands =
running games
Now we have an array of player hands (each hand being an array of objects) - we can create a dealer using mr-squishy.
We could play a round of a game where one player 'wins' 2 cards from the other. This would result in putting 4 cards on the bottom of the winning players hand:
var game = var player1 = gamevar player2 = game // deal 2 cards from the top of player 1's hand into a new arrayvar player1Cards = player1 // deal 2 cards from the top of player 2's hand into a new arrayvar player2Cards = player2 // decide that player 2 wins all 4 cards// and put them back onto the bottom of player 2's handplayer2
api
var game = cardmaster(hands)
Create a new game with the given hands of cards.
hands is an 2d array - each top level is a player and each element in the player array is a card in their hand.
// hands for 2 players - each player has 2 cardsvar hands = name:'jack' number:11 suit:'diamond' name:'jack' number:11 suit:'spade' name:'jack' number:11 suit:'club' name:'jack' number:11 suit:'heart' var game =
var player = game.player(index)
Get a reference to a players individual hand - index is zero-based so pass 0 for player1
var game = var player1 = gamevar player2 = game
var cards = player.hand()
Return the full list of cards in the players hand
var card = player.takeCard(position)
Take a single card from a players hand
Position can be 'top' or 'bottom' (top is default).
player.putCard(card, position)
Return a single card to a players hand
Position can be 'top' or 'bottom' (bottom is default).
var cards = player.takeCards(number, position)
Take some cards from a players hand - this alters the count for that hand (because it removes the cards from the hand)
Count is the number of cards to return in the array.
Position can be 'top' or 'bottom' (top is default).
player.putCards(cards, position)
Insert some cards back into a players hand - cards is an array of the card objects.
Position can be 'top' or 'bottom' (bottom is default)
var count = player.count()
Return a number representing how many cards a player has in their hand.
license
MIT