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.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();
}
}
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.
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment