It is beyond anything you have ever experienced or imagined
Useful JavaScript Game Extensions: Array#shuffle
Part 7/256
Shuffling is important, especially in card games.
/** * Returns a new array with the elements all shuffled up. * * @returns A new array that is randomly shuffled. * @type Array */ Array.prototype.shuffle = function() { var shuffledArray = []; this.each(function(element) { shuffledArray.splice(rand(shuffledArray.length + 1), 0, element); }); return shuffledArray; };
Notice how building on the previous components of rand and each makes this method much easier to write. You’ll find that when you surround yourself with great APIs things constantly become easier and easier, whereas when you are surrounded with terrible APIs things become harder and harder.
| Print article | This entry was posted by Daniel X Moore on September 6, 2010 at 12:08 am, and is filed under 256 JavaScript Game Extensions, Game Development, Programming. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

