Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************ CoffeeScript source: ************
- deckSize = 52
- deck = [0...deckSize]
- rand = (min = 0, max = 51) ->
- Math.floor(Math.random() * (max - min + 1)) + min
- swap = (a, b) ->
- [deck[a], deck[b]] = [deck[b], deck[a]]
- testCount = 52
- test = (deck) ->
- $('<table>').appendTo 'body'
- for [1..testCount]
- do shuffle
- for i in [0...deckSize]
- if deck.indexOf(i) is -1 then throw "#{i} is not in the deck"
- if deck.length isnt 52 then throw "deck length is #{deck.length}; should be 52"
- plot deck
- plot = (deck) ->
- tr = $ '<tr>'
- for i in [0...deck.length]
- bg = 5 * deck[i] % 256
- tr.append """
- <td style='background-color:rgb(#{bg},#{bg},#{bg});'>
-
- </td>"""
- tr.appendTo 'table'
- $ () ->
- test deck
- *********************************************/
- //Usage:
- //1. Include jQuery
- //2. Declare your shuffle() function globally
- var deck, deckSize, plot, rand, swap, test, testCount, _i, _results;
- deckSize = 52;
- deck = (function() {
- _results = [];
- for (var _i = 0; 0 <= deckSize ? _i < deckSize : _i > deckSize; 0 <= deckSize ? _i++ : _i--){ _results.push(_i); }
- return _results;
- }).apply(this);
- rand = function(min, max) {
- if (min == null) min = 0;
- if (max == null) max = 51;
- return Math.floor(Math.random() * (max - min + 1)) + min;
- };
- swap = function(a, b) {
- var _ref;
- return _ref = [deck[b], deck[a]], deck[a] = _ref[0], deck[b] = _ref[1], _ref;
- };
- testCount = 52;
- test = function(deck) {
- var i, _j, _k, _results1;
- $('<table>').appendTo('body');
- _results1 = [];
- for (_j = 1; 1 <= testCount ? _j <= testCount : _j >= testCount; 1 <= testCount ? _j++ : _j--) {
- shuffle();
- for (i = _k = 0; 0 <= deckSize ? _k < deckSize : _k > deckSize; i = 0 <= deckSize ? ++_k : --_k) {
- if (deck.indexOf(i) === -1) throw "" + i + " is not in the deck";
- if (deck.length !== 52) {
- throw "deck length is " + deck.length + "; should be 52";
- }
- }
- _results1.push(plot(deck));
- }
- return _results1;
- };
- plot = function(deck) {
- var bg, i, tr, _j, _ref;
- tr = $('<tr>');
- for (i = _j = 0, _ref = deck.length; 0 <= _ref ? _j < _ref : _j > _ref; i = 0 <= _ref ? ++_j : --_j) {
- bg = 5 * deck[i] % 256;
- tr.append("<td style='background-color:rgb(" + bg + "," + bg + "," + bg + ");'>\n \n</td>");
- }
- return tr.appendTo('table');
- };
- $(function() {
- return test(deck);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement