From b67f222706103583eea2284a4b0a0e80fc04de5c Mon Sep 17 00:00:00 2001
From: MSAYIF Bassem <bassem.msayif@etu.univ-amu.fr>
Date: Sun, 20 Sep 2020 14:07:23 +0200
Subject: [PATCH] =?UTF-8?q?[TP=202=20+=20TACHES=20OPTIONNELLES=20FINIS]=20?=
 =?UTF-8?q?Updated=20Crossword=20Class=20and=20Main=20Class=20Classe=20Cro?=
 =?UTF-8?q?ssword:=20-Ajout=20de=20la=20posibilit=C3=A9=20de=20compter=20l?=
 =?UTF-8?q?es=20mots=20en=20diagonale=20dans=20la=20m=C3=A9thode=20countWo?=
 =?UTF-8?q?rd(String=20word).=20-Ajout=20de=20deux=20m=C3=A9thodes=20perm?=
 =?UTF-8?q?=C3=A9ttant=20de=20chercher=20des=20mots=20en=20diagonale=20ver?=
 =?UTF-8?q?s=20la=20droite=20et=20la=20gauche.=20Classe=20Main:=20-Am?=
 =?UTF-8?q?=C3=A9lioration=20du=20message=20dans=20le=20terminal.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 tp2/Crossword.java | 58 +++++++++++++++++++++++++++++++++++++++++++---
 tp2/Main.java      |  4 ++--
 2 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/tp2/Crossword.java b/tp2/Crossword.java
index 1fdff78..fdd4013 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 0c9aa33..7eae744 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é...");
         }
 
     }
-- 
GitLab