-
-
Save anton0xf/5950689 to your computer and use it in GitHub Desktop.
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
| /* | |
| * The round switches won't stay on unless something is placed on top of them. | |
| */ | |
| var robot = this; | |
| var thrusters = ['left', 'top', 'right', 'bottom']; | |
| var state = 0; | |
| var start = 'top'; | |
| var rules = [/* 0 */ [['bottom', 'left', 0], | |
| ['right', 'bottom', 0], | |
| ['left', 'right', 1]], | |
| /* 1 */ [['left', 'bottom', 2]], | |
| /* 2 */ [['top', 'left', 2], | |
| ['right', 'top', 2], | |
| ['left', ['left', 'bottom'], 3]], | |
| /* 3 */ [['top', 'left', 3]]]; | |
| function fire(thruster) { | |
| robot.thrusters[thruster](true); | |
| } | |
| function multiFire(thrusters) { | |
| if(Array.isArray(thrusters)){ | |
| for(var i=0; i<thrusters.length; i++){ | |
| var thruster = thrusters[i]; | |
| fire(thruster); | |
| } | |
| }else{ | |
| fire(thrusters); | |
| } | |
| } | |
| function stopAll() { | |
| for(var i=0; i<thrusters.length; i++){ | |
| robot.thrusters[thrusters[i]](false); | |
| } | |
| } | |
| function getState() { | |
| return state; | |
| } | |
| function setState(next) { | |
| console.log(state + " -> " + next); | |
| state = next; | |
| } | |
| function searchSensor(sensor, rules) { | |
| for(var i=0; i<rules.length; i++){ | |
| var rule = rules[i]; | |
| var curSensor = rule[0]; | |
| if(sensor == curSensor) | |
| return [rule[1], rule[2]]; | |
| } | |
| return null; | |
| } | |
| robot.on('start', function() { | |
| multiFire(start); | |
| }); | |
| for(var i=0; i<thrusters.length; i++){ | |
| var s = thrusters[i]; | |
| (function(){ | |
| var sensor = s; | |
| robot.on('sensor:'+sensor, function(contact) { | |
| if(contact){ | |
| var next = searchSensor(sensor, rules[getState()]); | |
| if(next != null){ | |
| console.log("sensor: " + sensor); | |
| var nextThruster = next[0]; | |
| var nextState = next[1]; | |
| stopAll(); | |
| multiFire(nextThruster); | |
| setState(nextState); | |
| } | |
| } | |
| }); | |
| })(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
solution for level 6 of Bitbucket Bit's Quest
see discussion on juick.com