Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW0 #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

HW0 #23

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions batman.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/*global readline*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Оберните весь код в

(function () {
  /** code **/
}());

/*global print*/
function jump_coord(x1, x2, y1, y2) {
"use strict";
var x, y, res;
if (x1 === x2) {
y = (y1 + y2) / 2;
y = Math.floor(y);
x = x1;
} else if (y1 === y2) {
x = (x1 + x2) / 2;
x = Math.floor(x);
y = y1;
} else {
x = (x1 + x2) / 2;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

мне кажется, что разбор всех случаев лишний

x = Math.floor(x);
y = (y1 + y2) / 2;
y = Math.floor(y);
}
res = [x, y];
return res;
}

var i;
var inputs = readline().split(' ');
var W = parseInt(inputs[0])//, inputs[0]); // width of the building.
var H = parseInt(inputs[1])//, inputs[1]); // height of the building.
var N = parseInt(readline())//, readline()); // maximum number of turns before game over.
var inputs = readline().split(' ');
var X0 = parseInt(inputs[0])//, inputs[0]);
var Y0 = parseInt(inputs[1])//, inputs[1]);
var windows = new Array(W);
for (i = 0; i < W; i += 1) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

старайтесь избегать наименование переменных с большой буквы, для всех js разработчиков это функция-конструктор.

windows[i] = new Array(H);
}
var up = 0, down = H, left = 0, right = W, f = 1, coor;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

однобуквенные переменные зло!

coor = new Array(2);
// game loop
while (true) {
var BOMB_DIR = readline();
switch (BOMB_DIR) {
case "U":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P.S. Посмотрите решение ваших коллег, есть гораздо более короткие

if (f !== 1) {
if (down > Y0 - 1) {
down = Y0 - 1;
}
f += 1;
left = X0;
right = X0;
} else {
left = X0;
right = X0;
up = 0;
down = Y0 - 1;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
case "UR":
if (f !== 1) {
if (down > Y0 - 1) {
down = Y0 - 1;
}
if (left < X0 + 1) {
left = X0 + 1;
}
f += 1;
} else {
left = X0 - 1;
right = W;
up = 0;
down = Y0 - 1;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
case "R":
if (f !== 1) {
if (left < X0 + 1) {
left = X0 + 1;
}
f += 1;
up = Y0;
down = Y0;
} else {
left = X0 + 1;
right = W;
up = Y0;
down = Y0;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
case "DR":
if (f !== 1) {
if (up < Y0 + 1) {
up = Y0 + 1;
}
if (left < X0 + 1) {
left = X0 + 1;
}
f += 1;
} else {
left = X0 + 1;
right = W;
up = Y0 + 1;
down = H;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
case "D":
if (f !== 1) {
if (up < Y0 + 1) {
up = Y0 + 1;
}
left = X0;
right = X0;
f += 1;
} else {
left = X0;
right = X0;
up = Y0 + 1;
down = H;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
case "DL":
if (f !== 1) {
if (up < Y0 + 1) {
up = Y0 + 1;
}
if (right > X0 - 1) {
right = X0 - 1;
}
f += 1;
} else {
left = 0;
right = X0 - 1;
up = Y0 + 1;
down = H;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
case "L":
if (f !== 1) {
if (right > X0 - 1) {
right = X0 - 1;
}
up = Y0;
down = Y0;
f += 1;
} else {
left = 0;
right = X0 - 1;
up = Y0;
down = Y0;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
case "UL":
if (f !== 1) {
if (down > Y0 - 1) {
down = Y0 - 1;
}
if (right > X0 - 1) {
right = X0 - 1;
}
} else {
left = 0;
right = X0 - 1;
up = 0;
down = Y0 - 1;
f += 1;
}
coor = jump_coord(left, right, up, down);
X0 = coor[0];
Y0 = coor[1];
print(X0, Y0);
break;
default:
print('Err');
}
}