user1323808
user1323808

Reputation: 31

need assistance with recursive method in Java

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();
            }
        }

Upvotes: 1

Views: 339

Answers (2)

Simon Campbell
Simon Campbell

Reputation: 752

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)'.

public void firstSpace(String word) 
{  
    int row, column;

    // No need to add 1, Java arrays are accessed with the first index 
    // being 0. Math.random() returns from 0 up to but not including 1.0.
    // e.g. array size = 50, min index = 0, max index = 49
    // Lets say you get very close to 1 e.g. 0.9999, then 
    // 0.9999 * 50 = 49.995 (after integer truncating you have 49)
    row = (int)(Math.random() * gridDimensions);
    column = (int)(Math.random() * gridDimensions);

    if(puzzle[row][column] != ' ') { 
        // If this element is not "empty" then run the method again 
        // using recursion. null might be a better choice to compare
        // to depending on how you initialized the array.
        firstSpace(word);
    } else {
        // Otherwise we're finished and we can set the array element
        // to the new word.

        // (Assumed post condition (you might want to do something else once you
        // find a blank index))
        puzzle[row][column] = word;
    }
}

Upvotes: 0

Jim
Jim

Reputation: 22656

The specific issue you mentioned in the comments is because the firstSpace method needs to have a string as a parameter. You should use:

firstSpace(word);

Also be aware that this method doesn't currently return anything so you have no way of knowing which space it chose.

Upvotes: 2

Related Questions