Skip to content
Snippets Groups Projects
Commit b67f2227 authored by MSAYIF Bassem's avatar MSAYIF Bassem
Browse files

[TP 2 + TACHES OPTIONNELLES FINIS] Updated Crossword Class and Main Class

Classe Crossword:
-Ajout de la posibilité de compter les mots en diagonale dans la méthode countWord(String word).
-Ajout de deux méthodes perméttant de chercher des mots en diagonale vers la droite et la gauche.
Classe Main:
-Amélioration du message dans le terminal.
parent 24fbab55
No related branches found
No related tags found
No related merge requests found
...@@ -78,6 +78,20 @@ public class Crossword { ...@@ -78,6 +78,20 @@ public class Crossword {
EndY = PositionY + word.length() - 1; EndY = PositionY + word.length() - 1;
//System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY); //System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY);
return true; return true;
} else if (searchDiagonalRight(j, i, word)) {
PositionX = j;
EndX = PositionX + word.length() - 1;
PositionY = i;
EndY = PositionY + word.length() - 1;
//System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY);
return true;
} else if (searchDiagonalLeft(j, i, word)) {
PositionX = j;
EndX = PositionX - word.length() + 1;
PositionY = i;
EndY = PositionY + word.length() - 1;
//System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY);
return true;
} }
} }
...@@ -118,6 +132,40 @@ public class Crossword { ...@@ -118,6 +132,40 @@ public class Crossword {
return (wordfound.equals(word)); return (wordfound.equals(word));
} }
private boolean searchDiagonalRight(int y, int x, String word) {
int length = word.length();
String wordfound = "";
for (int z = 0; z < length; ++z) {
if (y < this.columns) {
if (x < this.rows) {
if (array[x][y] == word.charAt(z)) {
wordfound = wordfound + word.charAt(z);
++y;
++x;
}
}
}
}
return (wordfound.equals(word));
}
private boolean searchDiagonalLeft(int y, int x, String word) {
int length = word.length();
String wordfound = "";
for (int z = 0; z < length; ++z) {
if (y >= 0) {
if (x < this.rows) {
if (array[x][y] == word.charAt(z)) {
wordfound = wordfound + word.charAt(z);
--y;
++x;
}
}
}
}
return (wordfound.equals(word));
}
/*** Methode pour visualiser le tableau. (Déjà fourni) */ /*** Methode pour visualiser le tableau. (Déjà fourni) */
public void display() { public void display() {
if (rows > 0 && columns > 0) if (rows > 0 && columns > 0)
...@@ -147,14 +195,18 @@ public class Crossword { ...@@ -147,14 +195,18 @@ public class Crossword {
System.out.println(); System.out.println();
} }
public int countWord(String word){ public int countWord(String word) {
this.WordCount = 0; this.WordCount = 0;
for (int j = 0; j < columns; ++j) { for (int j = 0; j < columns; ++j) {
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i) {
if (searchRow(j, i, word)) { if (searchRow(j, i, word)) {
this.WordCount ++; this.WordCount++;
} else if (searchColumn(j, i, word)) { } else if (searchColumn(j, i, word)) {
this.WordCount ++; this.WordCount++;
} else if (searchDiagonalRight(j, i, word)) {
this.WordCount++;
} else if (searchDiagonalLeft(j, i, word)) {
this.WordCount++;
} }
} }
} }
......
...@@ -26,8 +26,8 @@ public class Main { ...@@ -26,8 +26,8 @@ public class Main {
tableau.search(word); tableau.search(word);
// Ecrire un message sur le terminal pour informer l'utilisateur du resultat. // Ecrire un message sur le terminal pour informer l'utilisateur du resultat.
if (tableau.search(word)) { if (tableau.search(word)) {
System.out.println("Trouvé!"); System.out.println("Le mot " + word + " a été trouvé " + tableau.countWord(word) + " fois");
} else System.out.println("Pas trouvé..."); } else System.out.println("Le mot " + word + " n'a pas été trouvé...");
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment