Power Of Thor - Episode 1 - Puzzle discussion

my code for “power of thor” works for the first 3 level, but i do not know how change my code for it to work on the fourth level “optimal angle”.
here is my code…

[EDIT: NO FULL CODE]

Is this what an individual if statement should look like?
if (thorX >= lightX){
directionX= “W”;
–thorX;
}
I’m updating Thor’s position, but the standard output doesn’t change.

I’m a bit stuck on this puzzle. I’m fairly certain I’ve narrowed the problem down to my output what I have is:

if(thorY = lightY && thorX != lightX)
        {
            cout<< directionX<< endl;
        }else if(thorX = lightX && thorY != lightY)
        {
            cout<< directionY<< endl;
        }else
        {
            cout<< directionY<< directionX<< endl;
        }
(spaces added so code would display)
Now this code works for the first test but fails for all the rest. It causes Thor to walk straight in a single direction. Now I’m not looking for the solution, but what am I doing wrong? The way I read the code is: If Thor’s location matches the light location but the other variable doesn’t Thor should move towards the variable that doesn’t match. Else the loop should be ignored and the program should move to the next loop. What I’m finding the code is doing is ignoring the and statement so if thorX and lightX are the same he shouldn’t move along the X but he is. Am I wording the actions wrong?

Also I noticed if I switch between thorX = lightX to thorX == lightX the movement pattern is wholly different. Now I’ve been told to use “==” for equality checks and “=” for assignments. Why does “=” move thor one way and “==” another? Lastly, I noticed some people have used (directionY+directionX) for the output what is the difference from my variation directionY<< directionX?

If you use an assignment (one equal sign) in place of an expression, the assignment will be executed and the expression will always be true.

Here is what your code really does:

thorY = lightY;
if(true && thorX != lightX) {
  cout<< directionX<< endl;
} else {
  thorX = lightX;
  if(true && thorY != lightY) {
    cout<< directionY<< endl;
  } else {
    cout<< directionY<< directionX<< endl;
  }
}

Can you see that the line cout<< directionY<< endl;will never be executed?

Much appreciation that actually explains a lot.

1 Like

Now there’s just one more thing I don’t understand. Why is Thor automatically moving SE? The game starts Thor at (5, 4) Light (31, 4). I have two lines(one for x one for y) that state if thor == light that direction is assigned “”. I’m not updating the positions till after a direction is assigned. So shouldn’t the program assign directionY with a null character making Thor move East for the first turn? I should note at y = 8 the programs starts producing a single direction even though thor and light do not equal.

I can get him to move the direction I want using if else statements but with the diagonal ones I can’t get him to go where I want. Anyone think they could help?

I am new here, and new programming with python too.

Can someone tell me the pseudo code, the algorithm to solve the first exercise?

BIG FAT NOPE.

Read this topic, if you’re talking about onboarding, read the onboarding topic, search for tutorials in your language, experiment, fail, experiment again, repeat until success. You won’t be able to do anything by asking the solution.

The algorithm is already written step by step in the statement :wink:

I just can’t seem to figure this out. It looks right to me, but I’m clearly not seeing something. My code is broken down as such:

NO FULL CODE PLEASE

What’s happening is Thor will go SE for a few turns then go E till he dies.
I’ve done the output as just the concatenation of direction y and x. I’ve changed the location of the increment/decrement. I’ve changed the the increments/decrements for thor ie: E = thorX-- thinking maybe I had the positions changing incorrectly. I’ve read through this forum but I just can’t pinpoint what I’m doing wrong. The funny thing is test 4 actually clears. Almost as if it’s hardcoded to work just for four but I don’t know how that would be.

Your problem looked weird to me, so I went looking at your code. You are assigning thorX and thorY to initialTx and initialTy before putting a value in initialTx and initialTy.

I do not know what happens in c++ when you compare something to an undefined value or increment a value, but seeing the behavior, I’ll suppose it consider the variable to be like 0. That would explain why he always go south east.

By default in C and C++, variables have junk data in them, but depending on implementation (including CG), variables are initialized with 0 (or NULL if pointer). So all in all, you are right @yohannjardin, and he shouldn’t try to use a variable before it’s unitialized in any language.

In Python, you can’t create a variable without initializing it.

I can’t believe I over looked that; thank you very much for the information.

I am having a problem in Python 3. I create a variable thor x and y to keep up with thor’s position -which are changed every time a direction is printed-, but they don’t seem to update, because thor keeps going on the same direction on the third puzzle even when he reached the same height of the light of power :S

You have to update yourself the variable.

Just solved it: I put the thor variable inside the while loop, so every time the loop ran it reset itself. I cut and paste them on the first lines of code and it worked like a charm :smile:

C:I think that i have the correct algorithm, i can’t figure out what’s this error:
“Timeout: your program did not provide an input in due time. Earth was destroyed!”;

System.out.println(“E”);