Last active
December 13, 2015 23:39
-
-
Save phantom42/4992869 to your computer and use it in GitHub Desktop.
indexed arrays - putting it all together
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
//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()) ; | |
} |
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
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