Hi ich habe ein paar Schwierigkeiten beim Schreiben eines Programms.
Ich versuche die Aufgabenstellung knapp zu formulieren und zwar wollte ich ein Programm schreiben, welches mir ermöglicht ein "Kreuzworträtsel" in Form eines zweidimensionalen Arrays auf ein bestimmtes Wort zu durchsuchen. Ich habe versucht das Rätsel in Horizontaler, Vertikaler und Diagonaler Richtung zu durchsuchen. Außerdem habe ich versucht die Anzahl des im Rätsel vorkommenden Wortes jeweils in Horizontaler, Vertikaler und Diagonaler Richtung zu zählen und auszugeben, jedoch Streikt mein Programm und liefert nicht das gewünschte Ergebnis, könnt ihr mir evtl weiterhelfen.
Dankeschön
//Programm
public class Kreuzwortraetsel
{
public static void main(String[] args)
{
char [][] raetsel =
{
{'Q', 'W', 'J', 'A', 'V', 'A', 'J'},
{'R', 'T', 'K', 'V', 'A', 'L', 'A'},
{'M', 'J', 'J', 'Z', 'C', 'V', 'V'},
{'N', 'X', 'Y', 'A', 'P', 'B', 'A'},
{'T', 'L', 'K', 'S', 'V', 'E', 'D'},
{'M', 'T', 'V', 'Q', 'R', 'A', 'S'},
{'Y', 'J', 'A', 'V', 'A', 'V', 'B'}
};
int n = 0;
System.out.println(CharMatrix.toString(raetsel));
String word = "JAVA";
//Horizontal
System.out.println("Das Wort " + word + " ist " + n + " mal in Horizontaler Richtung enthalten" + (searchHorizontal(raetsel, word, n)));
//Vertikal
System.out.println("Das Wort " + word + " ist " + n + " mal in Vertikaler Richtung enthalten" + (searchVertikal(raetsel, word, n)));
//Diagonal
System.out.println("Das Wort " + word + " ist " + n + " mal in Diagonaler Richtung enthalten" + (searchDiagonal(raetsel, word, n)));
}
public static int searchHorizontal(char[][] raetsel, String word, int n)
{
for(int i = 0; i < raetsel.length - 1; i++)
for(int j = 0; j < raetsel.length - 1; j++)
{
int k=0;
while (k < word.length() && raetsel [j+k] == word.charAt(k))
k++;
if(k == word.length())
{
n++;
continue;
}
}
return n;
}
public static int searchVertikal(char[][] raetsel, String word, int n)
{
for(int i = 0; i < raetsel.length -1; i++)
for(int j = 0; j < raetsel.length -1; j++)
{int k=0;
while (k < word.length() && raetsel [i+k][j] == word.charAt(k))
k++;
if(k == word.length())
{
n++;
continue;
}
}
return n;
}
public static int searchDiagonal(char[][] raetsel, String word, int n)
{
for(int i = 0; i < raetsel.length -1; i++)
for (int j = 0; j < raetsel.length; j++)
{
int k=0;
while (k < word.length() && raetsel [i+k][j+k] == word.charAt(k))
k++;
if(k == word.length())
{
n++;
continue;
}
}
return n;
}
}
//Programm ende
Ich versuche die Aufgabenstellung knapp zu formulieren und zwar wollte ich ein Programm schreiben, welches mir ermöglicht ein "Kreuzworträtsel" in Form eines zweidimensionalen Arrays auf ein bestimmtes Wort zu durchsuchen. Ich habe versucht das Rätsel in Horizontaler, Vertikaler und Diagonaler Richtung zu durchsuchen. Außerdem habe ich versucht die Anzahl des im Rätsel vorkommenden Wortes jeweils in Horizontaler, Vertikaler und Diagonaler Richtung zu zählen und auszugeben, jedoch Streikt mein Programm und liefert nicht das gewünschte Ergebnis, könnt ihr mir evtl weiterhelfen.
Dankeschön
//Programm
public class Kreuzwortraetsel
{
public static void main(String[] args)
{
char [][] raetsel =
{
{'Q', 'W', 'J', 'A', 'V', 'A', 'J'},
{'R', 'T', 'K', 'V', 'A', 'L', 'A'},
{'M', 'J', 'J', 'Z', 'C', 'V', 'V'},
{'N', 'X', 'Y', 'A', 'P', 'B', 'A'},
{'T', 'L', 'K', 'S', 'V', 'E', 'D'},
{'M', 'T', 'V', 'Q', 'R', 'A', 'S'},
{'Y', 'J', 'A', 'V', 'A', 'V', 'B'}
};
int n = 0;
System.out.println(CharMatrix.toString(raetsel));
String word = "JAVA";
//Horizontal
System.out.println("Das Wort " + word + " ist " + n + " mal in Horizontaler Richtung enthalten" + (searchHorizontal(raetsel, word, n)));
//Vertikal
System.out.println("Das Wort " + word + " ist " + n + " mal in Vertikaler Richtung enthalten" + (searchVertikal(raetsel, word, n)));
//Diagonal
System.out.println("Das Wort " + word + " ist " + n + " mal in Diagonaler Richtung enthalten" + (searchDiagonal(raetsel, word, n)));
}
public static int searchHorizontal(char[][] raetsel, String word, int n)
{
for(int i = 0; i < raetsel.length - 1; i++)
for(int j = 0; j < raetsel.length - 1; j++)
{
int k=0;
while (k < word.length() && raetsel [j+k] == word.charAt(k))
k++;
if(k == word.length())
{
n++;
continue;
}
}
return n;
}
public static int searchVertikal(char[][] raetsel, String word, int n)
{
for(int i = 0; i < raetsel.length -1; i++)
for(int j = 0; j < raetsel.length -1; j++)
{int k=0;
while (k < word.length() && raetsel [i+k][j] == word.charAt(k))
k++;
if(k == word.length())
{
n++;
continue;
}
}
return n;
}
public static int searchDiagonal(char[][] raetsel, String word, int n)
{
for(int i = 0; i < raetsel.length -1; i++)
for (int j = 0; j < raetsel.length; j++)
{
int k=0;
while (k < word.length() && raetsel [i+k][j+k] == word.charAt(k))
k++;
if(k == word.length())
{
n++;
continue;
}
}
return n;
}
}
//Programm ende