{{ message }}

Instantly share code, notes, and snippets.

# samlecuyer/generate.js

Created Oct 27, 2015
 var expect = require('chai').expect; var _ = require('lodash'); var fs = require('fs'); // This generates a set of unique dominoes up to a double-n (dn) function generate_set(dn) { var set = []; for (i = 0; i <= dn; i++) { for (j = i; j <= dn; j++) { set.push([i, j]); } } expect(set.length).to.eql(((dn + 1) * (dn + 2))/2); return set; // shuffle it so we can draw a random tile. return _.shuffle(set); } // [1,2] and [2,1] are the same tile // p and c are bad names function sameTile(p) { return function(c) { return (c[0] === p[0] && c[1] === p[1]) || (c[0] === p[1] && c[1] === p[0]); } } expect(sameTile([1,3])([2,3])).to.be.not.ok; expect(sameTile([1,3])([1,3])).to.be.ok; expect(sameTile([1,3])([3,1])).to.be.ok; // Can a tile be chained onto the end of an existing chain? function canPair(p) { return function(c) { return c[0] === p || c[1] === p; } } expect(canPair(10)([10,3])).to.be.ok; expect(canPair(10)([3,10])).to.be.ok; expect(canPair(10)([3,1])).to.not.be.ok; var set = generate_set(8); var start = [0,0]; var mine = _.chain(set).reject(sameTile(start)).shuffle().take(12).value(); function doGen(_set, _mine) { var nodes = _set.map(function(n) { var group = '1'; if (sameTile(n)(start)) { group = '2'; } else if (_.contains(_mine, n)) { group = '3'; } return { name: n.toString(), group: group } }); var links = _set.reduce(function(prev, curr, i, arr) { arr.forEach(function(other, j) { if (sameTile(other)(curr)) { return; } if (canPair(curr[0])(other) || canPair(curr[1])(other)) { prev.push({ source: i, target: j, value: 1 }); } }); return prev; }, []); var json = { nodes: nodes, links: links }; return json } var total = doGen(set, []) var total2 = doGen(set, mine) var total3 = doGen(_(mine).push(start).value(), mine) fs.writeFileSync('set.json', JSON.stringify(total)) fs.writeFileSync('set-mine.json', JSON.stringify(total2)) fs.writeFileSync('mine.json', JSON.stringify(total3))

to join this conversation on GitHub. Already have an account? Sign in to comment