2012-04-18 43 views
1

我有點新來編程,需要幫助做一個遞歸方法。我有一個方法,在二維數組中選擇一個隨機空間,然後我想檢查空間是否空閒。如果空間是自由,我想使用的空間,但如果不是我想選擇一個新的隨機空間在2D array.Thanks在Java中需要遞歸方法的幫助

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

你有什麼問題? – Jim 2012-04-18 10:51:23

+0

這在遞歸性是一個好的或甚至接近體面的解決方案中不會有問題。只需做一個while-loop選擇一個隨機空間並檢查它是否空閒。 – Mads 2012-04-18 10:51:43

+0

什麼是問題,你的退出條件是什麼?你有沒有界定你的界限? – Phani 2012-04-18 10:52:44

回答

0

我不認爲在您的索引計算中加1是必要的,也可能會導致數組越界異常。儘管這取決於您對gridDimensions的定義。

您在註釋中指定的問題是因爲Java編譯器試圖找到名爲'void firstSpace()'的方法,這是'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; 
    } 
} 
+0

感謝Simon幫助我編輯代碼以顯示目前爲止的完整代碼 – user1323808 2012-04-18 11:12:16

+0

好的,無需將+1添加到您的行和列計算中。我編輯了代碼,並解釋了爲什麼在評論中,希望這是可以理解的。 您發佈的新代碼與我的預期有很大不同。我認爲這是一個'n x n'字符串數組,你有一個'n x n'字符數組,它本質上是一個'n x 1'字符串數組。 – 2012-04-18 11:20:26

+0

非常感謝上百萬幫助,只有最後一個問題,當遞歸部分中的空間不清晰時,系統會提示我輸入一個新單詞,並且我不確定爲什麼您將這部分代碼放在其他部分拼圖中[row] [column] = word;因爲它們是不兼容的類型 – user1323808 2012-04-18 11:26:48

2

您在評論中提到的具體問題,是因爲firstSpace方法需要有一個字符串作爲參數。您應該使用:

firstSpace(word); 

另外要注意,這種方法目前並不返回任何東西,所以你無法知道它選擇哪個空間的方式。

+0

感謝吉姆我只是有兩個問題1)會提示我每次進入遞歸函數時都會輸入一個新單詞,並且2)我可以做一個} else {return row && column; ?? – user1323808 2012-04-18 11:06:52

+0

@ user1323808不,它只會使用傳遞給第一個遞歸函數調用的相同單詞。要返回兩個項目,你必須創建一個包含它們的對象,但是你是對的,你可以在else中返回它們(你也可以在first的第一部分使用return firstSpace(word);)。 – Jim 2012-04-18 11:23:38