Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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))
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var width = 500,
height = 281;
function doGraph(selector, graph) {
var color = d3.scale.category10();
var force = d3.layout.force()
.charge(-120)
.linkDistance(60)
.size([width, height]);
var svg = d3.select(selector).append("svg")
.attr("width", width)
.attr("height", height);
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return JSON.stringify(d); });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
}
// use set.json
doGraph('#graph-set', {nodes: [], links: []})
// use set-mine.json
doGraph('#graph-set-mine', {nodes: [], links: []})
// use mine.json
doGraph('#graph-mine', {nodes: [], links: []})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.