The Descent - Puzzle discussion

Game keeps telling me that int max and int imax aren’t initialised but I don’t know why… Any ideas?

class Player {

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);

    while (true) {
        int max;
        int iPosition;
        for (int i = 0; i < 8; i++) {
            int mountainH = in.nextInt(); // represents the height of one mountain.
            if(max < mountainH){
                max = mountainH;
                iPosition = i;
            }
        }

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

Try assigning values to them when you initialise them, e.g. int max = -1.

after printf reset the target_h = 0 , it should work

Hello everyone, I didn’t understand but, this ship is shooting ‘mountain 7’ two times. The first shot destroys the mountain, so why does it shoot again?

while (true) {
    let arr = [];
    for (let i = 0; i < 8; i++) {
        const mountainH = parseInt(readline());
        arr.push(mountainH);
    }
    let max=0;
    for(let i=0; i<8; i++) {
        if(max<arr[i]){
            max=arr[i];
        }
    }
    for(let i=0; i<8; i++){
        if(max==arr[i]){
            console.log(String(i));
        }
    }
}

Because there may be mountains of the same height.

In the last part of your while loop, you print the index of the mountain to shoot whenever the height is equal to the maximum height, and there can be more than one of those.

Thank you for your answer.
There is one mountain at height of 7. The ship shoots it and it got destroyed. Then It supposed to choose another mountain. There is two mountains at height of 5. But the ship shoots that mountain again. Its height is zero. Why? I don’t get it.

The ship shoots according to the output of your code. If your code instructs it to shoot twice in the same turn, or to shoot the same mountain in the following turn, the ship does so accordingly. To correct the ship’s behaviour, you have to correct your code.

Please help me !
Code
int main()
{
while (1) {
for (int i = 0; i < 8; i++) {
int mountain_h;
mountain_h = scanf("%d", &mountain_h);
printf("%d", mountain_h);
}
}
return 0;
}

Output → 11111111

Is the scanf just not working or something ?

Use
scanf("%d", &mountain_h);
instead of
mountain_h = scanf("%d", &mountain_h);

Also, for debugging purpose, please print to the error stream by using
fprintf(stderr, "%d\n", mountain_h);
instead of
printf("%d", mountain_h);

By the way, you can format your code on this forum by using the </> button on the toolbar.

1 Like

Hey y’all

Just wanted to give someone, like me, a heads up about the solution for this one in javascript.
Use let instead of var - must be something with the JS versions being old/new :slight_smile:

你最大的问题就是你前文的循环,和后面的打印毫无联系,若你删除掉 “print(“0”)”,以前的所有代码可能也可以过第一关。是因为你这份代码就是为第一关服务的,以下代码只能助你过一、二关,之后的几关我实在是不知道怎样去存储山被击中后下落多少,也不知道源代码中有无当打印出下标后山的数值变化,所以我也只能过到第2关。

[GT→ Your biggest problem is that your previous loop has nothing to do with the subsequent printing. If you delete “print(“0”)”, all the previous codes may also be able to pass the first level. It’s because your code is for the first level. The following code can only help you pass the first or second level. I really don’t know how to store the number of the mountains after being hit, and I don’t know the source. Is there any change in the value of the mountain after printing the subscript in the code, so I can only pass the second level.]

a = []
i = 0
while True:
    while i<8:    
        mountain_h = int(input())  # represents the height of one mountain.
        a.append(mountain_h)
        i += 1
    # 01 02
    for x in range(8):
        print(a.index(max(a)))
        a[a.index(max(a))] = 0
  • Please use English in this forum.
  • Please use the </> button on the formatting toolbar to format your code.
  • This is a reminder only: please be reminded not to post any fully working code here.
    Thanks.

就是每一次击中山峰后,再次循环,这时机器打出的8个input都会更新,更新的数值与图片上的数值实时对应。明白这一点这个题就不难解了

[GT→ That is, after hitting the mountain every time, the cycle is repeated again. At this time, the 8 inputs printed by the machine will be updated, and the updated value corresponds to the value on the picture in real time. Understand this, the problem is not difficult to solve]

In English, please!

Hi ! Please someone could explain me why the code below does not work ? I can’t understand why.
The first lap, it destroyed the good montain. Next, during the second lap, a missile destroyed me ^^

while (true) {
    let max = 0;
    let indexMax = 0;
    for (let i = 0; i < 8; i++) {
        const mountainH = parseInt(readline()); // represents the height of one mountain.
        if (mountainH > max ) {
            max = mountainH;
            indexMax = i;
        }
    }

    console.log(indexMax); 
    break;    // The index of the mountain to fire on.
    
}

Because you break (out of the while true loop) and cannot reach the next round.

thank’s i’m so stupid ^^

Hello there, just finished up the Defibrillators puzzle, and looking to continue with the Descent, but the Tab and Ctrl keys seems to be ineffective now… like, they’re still working on my computer, but they don’t have any effect on the computer integrated IDE…
Anyone experienced that issue before ?

EDIT: Seems like I was in EMACS configuration instead of Classic in the parameters, no idea how it got like that, but anyway, problem seems to be solved

import sys
import math

# The while loop represents the game.
# Each iteration represents a turn of the game
# where you are given inputs (the heights of the mountains)
# and where you have to print an output (the index of the mountain to fire on)
# The inputs you are given are automatically updated according to your last actions.

mts = []
# game loop
while True:
    for i in range(8):
        mountain_h = int(input())  # represents the height of one mountain.
        mts.append(mountain_h)
        mts.sort()
        print(len(mts) - 1)

    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr, flush=True)

    # The index of the mountain to fire on.

I don’t understand why this would not work. The largest mountain is immediately sorted to the end and printed.

You are asked to output the index of the highest mountain. If you sort the mountains, the original order is lost so the index becomes wrong.