Built with blockbuilder.org
Last active
January 5, 2017 06:05
-
-
Save mgold/2ef3afcedd3b41cf355290e7daa7e42c to your computer and use it in GitHub Desktop.
Zukei Puzzle Solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <style> | |
| body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
| .grid line { stroke: #aaa; stroke-width: 5px } | |
| .circles circle { fill: #444444; } | |
| </style> | |
| </head> | |
| <body> | |
| <script> | |
| var grid_resolution = 5; | |
| var grid_size = 400; | |
| var w = 960, h = 500; | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", w) | |
| .attr("height", h) | |
| .append("g") | |
| .attr("transform", "translate("+((w-grid_size)/2)+","+((h-grid_size)/2)+")") | |
| var s = d3.scaleLinear() | |
| .domain([0, grid_resolution-1]) | |
| .range([0, grid_size]) | |
| var s_px = function(x){ return s(x) + "px"; } | |
| // create the grid | |
| var grid = svg.append("g") | |
| .attr("class", "grid") | |
| grid.append("g") | |
| .attr("class", "horizontal") | |
| .selectAll("line") | |
| .data(d3.range(grid_resolution)) | |
| .enter() | |
| .append("line") | |
| .attr("x1", s.range()[0] + "px") | |
| .attr("x2", s.range()[1] + "px") | |
| .attr("y1", s_px) | |
| .attr("y2", s_px) | |
| grid.append("g") | |
| .attr("class", "vertical") | |
| .selectAll("line") | |
| .data(d3.range(grid_resolution)) | |
| .enter() | |
| .append("line") | |
| .attr("y1", s.range()[0] + "px") | |
| .attr("y2", s.range()[1] + "px") | |
| .attr("x1", s_px) | |
| .attr("x2", s_px) | |
| var solution = svg.append("g") | |
| .attr("class", "path") | |
| var circles = svg.append("g") | |
| .attr("class", "circles") | |
| var points = []; | |
| var coinFlip = function(){ | |
| return Math.random() < 0.5; | |
| } | |
| var render = function(showSolution){ | |
| if (showSolution){ | |
| computeAndRenderSolution(); | |
| }else{ | |
| circles.selectAll("circle").remove(); | |
| solution.selectAll("circle").remove(); | |
| points = []; | |
| generateNewPuzzle(); | |
| } | |
| } | |
| var generateNewPuzzle = function(){ | |
| for (var i = 0; i < grid_resolution; i++){ | |
| for (var j = 0; j < grid_resolution; j++){ | |
| if (coinFlip()){ | |
| points.push([i,j]) | |
| } | |
| } | |
| } | |
| points.forEach(function(p){ | |
| circles.append("circle") | |
| .attr("r", "16px") | |
| .attr("cx", s_px(p[0])) | |
| .attr("cy", s_px(p[1])) | |
| }) | |
| } | |
| var showingSolution = false; | |
| d3.select("body").on("click", function(){ | |
| showingSolution = !showingSolution; | |
| render(showingSolution); | |
| }) | |
| render(showingSolution); | |
| </script> | |
| </body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment