FireCross FireCross - 17 days ago 6
Java Question

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);
feld[randomHeight][randomWidth] = 1; //want to get them from selectRandomPosition
}


}

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 am not allowed to change anything, but it returns a new array. Now is my question how I can 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 does not know these variables.

Answer

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];