diff --git a/tp2/Crossword.java b/tp2/Crossword.java index 1fdff783d38a979fa52d790494bfdaa4b2cb75ef..fdd4013f52fc66887a324f7791d5cf6307cc1f16 100644 --- a/tp2/Crossword.java +++ b/tp2/Crossword.java @@ -78,6 +78,20 @@ public class Crossword { EndY = PositionY + word.length() - 1; //System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY); 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 { 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) */ public void display() { if (rows > 0 && columns > 0) @@ -147,14 +195,18 @@ public class Crossword { System.out.println(); } - public int countWord(String word){ + public int countWord(String word) { this.WordCount = 0; for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) { if (searchRow(j, i, word)) { - this.WordCount ++; + this.WordCount++; } 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++; } } } diff --git a/tp2/Main.java b/tp2/Main.java index 0c9aa33fa0ecd5bb11e6b1f446c4e7855cbe906c..7eae7447389e682c659a2fc8046f31e8253b76e3 100644 --- a/tp2/Main.java +++ b/tp2/Main.java @@ -26,8 +26,8 @@ public class Main { tableau.search(word); // Ecrire un message sur le terminal pour informer l'utilisateur du resultat. if (tableau.search(word)) { - System.out.println("Trouvé!"); - } else System.out.println("Pas trouvé..."); + System.out.println("Le mot " + word + " a été trouvé " + tableau.countWord(word) + " fois"); + } else System.out.println("Le mot " + word + " n'a pas été trouvé..."); } }