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 | $('<table>').appendTo 'body' | |
14 | for [1..testCount] | |
15 | - | do shuffle |
15 | + | do shuffle |
16 | - | plot deck |
16 | + | for i in [0...deckSize] |
17 | if deck.indexOf(i) is -1 then throw "#{i} is not in the deck" | |
18 | if deck.length isnt 52 then throw "deck length is #{deck.length}; should be 52" | |
19 | plot deck | |
20 | ||
21 | plot = (deck) -> | |
22 | tr = $ '<tr>' | |
23 | for i in [0...deck.length] | |
24 | bg = 5 * deck[i] % 256 | |
25 | tr.append """ | |
26 | <td style='background-color:rgb(#{bg},#{bg},#{bg});'> | |
27 | | |
28 | </td>""" | |
29 | tr.appendTo 'table' | |
30 | ||
31 | $ () -> | |
32 | test deck | |
33 | *********************************************/ | |
34 | //Usage: | |
35 | //1. Include jQuery | |
36 | //2. Declare your shuffle() function globally | |
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 _ref; | |
56 | return _ref = [deck[b], deck[a]], deck[a] = _ref[0], deck[b] = _ref[1], _ref; | |
57 | }; | |
58 | ||
59 | - | var _j, _results1; |
59 | + | |
60 | ||
61 | test = function(deck) { | |
62 | var i, _j, _k, _results1; | |
63 | $('<table>').appendTo('body'); | |
64 | _results1 = []; | |
65 | for (_j = 1; 1 <= testCount ? _j <= testCount : _j >= testCount; 1 <= testCount ? _j++ : _j--) { | |
66 | shuffle(); | |
67 | for (i = _k = 0; 0 <= deckSize ? _k < deckSize : _k > deckSize; i = 0 <= deckSize ? ++_k : --_k) { | |
68 | if (deck.indexOf(i) === -1) throw "" + i + " is not in the deck"; | |
69 | if (deck.length !== 52) { | |
70 | throw "deck length is " + deck.length + "; should be 52"; | |
71 | } | |
72 | } | |
73 | _results1.push(plot(deck)); | |
74 | } | |
75 | return _results1; | |
76 | }; | |
77 | ||
78 | plot = function(deck) { | |
79 | var bg, i, tr, _j, _ref; | |
80 | tr = $('<tr>'); | |
81 | for (i = _j = 0, _ref = deck.length; 0 <= _ref ? _j < _ref : _j > _ref; i = 0 <= _ref ? ++_j : --_j) { | |
82 | bg = 5 * deck[i] % 256; | |
83 | tr.append("<td style='background-color:rgb(" + bg + "," + bg + "," + bg + ");'>\n \n</td>"); | |
84 | } | |
85 | return tr.appendTo('table'); | |
86 | }; | |
87 | ||
88 | $(function() { | |
89 | return test(deck); | |
90 | }); |