0

validCellに渡された「リスト」をvalidCellで変更し、変更されたリストを返すにはどうすればよいですか? validCell はパラメーターを取り、cellsForWord の for ループの r & c によって指定された開始点から、「単語」を綴るセルのパスが見つかるかどうかを確認します。私が持っているものが正しいとは思いません。

public class GoodWordOnBoardFinder implements IWordOnBoardFinder {

@Override
public List<BoardCell> cellsForWord(BoggleBoard board, String word) {
    // TODO Auto-generated method stub
    List<BoardCell> list = new ArrayList<BoardCell>();
    //Loop through each cell on board to find a starting point
       for(int r=0; r < board.size(); r++)
       {
           for(int c=0; c < board.size(); c++)
           {
              if(validCell(board, r, c, list, word, 0))
                  return list;
                   //***HOW to get populated list NOT Blank list???
           }
       }
    return null;
}
public boolean validCell(BoggleBoard theBoard, int row, int col, List<BoardCell> cList, String theWord, int letterIndex ){

    BoardCell cell = new BoardCell(row, col);

   String letter = theWord.substring(letterIndex, letterIndex+1);
    //Check the whole world has been found
   if(letterIndex >= theWord.length())
       return true;

   //Check if row or column is off the board
   if(row > theBoard.size() || col > theBoard.size())
       return false;
   //Check if cell has already been visited
   if(cList.contains(cell))
       return false;
   //Check if cell face isn't the letter we're looking for
   if(!theBoard.getFace(row, col).equals(letter))
   {
       //Make sure the letter isn't a Q bc Boggle is special
       if(!(theBoard.getFace(row, col).equals("Qu") && letter.equals("q")))
           return true;
   }
   cList.add(cell); 

  //Check all neighboring cells for letters of the word
  int[] rdelta = {-1,-1,-1, 0, 0, 1, 1, 1};
  int[] cdelta = {-1, 0, 1,-1, 1,-1, 0, 1};
  for(int k=0; k < rdelta.length; k++){
    if (validCell(theBoard, row+rdelta[k], col+cdelta[k], cList, theWord, letterIndex+1))
        return true;
   }
  cList.remove(cell);
return false;
}

}

4

3 に答える 3

0

リストをクラス メンバーに変換することを検討してください。次に、メソッドに渡す必要はありません。

public class GoodWordOnBoardFinder implements IWordOnBoardFinder {

   // The board
   List<BoardCell> board;

   // methods
   public boolean validCell(BoggleBoard theBoard, int row, int col, String theWord, int letterIndex ){
     // access/modify the board (= what you called cList)
   }
}
于 2012-08-09T05:25:30.467 に答える