Skip to content

Instantly share code, notes, and snippets.

@cyberbikepunk
Created October 13, 2015 22:26
Show Gist options
  • Save cyberbikepunk/a0622af8c4bc051bfec5 to your computer and use it in GitHub Desktop.
Save cyberbikepunk/a0622af8c4bc051bfec5 to your computer and use it in GitHub Desktop.
Quizz for BaseCase
/*
This AI simply saves the planet.
*/
function find_closest_rock(rocks){
var closest = 0
for (var i = 1; i < rocks.length; i++){
if (rocks[i].distance < rocks[closest].distance){
closest = i
}
}
return rocks[closest]
}
function align_paddle_and_wait(delta, distance){
var paddle_moves = [];
var increment = delta < 0 ? -1 : 1
var abs_delta = Math.abs(delta)
for (i=0; i < abs_delta; i++){
paddle_moves.push(increment)
}
for (i=0; i < (distance - abs_delta); i++){
paddle_moves.push(0)
}
return paddle_moves
}
defender.start(
function notify_player(rocks, paddle_y){
var paddle_moves = []
var closest_rock = find_closest_rock(rocks)
if (closest_rock.radians){
var delta_y = - parseInt(Math.tan(closest_rock.radians) * closest_rock.distance)
paddle_moves = align_paddle_and_wait(delta_y, closest_rock.distance)
}
return paddle_moves;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment