Power Of Thor - Episode 1 - Puzzle discussion

i’m using C language

Maybe your string has quotes in it.

the code is that:

EDIT: NO FULL CODE
You may share one part of your code, but not the entirety of its logic -> let others think about how to solve this puzzle :slight_smile:

directionX and directionY may be uninitialized (when initialTX==lightX or initialTY==lightY), leading to random characters being printed.

how to do it??

@DanFletcher i have your same problem, what do u did to solve it?
printf("%c%c\n",directionY, directionX);

if your directionY is zero, than your string gets zero-terminated at first char and directionX is never printed
i assume zero-termination as it is printf from C

Wrong: directionX is printed, and a NUL char is printed for directionY, which is not visible but still rejected by the validator.

well then, i’ve posted working variant of C code upper in topic - please find and refer

same problem… after few turns Thor is always leaving the map… he keeps going in the direction from previous turns

try using

directionX = "";
1 Like

I need some help with the code, But I just can’t seem to be able to get Thor to change direction, a direction is chosen, but then he doesn’t change it when he should. Has anyone had this problem? If so, could you please give me a hint as to how to solve it?

1 Like

@GordonDrake @emeeu @wajdi_feki88 Your program only gets the initial position of Thor and the light. Everytime Thor moves you have to update his position. Otherwise your program will choose the same direction every turn (the direction that was right for the first turn) and never change direction.

1 Like

I think thorX and thorY are suppused to be defined before the loop. The strings in the loop. Correct me if I’m wrong.

Is the “string” type allowed in C?

I have inicialised the char directionX/Y=0;

I also update the thorX/Y++

of course in the IFs, like:

if ( thorX < lightx ) { directionX=‘E’; thorX++;}

in the end I printf("%c%c\n",directionY,directionX);

I read through the whole forum and I could not find the answer. Your code may be right but I don’t want just to copy it and don’t understand.

Help please.

When Thor only has to move in one direction the expected output is one character and a newline.

If you use printf("%c%c\n",directionY,directionX); you are outputting two characters and a newline.

you picked my reply to c++ solution of puzzle, if you need pure c look at this Power Of Thor - Episode 1 - Puzzle discussion

how to do the easy angle test? i don’t know what i have to do

I’m not sure that strcat() is the easiest function to get right for a beginnner in C. In fact, this example code is not correct since char s[1] is too short. Its length should be at least 2 to hold a single char string like "N".

IMHO, for the Thor problem, the simplest solution is to output the direction a soon as it is known, and to finalize with a newline:

if (/* want to go to the North*/) {
    printf("N");
} else if (/* want to go to the South*/)
    /* ... */
}
if (/* want to go to the West */) {
    printf("W");
    /* ... */
}
printf("\n");

If you really want to have a single call to printf, using strings (AKA pointers to char) instead of chars for directionX and directionY is less error prone:

const char *directionX = "";
const char *directionY = "";
if (/* want to go to the North */) {
    directionY = "N";
} else if (/* want to go to the South */) {
    /* ... */
}
if (/* want to go to the West */) {
    directionX = "W";
    /* ... */
}
printf("%s%s\n", directionY, directionX);

Finally, note that the examples given above need to be completed to handle all directions, to update Thor’s position, and so on.

2 Likes

Thank you very much, I was stagnating on a syntax problem. And I did not try to use pointers. Also I have a mess in apostrophes. Never know which ones to use. I’m just beginning with C so there’s so much I have to learn.