Created
January 1, 2016 19:11
-
-
Save taptapdan/1f8a3e63458b7f4884eb to your computer and use it in GitHub Desktop.
CodeCombat: Black Diamond Solution
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
| // track positions for safe route back to center | |
| var positionStack = []; | |
| // track last target, to determine changes | |
| var lastTarget = { x: 40, y: 34 }; | |
| loop { | |
| var gem = this.findNearest(this.findItems()); | |
| if (gem) { | |
| // The isPathClear method tells you if there’s an obstacle in the way. | |
| var clear = this.isPathClear(this.pos, gem.pos); | |
| if (clear) { | |
| // If it’s clear, move() to gem.pos. | |
| this.move(gem.pos); | |
| // When we head towards a new gem, place the previous | |
| // location onto the positionStack for safe travel back. | |
| if ( gem.pos != lastTarget ) { | |
| positionStack.push( lastTarget ); | |
| lastTarget = gem.pos; | |
| } | |
| } else { | |
| // Else, move back to the center point. | |
| // Use the positionStack to travel back to center. | |
| while ( positionStack.length > 0 ) { | |
| var pos = positionStack.pop(); | |
| this.moveXY(pos.x, pos.y); | |
| } | |
| // Always put the center location back onto the stack. | |
| lastTarget = { x: 40, y: 34 }; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment