I’m kinda new to programming and need help doing a recursive method.I have a method that picks a random space in a 2d array and then I want to check if the space is free.If the space is free I want to use that space but if it isn’t I want to pick a new random space in the 2d array.Thanks
import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle
{
private char[][] puzzle ;
private ArrayList<String> puzzleWords ;
private int letterCount = 0 ;
private int gridDimensions;
public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
{
this.puzzleWords = userSpecifiedWords ;
}
private void createPuzzleGrid()
{
int i, itemLength;
String item;
for (i = 0; i < puzzleWords.size(); i++) {
item = puzzleWords.get(i);
itemLength = item.length();
letterCount = letterCount + itemLength;
}
gridDimensions = letterCount * 2;
puzzle = new char[gridDimensions][gridDimensions] ;
}
private void generateWordSearchPuzzle()
{
}
public void firstSpace(String Word)
{
int row, column;
row = (int)(Math.random() * gridDimensions +1);
column = (int)(Math.random() * gridDimensions +1);
if(puzzle[row][column] != ' '){
firstSpace();
}
}
I don’t think adding 1 in your index calculations is necessary and could perhaps cause an array out of bounds exception. Although, that depends on your definition of gridDimensions.
The problem you specified in the comments is because the Java compiler was trying to find a method named ‘void firstSpace()’, this is a different method that ‘void firstSpace(String word)’.