Hey, friends!I am new to coding and JS.Could someone explain why this code doesn’t work for easy and optimal angels?This is my second time solving this task.For the first time I solved it correctly with if,else if, else.Then I decided to find an other way but it didn’t work.
var inputs = readline().split(’ ');
const LX = parseInt(inputs[0]); // the X position of the light of power
const LY = parseInt(inputs[1]); // the Y position of the light of power
const TX = parseInt(inputs[2]); // Thor’s starting X position
const TY = parseInt(inputs[3]); // Thor’s starting Y position
let tx=TX, ty=TY;
while (true) {
const remainingTurns = parseInt(readline());
let dirX=‘’, dirY=‘’;
You would find using console.error useful in helping you to debug, e.g. adding this line after let dirX='', dirY='';: console.error(tx + ' ' + ty);
It prints out the values of the variables so that you know where they are different from your expected values.
By the way, please format your code properly on this forum by using the </> button in the formatting toolbar.
while (true) {
const remainingTurns = parseInt(readline()); // The level of Thor’s remaining energy, representing the number of moves he can still make.
const routeT = ‘’;
if ( LX < TX && LY > TY) {
routeT = 'SW';
} else if ( LX > TX && LY < TY ) {
routeT = 'NE';
} else if (LY < TY && LX < TX) {
routeT = 'NO';
} else if ( LY < TY && LX > TX ) {
routeT = 'SE';
} else if ( LX == TX && LY > TY ) {
routeT = 'S';
}else if ( LX == TX && LY > TY ) {
routeT = 'N';
} else if ( LY == TY && LX > TX ) {
routeT = 'W';
} else if ( LY == TY && LX > TX ) {
routeT = 'O';
}
const remainingTurns = parseInt(readline()); // The level of Thor’s remaining energy, representing the number of moves he can still make.
let routeT = ‘’;
let positionTX = TX;
let positionTY = TY;
if ( LX < TX && LY > TY) {
routeT = 'SW';
positionTX--;
positionTY++;
} else if ( LX > TX && LY < TY ) {
routeT = 'NE';
positionTX++;
positionTY--;
} else if (LY < TY && LX < TX) {
routeT = 'NW';
positionTX--;
positionTY--;
} else if ( LY > TY && LX > TX ) {
routeT = 'SE';
positionTX++;
positionTY++;
} else if ( LY > TY ) {
routeT = 'S';
positionTY++;
}else if ( LY < TY ) {
routeT = 'N';
positionTY--;
} else if ( LX > TX ) {
routeT = 'E';
positionTX++;
} else if ( LX < TX ) {
routeT = 'W';
positionTX--;
}
while (true) {
const remainingTurns = parseInt(readline()); // The level of Thor's remaining energy, representing the number of moves he can still make.
let routeT = '';
let positionTX = TX;
let positionTY = TY;
if ( LX < TX && LY > TY) {
routeT = 'SW';
positionTX--;
positionTY++;
} else if ( LX > TX && LY < TY ) {
routeT = 'NE';
positionTX++;
positionTY--;
} else if (LY < TY && LX < TX) {
routeT = 'NW';
positionTX--;
positionTY--;
} else if ( LY > TY && LX > TX ) {
routeT = 'SE';
positionTX++;
positionTY++;
} else if ( LY > TY ) {
routeT = 'S';
positionTY++;
}else if ( LY < TY ) {
routeT = 'N';
positionTY--;
} else if ( LX > TX ) {
routeT = 'E';
positionTX++;
} else if ( LX < TX ) {
routeT = 'W';
positionTX--;
}
// Write an action using console.log()
// To debug: console.error('Debug messages...');
// A single line providing the move to be made: N NE E SE S SW W or NW
console.log(routeT);
}
You are never updating the position of Thor, since all your checks are using TX & TY and since they are never updated it’s like Thor is always at the starting position.
Just make sure TX & TY are defined as “let” and not “const” and simply replace all the positionTX by TX and positionTY by TY, you should also drop the lines creating positionTX & positionTY as you don’t need these variables.
The 3rd and 4th tests cannot be done
Test 1 starts Thor 26 places away and gives you 26 steps to succeed
Test 2 starts Thor 13 places away and gives you 13 steps to succeed
Test 3 starts Thor 31 places away but only gives you 14 steps
Test 4 starts Thor 36 places away but only gives you 18 steps
Or have I missed something?
i made this code but thor only moves up and down why
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
# ---
# Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
# light_x: the X position of the light of power
# light_y: the Y position of the light of power
# initial_tx: Thor's starting X position
# initial_ty: Thor's starting Y position
light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]
# game loop
while True:
remaining_turns = int(input()) # The remaining amount of turns Thor can move. Do not remove this line.
dif_y = light_y - initial_ty #Y Anteil des Vektors berechnen
dif_x = light_x - initial_tx # same für X
#straight = math.sqrt(dif_y*dif_y+dif_x*dif_x) # berechnen der länge des Vektors
#schräge bewegungen
if dif_y < 0 & dif_x < 0:
print ("NW")
if dif_y > 0 & dif_x < 0:
print ("SW")
if dif_y > 0 & dif_x > 0:
print ("SE")
if dif_y < 0 & dif_x > 0:
print ("NE")
#gerade Bewegungen
if dif_y == 0 & dif_x > 0:
print ("E")
if dif_y == 0 & dif_x < 0:
print ("W")
if dif_y < 0 & dif_x == 0:
print ("N")
if dif_y > 0 & dif_x == 0:
print ("S")
People, help me find an error writing in C ++.
I ran all the tests successfully, but as a result I still get an error at Easy angle … here is my code … I already broke all my eyes while trying to find an error
#include #include #include #include
using namespace std;
/**
Auto-generated code below aims at helping you parse
the standard input according to the problem statement.
Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
**/
string direction_x, direction_y;
int tx, ty, distantion_x, distantion_y;
int main()
{
int light_x=32; // the X position of the light of power
int light_y=17; // the Y position of the light of power
int initial_tx=0; // Thor’s starting X position
int initial_ty=0; // Thor’s starting Y position
cin >> light_x >> light_y >> initial_tx >> initial_ty; cin.ignore();
tx=initial_tx;
ty=initial_ty;
// game loop
while (1) {
int remaining_turns; // The remaining amount of turns Thor can move. Do not remove this line.
cin >> remaining_turns; cin.ignore();
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
distantion_x=light_x-tx;
distantion_y=light_y-ty;
if(tx>=0)
{
if (distantion_x<0){
direction_x="W";
tx--;
}
}
else
direction_x="";
if(tx<=39)
{
if (distantion_x>0){
direction_x="E";
tx++;
}
}
else
direction_x="";
if(ty>=0)
{
if (distantion_y<0){
direction_y="N";
ty--;
}
}
else
direction_y="";
if(ty<17)
{
if (distantion_y>0){
direction_y="S";
ty++;
}
}
else
direction_y="";
cerr<<endl<<"distantion x: "<<distantion_x<<" distantion y: "<<distantion_y;
cerr<<endl<<"tx->"<<tx<<" "<<direction_x<<" ,ty->"<<ty<<" "<<direction_y;
// A single line providing the move to be made: N NE E SE S SW W or NW
cout <<direction_y<< direction_x<< endl;
Please make use of the </> button on the formatting toolbar to format your code properly, if you don’t want others to break their eyes too I tried to read your code but there was not proper indentation and also some bits of it were broken, so I gave up.
You may watch the replay to have an idea of where things may go wrong. Go to MY REPORT, then click DETAILS (see the screenshot below):