Advertisement
Guest User

JavaScript shuffle visualizer (jQuery version)

a guest
Aug 27th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************ CoffeeScript source: ************
  2. deckSize = 52
  3. deck = [0...deckSize]
  4.  
  5. rand = (min = 0, max = 51) ->
  6.   Math.floor(Math.random() * (max - min + 1)) + min
  7.  
  8. swap = (a, b) ->
  9.   [deck[a], deck[b]] = [deck[b], deck[a]]
  10.  
  11. testCount = 52
  12. test = (deck) ->
  13.     $('<table>').appendTo 'body'
  14.     for [1..testCount]
  15.         do shuffle
  16.         plot deck        
  17.  
  18. plot = (deck) ->
  19.     tr = $ '<tr>'    
  20.     for i in [0...deck.length]
  21.         bg = 5 * deck[i] % 256
  22.         tr.append """
  23.             <td style='background-color:rgb(#{bg},#{bg},#{bg});'>
  24.                 &nbsp;
  25.             </td>"""
  26.     tr.appendTo 'table'
  27.    
  28. $ () ->
  29.     test deck
  30. *********************************************/
  31. //Usage:
  32. //1. Include jQuery
  33. //2. Declare your shuffle() function globally
  34.  
  35. var deck, deckSize, plot, rand, swap, test, testCount, _i, _results;
  36.  
  37. deckSize = 52;
  38.  
  39. deck = (function() {
  40.   _results = [];
  41.   for (var _i = 0; 0 <= deckSize ? _i < deckSize : _i > deckSize; 0 <= deckSize ? _i++ : _i--){ _results.push(_i); }
  42.   return _results;
  43. }).apply(this);
  44.  
  45. rand = function(min, max) {
  46.   if (min == null) min = 0;
  47.   if (max == null) max = 51;
  48.   return Math.floor(Math.random() * (max - min + 1)) + min;
  49. };
  50.  
  51. swap = function(a, b) {
  52.   var _ref;
  53.   return _ref = [deck[b], deck[a]], deck[a] = _ref[0], deck[b] = _ref[1], _ref;
  54. };
  55.  
  56. testCount = 52;
  57.  
  58. test = function(deck) {
  59.   var _j, _results1;
  60.   $('<table>').appendTo('body');
  61.   _results1 = [];
  62.   for (_j = 1; 1 <= testCount ? _j <= testCount : _j >= testCount; 1 <= testCount ? _j++ : _j--) {
  63.     shuffle();
  64.     _results1.push(plot(deck));
  65.   }
  66.   return _results1;
  67. };
  68.  
  69. plot = function(deck) {
  70.   var bg, i, tr, _j, _ref;
  71.   tr = $('<tr>');
  72.   for (i = _j = 0, _ref = deck.length; 0 <= _ref ? _j < _ref : _j > _ref; i = 0 <= _ref ? ++_j : --_j) {
  73.     bg = 5 * deck[i] % 256;
  74.     tr.append("<td style='background-color:rgb(" + bg + "," + bg + "," + bg + ");'>\n    &nbsp;\n</td>");
  75.   }
  76.   return tr.appendTo('table');
  77. };
  78.  
  79. $(function() {
  80.   return test(deck);
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement