Boggle のようなゲームのプログラムを作成する必要があり、現在、現在の文字の下にある各文字をチェックして、単語を作成するかどうかを確認しています。したがって、次のようなボードの場合:
W O Y R
F U M F
H T R V
I G S W
検出される唯一の単語は、上から下に向かう "OUT" です。単語の一部が見つかると、その文字を文字列に入れ、それを null に設定して、同じ単語で文字を 2 回使用しないようにします (完全なアルゴリズムでは、複数の方向で検索できる必要があります)。スタックを使用して、使用した文字の座標を追跡し、バックトラックできるようにします。スタックをポップするたびに、文字列の最後の文字を取得して、元の位置にボードに戻します。ただし、問題は、複数の文字が削除されると、それらがすべて同じインデックスに配置され、前の文字が上書きされることです。したがって、「OUT」の場合、3 文字を置き換えた後、ボードは次のようになります。
W null Y R
F null M F
H O R V
I G S W
コードを調べて、2回書き直そうとしましたが、常にこれを行います。なぜこれが起こっているのかについての洞察はありますか?
private void checkNeighbors(LetterCoor center){
String check = out;
while (!path.empty()){
if(center.getDirec()==0){//If the direction to check is down
System.out.println("Bottom");
if((center.getRow())+1<sideLength && board[(center.getRow())+1][center.getCol()]!=null){//makes sure the space below is !null and !out of bounds
check+=board[center.getRow()+1][center.getCol()];
System.out.println("Checking " + check);
if(isValidWord(check)){//checks if string is part of the lexicon
center.nextNeighbor();
board[center.getRow()+1][center.getCol()]=null;
center = new LetterCoor(center.getRow()+1, center.getCol(), 0);
System.out.println("push " + check.substring(check.length()-1));
path.push(center);
out=check;
}
else{
center=(LetterCoor) path.pop();
center.nextNeighbor();
path.push(center);
}
}//end of null if
else{
System.out.println("Null or end of board");
center=(LetterCoor) path.pop();
center.nextNeighbor();
path.push(center);
}
}//end of direc 0 if
else{
System.out.println("pop " + out.substring(out.length()-1,out.length()));
center=(LetterCoor) path.pop();
center.nextNeighbor();
board[center.getRow()][center.getCol()]=out.substring(out.length()-1,out.length());
out=out.substring(0,out.length()-1);
if (center.getDirec()<1){
path.push(center);
}
}
System.out.println("Current string is " + out);
}//end of while loop
}
私のコードを明確にする必要がある場合は、お知らせください。
また、明確にするために、LetterCoor オブジェクトは 3 つの int を格納します。1 つ目は文字の行インデックス、2 つ目は列インデックス、3 つ目は検索方向を示します (0 = 下、1 = 右下、2 = 右など)。