From 03bf060e58a0ac0ce4d3c03bbf89263323937f55 Mon Sep 17 00:00:00 2001 From: MSAYIF Bassem <bassem.msayif@etu.univ-amu.fr> Date: Sat, 19 Sep 2020 23:55:31 +0200 Subject: [PATCH] =?UTF-8?q?Updated=20Crossword=20Class=20(Task=202)=20+=20?= =?UTF-8?q?Updated=20Main=20Class=20-Impl=C3=A9mentation=20de=20la=20reche?= =?UTF-8?q?rche=20d=E2=80=99un=20mot=20dans=20le=20tableau=20Crossword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tp2/Crossword.java | 235 +++++++++++++++++++++++++++------------------ tp2/Main.java | 33 ++++--- 2 files changed, 160 insertions(+), 108 deletions(-) diff --git a/tp2/Crossword.java b/tp2/Crossword.java index 9466705..4347bd4 100644 --- a/tp2/Crossword.java +++ b/tp2/Crossword.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Arrays; import java.util.Scanner; import java.io.*; @@ -6,104 +7,146 @@ import java.io.*; * Class Crossword : Creates a 2D array of characters read from the input file */ -public class Crossword -{ - private char array[][]; // Tableaux 2D contenant les caractères - private int rows; // Nombres de lignes de array - private int columns; // Nombres de colonnes de array - - private int WordCount; // Nombre d'occurences d'un mot - private int PositionX; // Colonne de début du mot - private int PositionY; // Ligne de début du mot - private int EndX; // Colonne de fin du mot - private int EndY; // Ligne de fin du mot - - - /*** Constructor: Reads each line from File <file> and writes to a new row in the array. - ** Updates <rows> and <columns> to height and width of the array. */ - public Crossword(File file) throws IOException { - - if(!file.exists()){ +public class Crossword { + private char array[][]; // Tableaux 2D contenant les caractères + private int rows; // Nombres de lignes de array + private int columns; // Nombres de colonnes de array + + private int WordCount; // Nombre d'occurences d'un mot + private int PositionX; // Colonne de début du mot + private int PositionY; // Ligne de début du mot + private int EndX; // Colonne de fin du mot + private int EndY; // Ligne de fin du mot + + + /*** Constructor: Reads each line from File <file> and writes to a new row in the array. + ** Updates <rows> and <columns> to height and width of the array. */ + public Crossword(File file) throws IOException { + + if (!file.exists()) { System.out.println("le fichier n'existe pas"); return; } - // Code pour calculer la taille du tableau et initialiser les variables d'instance "rows" et "columns" - - Scanner scan = new Scanner(file); - - columns = scan.next().length(); - while (scan.hasNextLine()) { - rows++; - scan.nextLine(); - } - - // Code pour créer le tableau "array" - char[][] array = new char[rows][columns]; - - // Code pour remplir "array" avec les caractères du Fichier "file" - Scanner scanner = new Scanner(file); - for (int i = 0; i < rows; i++) { - String fileLine = scanner.nextLine(); - for (int j = 0; j < columns; j++) { - char nextChar = fileLine.charAt(j); - array[i][j] = nextChar; - } - } - this.array = array; - - } - - - /*** Methode Search(String) : Trouver le premier occurence du mot <word> dans le Tableau - *** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/ - - public boolean search(String word) { - - // Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire. - - // Chercher le premier caractère du mot "word" dans le tableau array. - // Si array[i][j] contient ce caractère, alors le mot peut apparaître dans le même ligne, - // ou dans le même colonne. Utiliser les methodes SearchRow() ou searchColumn() selon le cas. - - return false; // mot pas trouvé - } - - /* Methode Interne SearchRow(int,int, String) : Cherche une ligne du tableaux pour le mot <word> à partir de array[y][x] */ - - private boolean searchRow(int y, int x, String word) { - - // Ecrire code ici ... - return false; - } - - /* Methode Interne SearchRow(int,int, String) : Cherche une colonne du tableaux pour le mot <word> à partir de array[y][x] */ - - private boolean searchColumn(int y, int x, String word) { - - // Ecrire code ici ... - return false; - } - - /*** Methode pour visualiser le tableau. (Déjà fourni) */ - public void display() { - if (rows>0 && columns>0) - CrosswordGUI.display(array); - else - System.out.println("Error: Array is Empty."); - } - - /*** Methode pour visualiser le tableau avec le mot en surbrillance. (Déjà fourni) */ - public void displayWord() { - if ((PositionX<0) || (PositionX>EndX) || (EndX>=columns)) { - System.out.println("Error: Incorrect x-coordinates for Word"); - return; - } - if ((PositionY<0) || (PositionY>EndY) || (EndY>=rows)) { - System.out.println("Error: Incorrect y-coordinates for Word"); - return; - } - CrosswordGUI.display(array, PositionY, PositionX, EndY, EndX); - } - + // Code pour calculer la taille du tableau et initialiser les variables d'instance "rows" et "columns" + + Scanner scan = new Scanner(file); + + columns = scan.next().length(); + while (scan.hasNextLine()) { + rows++; + scan.nextLine(); + } + + // Code pour créer le tableau "array" + char[][] array = new char[rows][columns]; + + // Code pour remplir "array" avec les caractères du Fichier "file" + Scanner scanner = new Scanner(file); + for (int i = 0; i < rows; i++) { + String fileLine = scanner.nextLine(); + for (int j = 0; j < columns; j++) { + char nextChar = fileLine.charAt(j); + array[i][j] = nextChar; + } + } + this.array = array; + + } + + + /*** Methode Search(String) : Trouver le premier occurence du mot <word> dans le Tableau + *** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/ + + public boolean search(String word) { + // Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire. + if (word.length() <= 0) { + return false; + } + for (int j = 0; j < columns; ++j) { + for (int i = 0; i < rows; ++i) { + if (searchRow(j, i, word)) { + PositionX = j; + EndX = PositionX + word.length() - 1; + PositionY = i; + EndY = PositionY; + //System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY); + return true; + } else if (searchColumn(j, i, word)) { + PositionX = j; + EndX = PositionX; + PositionY = i; + EndY = PositionY + word.length() - 1; + //System.out.println(PositionX + " " + EndX + " " + PositionY + " " + EndY); + return true; + } + + } + + } + return false; // mot pas trouvé + } + + /* Methode Interne SearchRow(int,int, String) : Cherche une ligne du tableaux pour le mot <word> à partir de array[y][x] */ + + private boolean searchRow(int y, int x, String word) { + int length = word.length(); + String wordfound = ""; + for (int z = 0; z < length; ++z) { + if (y < this.columns) { + if (array[x][y] == word.charAt(z)) { + wordfound = wordfound + word.charAt(z); + ++y; + } + } + } + return (wordfound.equals(word)); + } + + /* Methode Interne SearchRow(int,int, String) : Cherche une colonne du tableaux pour le mot <word> à partir de array[y][x] */ + + private boolean searchColumn(int y, int x, String word) { + int length = word.length(); + String wordfound = ""; + for (int z = 0; z < length; ++z) { + if (x < this.rows) { + if (array[x][y] == word.charAt(z)) { + wordfound = wordfound + word.charAt(z); + ++x; + } + } + } + return (wordfound.equals(word)); + } + + /*** Methode pour visualiser le tableau. (Déjà fourni) */ + public void display() { + if (rows > 0 && columns > 0) + CrosswordGUI.display(array); + else + System.out.println("Error: Array is Empty."); + } + + /*** Methode pour visualiser le tableau avec le mot en surbrillance. (Déjà fourni) */ + public void displayWord() { + if ((PositionX < 0) || (PositionX > EndX) || (EndX >= columns)) { + System.out.println("Error: Incorrect x-coordinates for Word"); + return; + } + if ((PositionY < 0) || (PositionY > EndY) || (EndY >= rows)) { + System.out.println("Error: Incorrect y-coordinates for Word"); + return; + } + CrosswordGUI.display(array, PositionY, PositionX, EndY, EndX); + } + + public void printArr() { + for (int j = 0; j < columns; j++) { + for (int i = 0; i < rows; i++) + System.out.print(array[i][j] + ""); + } + System.out.println(); + } + + } diff --git a/tp2/Main.java b/tp2/Main.java index 18bd643..0c9aa33 100644 --- a/tp2/Main.java +++ b/tp2/Main.java @@ -1,25 +1,34 @@ import java.io.*; +import java.lang.reflect.Array; import java.util.Scanner; /*** Methode Main : Lire le fichier donné et créer un tableau 2D qui contint le characters du fichier. *** Demander un mot d'utilisateur et le chercher dans le tableau - *** Visualiser the tableau avec le mot trouver ou informer l'utilisatuer si le mot n'est pas present */ + *** Visualiser the tableau avec le mot trouver ou informer l'utilisatuer si le mot n'est pas present */ public class Main { public static void main(String args[]) throws IOException { - String filename; - - // Demander le nom de Fichier à l'utisateur - // Constriure une instance de la classe "Crossword" - // Crossword tableau = ... - - //tableau.display(); + String filename; + Scanner scan = new Scanner(System.in); + + // Demander le nom de Fichier à l'utisateur + filename = scan.next(); + File file = new File(filename); + // Construire une instance de la classe "Crossword" + Crossword tableau = new Crossword(file); + tableau.display(); + // Demander un mot à chercher ... + while (true) { + String word = scan.next(); + // Utiliser la méthode Crossword.search() pour chercher le mot + 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é..."); + } - // Demander un mot à chercher ... - // Utiliser la méthode Crossword.search() pour chercher le mot - // Ecrire un message sur le terminal pour informer l'utilisateur du resultat. - } } -- GitLab