Skip to content

Instantly share code, notes, and snippets.

@wanradt
Created September 13, 2020 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wanradt/5dd7140003b161bfd19dde8ba3fc66a7 to your computer and use it in GitHub Desktop.
Save wanradt/5dd7140003b161bfd19dde8ba3fc66a7 to your computer and use it in GitHub Desktop.
Shadows of the Knight - Episode 1
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
var inputs = readline().split(' ');
const W = parseInt(inputs[0]); // width of the building.
const H = parseInt(inputs[1]); // height of the building.
const N = parseInt(readline()); // maximum number of turns before game over.
var inputs = readline().split(' ');
const X0 = parseInt(inputs[0]);
const Y0 = parseInt(inputs[1]);
let rect = [ [0,0], [W-1,H-1] ];
let current_position = [ X0, Y0 ];
const jump = ( direction ) => {
let choosen_direction = _set_direction( direction );
_set_area( current_position, choosen_direction );
let width = rect[1][0] - rect[0][0];
let height = rect[1][1] - rect[0][1];
let actualJump = [
(parseInt( width/2 ) || 1 ),
(parseInt( height/2 ) || 1 )
];
current_position[0] += actualJump[0] * choosen_direction[0];
current_position[1] += actualJump[1] * choosen_direction[1];
return current_position;
}
const _set_direction = ( direction ) => {
const dir_to_coord = {
"U": [ 0,-1],
"UL": [-1,-1],
"L": [-1, 0],
"DL": [-1, 1],
"D": [ 0, 1],
"DR": [ 1, 1],
"R": [ 1, 0],
"UR": [ 1,-1],
};
return dir_to_coord[ direction ];
}
const _set_area = ( position,direction ) => {
let min_x, min_y, max_x, max_y;
if ( direction[0] == 1 ) {
min_x = position[0];
max_x = rect[1][0];
} else if ( direction[0] == -1 ) {
max_x = position[0];
min_x = rect[0][0];
} else {
max_x = position[0];
min_x = position[0];
}
if ( direction[1] == 1 ) {
min_y = position[1];
max_y = rect[1][1];
} else if ( direction[1] == -1 ) {
max_y = position[1];
min_y = rect[0][1];
} else {
max_y = position[1];
min_y = position[1];
}
rect = [ [ min_x, min_y ], [ max_x, max_y ] ];
}
// game loop
while (true) {
const bombDir = readline(); // the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)
let window = jump( bombDir );
console.log(`${window[0]} ${window[1]}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment