Ich versuche herauszufinden, wie ein Char-Array für ein Wort rekursiv suchen und zurückgeben, wenn es oder nicht vorhanden ist. Stellen Sie es sich vor wie das Programmieräquivalent einer Wortsuche. Mein aktueller Code ist unten. Der Startwert von 9999 ist hilfreich beim Testen. Wie schreibe ich eine rekursive Suchmethode, um die Anwesenheit eines gegebenen Wortes in einem char-Array zu überprüfen?Rekursive Array-Suche von Char-Array
public class Board {
private char[][] board = new char[4][4];
private boolean[][] visited = new boolean[4][4];
private String word;
public Board(int seed){
word = "";
Random rand = new Random(seed);
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
char randomChar = (char) (rand.nextInt(27) + 65);
//System.out.print(" " + randomChar + " ");
board[i][j] = randomChar;
//System.out.print(board[i][j]);
}//System.out.println();
}
}
public void resetBoard(){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
visited[i][j] = false;
}
}
}
public void printBoard(){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(j == 0)
System.out.println("+---+ +---+ +---+ +---+");
System.out.print("| " + board[i][j] + " | ");
}
System.out.println("\n+---+ +---+ +---+ +---+");
}
}
public boolean verifyWord(String w){
this.word = w;
for(int i = 0; i < w.length(); i++){
// char letter = w.charAt(i);
// System.out.println(letter);
boolean wordVerify = verifyWordRecursively(0, 0, 0);
if(wordVerify == true)
return true;
// if(i == w.length() - 1){
// if(wordVerify == true)
// return true;
// }
}return false;
}
public boolean verifyWordRecursively(int wordIndex, int row, int col){
char letter = word.charAt(wordIndex);
System.out.println(letter);
if(board[row][col] == letter){
return true;
}
else{
if(col + 1 < board[0].length){
verifyWordRecursively(wordIndex, row, col + 1);
}
if(row + 1 < board.length){
verifyWordRecursively(wordIndex, row + 1, col);
}
}return false;
}
}
Hier ist meine Hauptklasse:
public class LA2Main {
public static void main(String[] args) throws IOException{
int seed = getSeed();
Board b = new Board(seed);
b.printBoard();
Scanner inFile = new Scanner(new FileReader("input.txt"));
// while(inFile.hasNextLine()){
// System.out.println(inFile.nextLine());
String word = inFile.nextLine();
b.resetBoard();
System.out.println("-----------------------\n" + word);
boolean isVerified = b.verifyWord(word);
if(isVerified == true)
System.out.println("'" + word + "' was found on the board!");
else
System.out.println("'" + word + "' is NOT on this board");
b.printBoard();
// }
}
public static int getSeed(){
Scanner sc = new Scanner(System.in);
int userInput;
while(true){
try{
System.out.println("Enter an integer seed value greater than 0: ");
userInput = Integer.parseInt(sc.next());
if(userInput > 0)
return userInput;
}
catch(NumberFormatException e){
System.out.println("Invalid!");
}
}
}
}
Ich würde dies wahrscheinlich mit Iteration nicht Rekursion tun. Ich sage das, weil es viele Fälle von Ecken gibt und die Menge an Informationen, die Sie an Ihre rekursive Funktion übergeben müssten, würde unhandlich werden. I.e. Zuerst möchtest du nur überprüfen, ob der Buchstabe bei dir der erste im Wort ist, dann nach Buchstaben suchen, die als nächstes kommen, und danach musst du in die gleiche Richtung gehen wie die ersten zwei Buchstaben. Es ist sicherlich möglich, aber Iteration scheint der Weg in meinem Buch zu sein. – kpie