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

Updated Crossword Class (Task 2) + Updated Main Class

-Implémentation de la recherche d’un mot dans le tableau Crossword
parent 450f277d
Branches
No related tags found
No related merge requests found
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; import java.util.Scanner;
import java.io.*; import java.io.*;
...@@ -6,8 +7,7 @@ import java.io.*; ...@@ -6,8 +7,7 @@ import java.io.*;
* Class Crossword : Creates a 2D array of characters read from the input file * Class Crossword : Creates a 2D array of characters read from the input file
*/ */
public class Crossword public class Crossword {
{
private char array[][]; // Tableaux 2D contenant les caractères private char array[][]; // Tableaux 2D contenant les caractères
private int rows; // Nombres de lignes de array private int rows; // Nombres de lignes de array
private int columns; // Nombres de colonnes de array private int columns; // Nombres de colonnes de array
...@@ -58,30 +58,64 @@ public class Crossword ...@@ -58,30 +58,64 @@ public class Crossword
*** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/ *** Si touver, mettre à jour les valeurs (PositionY, PositionX, EndY, EndX)*/
public boolean search(String word) { public boolean search(String word) {
// Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire. // 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;
}
// 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é 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] */ /* 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) { private boolean searchRow(int y, int x, String word) {
int length = word.length();
// Ecrire code ici ... String wordfound = "";
return false; 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] */ /* 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) { private boolean searchColumn(int y, int x, String word) {
int length = word.length();
// Ecrire code ici ... String wordfound = "";
return false; 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) */ /*** Methode pour visualiser le tableau. (Déjà fourni) */
...@@ -105,5 +139,14 @@ public class Crossword ...@@ -105,5 +139,14 @@ public class Crossword
CrosswordGUI.display(array, PositionY, PositionX, EndY, EndX); 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();
}
} }
import java.io.*; import java.io.*;
import java.lang.reflect.Array;
import java.util.Scanner; import java.util.Scanner;
/*** Methode Main : Lire le fichier donné et créer un tableau 2D qui contint le characters du fichier. /*** Methode Main : Lire le fichier donné et créer un tableau 2D qui contint le characters du fichier.
...@@ -10,16 +11,24 @@ public class Main { ...@@ -10,16 +11,24 @@ public class Main {
public static void main(String args[]) throws IOException { public static void main(String args[]) throws IOException {
String filename; String filename;
Scanner scan = new Scanner(System.in);
// Demander le nom de Fichier à l'utisateur // Demander le nom de Fichier à l'utisateur
// Constriure une instance de la classe "Crossword" filename = scan.next();
// Crossword tableau = ... File file = new File(filename);
// Construire une instance de la classe "Crossword"
//tableau.display(); Crossword tableau = new Crossword(file);
tableau.display();
// Demander un mot à chercher ... // Demander un mot à chercher ...
while (true) {
String word = scan.next();
// Utiliser la méthode Crossword.search() pour chercher le mot // 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. // 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é...");
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment