Search code examples
javaminesweeper

return new int[]{randomHeight, randomWidth};


I want to code some basic stuff for a little minesweeper-game, that we do in university. Now I have the problem that my code .

public class Minesweeper1 {

    public static int[][]  makeRandomBoard(int s, int z, int n){
        //creating the field and fill with 0
        int feld[][] = new int [s][z];
        for(int i = 0; i < s; i++){
            for(int j = 0; j < z; j++){
                feld[i][j] = 0;
            }
        }

        //n-times to fill the field
        for( int i = 0; i < n; i++){
            selectRandomPosition(s, z);
            //want to get them from selectRandomPosition
            feld[randomHeight][randomWidth] = 1;
        }
    }
}

So it starts the selectRandomPosition code:

public static int[] selectRandomPosition(int maxWidth, int maxHeight) {
    int randomHeight = StdRandom.uniform(0, maxHeight);
    int randomWidth = StdRandom.uniform(0, maxWidth);
    return new int[]{randomHeight, randomWidth};
}

Here I'm not allowed to change anything, but it returns a new array. Now is my question how can I use the new array in my makeRandomBoard method, since I do not know any name of the array. When I use feld[randomHeight][randomWidth] = 1;, it says that it doesn't know these variables.


Solution

  • how I can use the new array in my makeRandomBoard method, since I do not know any name of the array?

    Call the method, and assign its return value to a variable. Now you have a name for the array:

    // Make a call
    int[] randomArray = selectRandomPosition(maxW, maxH);
    // Access the width
    int randomW = randomArray[0];
    // Access the height
    int randomH = randomArray[1];