SHOW:
|
|
- or go back to the newest paste.
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 | deck = [0...deckSize] | |
14 | $('<table>').appendTo 'body' | |
15 | for [1..testCount] | |
16 | do shuffle | |
17 | for i in [0...deckSize] | |
18 | if deck.indexOf(i) is -1 then throw "#{i} is not in the deck" | |
19 | if deck.length isnt 52 then throw "deck length is #{deck.length}; should be 52" | |
20 | plot deck | |
21 | ||
22 | plot = (deck) -> | |
23 | tr = $ '<tr>' | |
24 | for i in [0...deck.length] | |
25 | bg = 5 * deck[i] % 256 | |
26 | tr.append """ | |
27 | <td style='background-color:rgb(#{bg},#{bg},#{bg});'> | |
28 | | |
29 | </td>""" | |
30 | tr.appendTo 'table' | |
31 | ||
32 | $ () -> | |
33 | test deck | |
34 | *********************************************/ | |
35 | //Usage: | |
36 | //1. Include jQuery | |
37 | - | var deckSize, plot, rand, swap, test, testCount; |
37 | + | |
38 | var deck, deckSize, plot, rand, swap, test, testCount, _i, _results; | |
39 | ||
40 | deckSize = 52; | |
41 | ||
42 | deck = (function() { | |
43 | _results = []; | |
44 | for (var _i = 0; 0 <= deckSize ? _i < deckSize : _i > deckSize; 0 <= deckSize ? _i++ : _i--){ _results.push(_i); } | |
45 | return _results; | |
46 | }).apply(this); | |
47 | ||
48 | rand = function(min, max) { | |
49 | if (min == null) min = 0; | |
50 | if (max == null) max = 51; | |
51 | return Math.floor(Math.random() * (max - min + 1)) + min; | |
52 | }; | |
53 | ||
54 | swap = function(a, b) { | |
55 | - | var i, _i, _j, _k, _results, _results1; |
55 | + | |
56 | return _ref = [deck[b], deck[a]], deck[a] = _ref[0], deck[b] = _ref[1], _ref; | |
57 | - | _results = []; |
57 | + | |
58 | - | for (var _i = 0; 0 <= deckSize ? _i < deckSize : _i > deckSize; 0 <= deckSize ? _i++ : _i--){ _results.push(_i); } |
58 | + | |
59 | - | return _results; |
59 | + | |
60 | ||
61 | test = function(deck) { | |
62 | - | _results1 = []; |
62 | + | var i, _j, _k, _l, _results1, _results2; |
63 | - | for (_j = 1; 1 <= testCount ? _j <= testCount : _j >= testCount; 1 <= testCount ? _j++ : _j--) { |
63 | + | |
64 | _results1 = []; | |
65 | - | for (i = _k = 0; 0 <= deckSize ? _k < deckSize : _k > deckSize; i = 0 <= deckSize ? ++_k : --_k) { |
65 | + | for (var _j = 0; 0 <= deckSize ? _j < deckSize : _j > deckSize; 0 <= deckSize ? _j++ : _j--){ _results1.push(_j); } |
66 | return _results1; | |
67 | }).apply(this); | |
68 | $('<table>').appendTo('body'); | |
69 | _results2 = []; | |
70 | for (_k = 1; 1 <= testCount ? _k <= testCount : _k >= testCount; 1 <= testCount ? _k++ : _k--) { | |
71 | - | _results1.push(plot(deck)); |
71 | + | |
72 | for (i = _l = 0; 0 <= deckSize ? _l < deckSize : _l > deckSize; i = 0 <= deckSize ? ++_l : --_l) { | |
73 | - | return _results1; |
73 | + | |
74 | if (deck.length !== 52) { | |
75 | throw "deck length is " + deck.length + "; should be 52"; | |
76 | } | |
77 | - | var bg, i, tr, _i, _ref; |
77 | + | |
78 | _results2.push(plot(deck)); | |
79 | - | for (i = _i = 0, _ref = deck.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { |
79 | + | |
80 | return _results2; | |
81 | }; | |
82 | ||
83 | plot = function(deck) { | |
84 | var bg, i, tr, _j, _ref; | |
85 | tr = $('<tr>'); | |
86 | for (i = _j = 0, _ref = deck.length; 0 <= _ref ? _j < _ref : _j > _ref; i = 0 <= _ref ? ++_j : --_j) { | |
87 | bg = 5 * deck[i] % 256; | |
88 | tr.append("<td style='background-color:rgb(" + bg + "," + bg + "," + bg + ");'>\n \n</td>"); | |
89 | } | |
90 | return tr.appendTo('table'); | |
91 | }; | |
92 | ||
93 | $(function() { | |
94 | return test(deck); | |
95 | }); |