OK I’m trying to avoid just showing my code and saying ‘fix it’. So I’ll describe what I’m doing, which I think is probably headed in the wrong direction.
The first thing I do is create vars for directionX and directionY, to hold the E/W, N/S characters.
Then I set up if/else statements where;
if lightX is greater than initialTX, directionX = E, else it = W
or set it to = “” if no movement is needed on that axis.
same for lightY
then I output ‘directionY >> direction X’
yes, of course I am appending ‘>> endl’ to every output.
This works for the straight line cases (01 and 02), but with either of the angles it fails. I would expect it would stop moving N/S when it reaches the Y position of the light, but it just keeps going in the same direction.
To try and start debugging, I put in
“cerr >> “I’m about to give a direction to head in…” >> endl;”
just before the output… I’d expect it then to print that message exactly once for each time it gives a direction, but instead I get that message repeated dozens of times for each time a direction is given. So, I don’t understand why this loop is happening so many times for each turn, which means I don’t know how it’s being used or what’s expected.
Thanks for your help. Here’s a segment of the output so it’s clear what I’m talking about;
I’m about to give a direction to head in
I’m about to give a direction to head in
I’m about to give a direction to head…
Standard Output Stream:
E
2/27
Game information:
Thor’s moving…
Thor position = (6,4). Light position = (31,4). Energy = 99
Standard Error Stream:
I’m about to give a direction to head in
I’m about to give a direction to head in
I’m about to give a direction to head in
I’m about to give a direction to head in
I found the problem, I wasn’t using my own thorX, thorY, but reusing the initial ones. just being clumsy.
but the unexplained repeated loops, never figured that out. in fact it passes all the tests now, 100% score, but still running each loop dozens of times. somebody in the IRC chat window said they modified it to take out ‘energy’ but created a bug when doing so.
Hi, the thor goes only in only one direction it does not changes its direction, like if it going in SW direction then it does not go in only W direction. Why?
I’m running in to the same problem I’m seeing all over this thread. In test 3, Thor walks right off the cliff. My problem comes in step 15:
Thor’s moving…
Thor position = (18,17). Light position = (0,17). Energy = 31
Standard Output Stream:
SW
Game information:
Failure: Thor wandered off the path and died (invalid position).
Thor position = (17,18). Light position = (0,17). Energy = 31
However, I’m pretty sure in my code when thorX == lightX, it’s not supposed to continue going in that direction. I’m updating Thor’s position in each step, though.
I am having trouble completing the 3rd and 4th test runs. In the first two runs my code clearly proves that it can give only one direction without having to add the other. Then during the 3rd run it keeps going diagonally SW until it reaches the edge when the Y coordinates are both 17, at which point it again results in a SW instead of just W.
I am trying to explain the problem without posting my code. Please help me out if you manage to understand my problem
if (thorY < lightY) {
direction = "S";
thorY++;
} else if (thorY > lightY) {
direction = "N";
thorY--;
}
if (thorX > lightX) {
direction += "W";
thorX--;
} else if (thorX < lightX) {
direction += "E";
thorX++;
}
(Note for CG: we really need a facepalm emoji.)… I’m glad you agree on that point at least. Let’s be a tad clearer: what is the value of direction in this else block? Is it an empty string because you take care of that in a part of you code you didn’t show us, or is it just the value of the previous step, like "SW" per instance?
I declare the empty string before the if statements so that should be alright. But I also tried clearing it after the print and I still get the same result.