Last active
April 27, 2016 03:16
-
-
Save gartenfeld/e38b906b00ba350a5a61426c3259bfb4 to your computer and use it in GitHub Desktop.
Maze Explorer Draft
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
| var app = require('express')(); | |
| var puzzle = require('./flipboard/maze'); | |
| app.get('/maze/new', puzzle.init); | |
| app.get('/maze/update/:id', puzzle.update); | |
| app.listen(3000, 'localhost'); |
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
| var fetch = require('request'); | |
| var API = 'https://challenge.flipboard.com/step'; | |
| var Explorer = function(team) { | |
| this.team = team; | |
| this.history = ''; | |
| }; | |
| Explorer.prototype.go = function(x, y) { | |
| var url = API + '?s=' + this.team.maze + '&x=' + x + '&y=' + y; | |
| var me = this; | |
| fetch(url, function (err, data) { | |
| data = JSON.parse(data.body); | |
| var found = data.end; | |
| var letter = data.letter; | |
| var paths = data.adjacent.filter(function(cell) { | |
| var key = JSON.stringify([cell.x, cell.y]); | |
| return !me.team.map[key]; | |
| }); | |
| var codes = { | |
| 0: 'deadend', | |
| 1: 'continue' | |
| }; | |
| var status = codes[paths.length] || 'fork'; | |
| var here = JSON.stringify([x, y]); | |
| me.history += me.history === '' ? '' : '_'; | |
| me.history += here; | |
| if (found) { | |
| me.team.finished = true; | |
| me.team.map.answer = me.history.split('_'); | |
| } | |
| me.team.map[here] = { | |
| letter: letter, | |
| status: status | |
| }; | |
| me.team.publish(); | |
| if (!me.team.finished && paths.length) { | |
| var first = paths.shift(); | |
| paths.forEach(function(next) { | |
| var buddy = new Explorer(me.team); | |
| buddy.history = me.history; | |
| buddy.go(next.x, next.y); | |
| }); | |
| me.go(first.x, first.y); | |
| } | |
| }); | |
| }; | |
| module.exports = Explorer; |
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
| var fetch = require('request'); | |
| var Team = require('./team'); | |
| var URL = 'https://challenge.flipboard.com/start'; | |
| var RE = /(?:s=)(.+)(?=&x)/; | |
| var teams = {}; | |
| var Maze = {}; | |
| Maze.init = function(req, res) { | |
| fetch(URL, function (err, data) { | |
| var query = data.request.uri.query || ''; | |
| var match = query.match(RE); | |
| if (match) { | |
| res.send(match[1]); | |
| } | |
| }); | |
| }; | |
| Maze.update = function(req, res) { | |
| var maze = req.params.id; | |
| var team = teams[maze] = teams[maze] || new Team(maze); | |
| team.subscribe(res.json.bind(res)); | |
| }; | |
| module.exports = Maze; |
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
| var Explorer = require('./explorer'); | |
| var Team = function(maze) { | |
| this.map = { answer: [] }; | |
| this.maze = maze; | |
| this.started = false; | |
| this.finished = false; | |
| this.audience = []; | |
| }; | |
| Team.prototype.subscribe = function(cb) { | |
| this.audience.push(cb); | |
| if (!this.started) { | |
| new Explorer(this).go(0, 0); | |
| this.started = true; | |
| } | |
| if (this.finished) { | |
| this.publish(); | |
| } | |
| }; | |
| Team.prototype.publish = function() { | |
| while (this.audience.length) { | |
| this.audience.shift()(this.map); | |
| } | |
| }; | |
| module.exports = Team; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment