daily pastebin goal
49%
SHARE
TWEET

set-v2.js

a guest Jul 25th, 2013 38 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.shuffle =  function() {
  2.     var c = this.length, temp, i;
  3.     while (c > 0) {
  4.         i = Math.floor(Math.random() * c);
  5.         c--;
  6.         temp = this[c];
  7.         this[c] = this[i];
  8.         this[i] = temp;
  9.     }
  10.     return this;
  11. };
  12.  
  13. function Deck(cards) {
  14.     if (cards == null) {
  15.         this.cards = Deck.CARDS.slice(0).shuffle();
  16.     } else {
  17.         this.cards = cards;
  18.     }
  19. }
  20.  
  21. Deck.CARDS = (function() {
  22.     var list = [];
  23.     for (var i = 0; i < 81; i++) {
  24.         list.push([
  25.             ~~(i / 27) % 3,
  26.             ~~(i /  9) % 3,
  27.             ~~(i /  3) % 3,
  28.             ~~(i /  1) % 3
  29.         ]);
  30.     }
  31.     return list;
  32. }());
  33.  
  34.  
  35. Deck.isSet = function(a, b, c) {
  36.     for (var i = 0; i < 4; i++) {
  37.         if (!(a[i] === b[i] && b[i] === c[i]) &&
  38.             !(a[i] !== b[i] && a[i] !== c[i] && b[i] !== c[i])) {
  39.             return false;
  40.         }
  41.     }
  42.     return true;
  43. };
  44.  
  45. Deck.prototype.deal = function(n, dest) {
  46.     dest = dest || new Deck([]);
  47.     for (var i = 0; i < n; i++) {
  48.         dest.cards.push(this.cards[i]);
  49.     }
  50.     this.cards = this.cards.slice(n);
  51.     return dest;
  52. };
  53.  
  54. Deck.prototype.isEmpty = function() {
  55.     return this.cards.length === 0;
  56. };
  57.  
  58. Object.defineProperty(Deck.prototype, 'length', {
  59.     get: function() {
  60.         return this.cards.length;
  61.     }
  62. });
  63.  
  64. Deck.prototype.findSet = function() {
  65.     var cards = this.cards, len = cards.length, result = false;
  66.     for (var i = 0; i < len - 2; i++) {
  67.         for (var j = i + 1; j < len - 1; j++) {
  68.             for (var k = j + 1; k < len - 0; k++) {
  69.                 if (Deck.isSet(cards[i], cards[j], cards[k])) {
  70.                     var set = [cards[i], cards[j], cards[k]];
  71.                     this.cards = this.cards.filter(function(e, ii) {
  72.                         return ii !== i && ii !== j && ii !== k;
  73.                     });
  74.                     return set;
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     return null;
  80. };
  81.  
  82. function Histo() {
  83.     this.data = {};
  84. }
  85.  
  86. Histo.prototype.increment = function(point) {
  87.     if (this.data[point] == null) this.data[point] = 0;
  88.     this.data[point]++;
  89. };
  90.  
  91. function monteCarlo(n) {
  92.     var histo = new Histo();
  93.     for (var i = 0; i < n; i++) {
  94.         var deck = new Deck(), dealt = deck.deal(9);
  95.         while (!deck.isEmpty()) {
  96.             deck.deal(3, dealt);
  97.             while (dealt.findSet() != null) {};
  98.             histo.increment(dealt.length);
  99.         }
  100.     }
  101.     return histo;
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top