The Descent - Puzzle discussion

Your code works as expected.

Just don’t destroy the provided default code and the “bug in the game” will be fix. :slightly_smiling:

   While true
       READ MAP
       FIND INDEX
       PRINT INDEX
3 Likes

its not logical to use a variable inside the loop it will update each time then even though i pop the former greatest because the loop sets it from the very beginning i will end up bombing the same mountain always theres a huge problem with logic i this algorithm

this game makes no sense seriously simple programming logic is not working

6 Likes

When you fire a mountain, it is not entirely destroyed, its height has only been reduced. That’s why the list of heights is provided at each turn.

Your algorithm can’t work if you don’t read the new height. Plus, you are shifting all the indexes, but when a mountain is destroyed, the mountains on the right are not magically shifted on the left…

1 Like

i just noticed that game supplies inside the game loop for me the new data each time so i dont needf to wory about entire logic but i didnt know that

/*int max = 0; <<<  I can't understand why when I'm trying to use this variable 
outside the "while" loop it doesn't work!? */
   int imax = 0; // <<< With this one here is WORKING fine.
    // game loop
    while (true) {
        
        int max = 0;
        
        for (int i = 0; i < 8; i++) {
            int mountainH = in.nextInt(); // represents the height of one mountain, from 9 to 0.
            if(mountainH > max){
                 max = mountainH;
                 imax = i;
                 
            }
        }

        // Write an action using System.out.println()
        // To debug: System.err.println("Debug messages...");

        System.out.println(imax); // The number of the mountain to fire on.
    }

I’m interesting to learn Java and new to programming language, but when a small things like that which I don’t understand. It eat me from inside :rage: with the question “Why, why… what is the reason!”
My question is inside the code like a comment.
:seedling: Thank you! :seedling:

because it has to be inside the cycle for the algorithm to work correctly, duh

Thank you for the answer! But why the other variable is working fine when is not in the cycle? :confused:

Not sure if I understand your problem, but it’s not a problem if you write
int max = 0;
outside the loop. I guess you just forgot to set max to 0 in every loop iteration when you moved the variable declaration outside the loop.
Just do max = 0; in every loop iteration and you should be fine:

int imax = 0;
int max = 0;
// game loop
while (true) {        
    max = 0;  // <- if you don't do this, you won't find the right maximum because max will still be the value from last round
    for (int i = 0; i < 8; i++) {
        int mountainH = in.nextInt(); // represents the height of one mountain, from 9 to 0.
        if(mountainH > max){
             max = mountainH;
             imax = i;                 
        }
    }
    /* OUTPUT*/
}
3 Likes

The variables are different because you compare mountain height to max, but imax you only update depending on max comparison. in order to get max height, you need to reinitialize max on every step.

1 Like

Hi there,

I’m sorry for the very stupid question… There’s something I don’t get here.

How can the imax vary in order to get the right identifier of the mountain to destroy ?

If the condition mountain_h > max isn’t true, what does imax correspond to ?

Thanks a lot,

Marion

1 Like

Max is resetted to 0 at the start of the while loop, so you will necessarily enter in the IF statement 1 times

Makes sense ! Thanks a lot for this.

The Swift version has different hints, the right one appears if you toggle the language.

I am not able to finish this, please help me…
I tried to print the mountain number but it fails for the second time…

could you be more specific? It’s quite difficult to help someone who says “help, I print a number, it fails” :wink:

Can someone tell me if this is a bug in the game or actually a bug with my solution?
Validator 5 - One Mountain

https://www.codingame.com/replay/solo/126518698

The code is nearly identical to the proposed solution.

EDIT:
I changed this line:

if (mountainH > heightestMountain) {
heighestIndex = i
heightestMountain = mountainH
}

to this:

if mountainH > heightestMountain {
heighestIndex = i
heightestMountain = mountainH
}

and now I got 100%. Pretty sure that there isn’t a syntax rule in Golang which forbids parantheses, so a bug in the system?

1 Like

Hi, I think there’s a problem in the game, My code sometimes does’t pass a test and I see that the ship is just standing in the beginning without moving, although if I repeat running the test it just simply passes!
And I think it’s very frustrating as a first challenge to not be working!
Thanks for the effort :slight_smile:

1 Like

Indeed we seem to have an issue here. Not specifically linked to this puzzle. Code in JS sometimes timeout :confused: We’ll look into it right away. Thank you for reporting it @hbahaa91

1 Like

Hey noob here. I am writing code in JavScript for this game. I want to use “console.out()”, for seeing the type of variable(such as of “var mountainH”) and other debugging purposes.

But not sure how to do it. Please help if anyone knows how to do it. Or suggest method you guys use for the same.