Skip to content

Instantly share code, notes, and snippets.

@phantom42
Last active December 13, 2015 23:39
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 phantom42/4992869 to your computer and use it in GitHub Desktop.
Save phantom42/4992869 to your computer and use it in GitHub Desktop.
indexed arrays - putting it all together
//here's our players array from exercise 1
var players = [];
players[0] = {'name': 'Robert', hand: []};
players[1] = {'name': 'Joe', hand: []};
//here's our code to create the deck
var suits = ['clubs','diamonds','hearts','spades'];
var ranks = [2,3,4,5,6,7,8,9,10,'J','Q','K','A'];
var deck = [];
for (var i=0;i<suits.length;i++) {
for (var j=0;j<ranks.length;j++) {
var card = {'rank': ranks[j], 'suit':suits[i]};
deck.push(card);
}
}
//This will shuffle the deck. Nothing for you to do here. Just here to
//make the final output a little more realistic
deck.sort(function() {return 0.5 - Math.random()});
//Deal 5 cards to each player. Make sure you deal them out
//one player at a time, just like in a real poker game.
for (var i = 0 ; i < 5 * players.length ; i++) {
players[i % players.length].hand.push(deck.shift()) ;
}
var deck = [] ;
var ranks = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"] ;
var suits = ["clubs","diamonds","hearts","spades"] ;
for (var i = 0 ; i < (ranks.length * suits.length) ; i++) {
deck[deck.length] = {"rank":ranks[i % ranks.length], "suit":suits[i % suits.length]} ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment