diff --git a/tp2/.classpath b/tp2/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..3f3893aff96296c1ce15c61728d13a5d97589bbe --- /dev/null +++ b/tp2/.classpath @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="src" path=""/> + <classpathentry kind="output" path=""/> +</classpath> diff --git a/tp2/.gitignore b/tp2/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..a47bb674428539104d28627e15bda6eda273e532 --- /dev/null +++ b/tp2/.gitignore @@ -0,0 +1,5 @@ +/Crossword.class +/CrosswordGUI$Directions.class +/CrosswordGUI.class +/CrosswordPanel.class +/Main.class diff --git a/tp2/.project b/tp2/.project new file mode 100644 index 0000000000000000000000000000000000000000..568f1347f6ba97ea40d0443a8441c5410433e37f --- /dev/null +++ b/tp2/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>tp2</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/tp2/Crossword.java b/tp2/Crossword.java new file mode 100644 index 0000000000000000000000000000000000000000..cfaf6fa51396e0ca59068f0dd0df8dcb0ddfdddf --- /dev/null +++ b/tp2/Crossword.java @@ -0,0 +1,187 @@ +import java.util.Scanner; +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 { + + // Code pour calculer la taille du tableau et initialiser les variables d'instance "rows" et "columns" + + Scanner input = new Scanner(file); + columns = input.next().length(); + while (input.hasNextLine()) { + rows++; + input.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) { + WordCount = 0; + // Verifier que le taile du mot "word" est supérieure à zero. Sinon rien à faire. + if (word.length()> 0) { + + + + // Chercher le premier caractère du mot "word" dans le tableau array. + char firstchar = word.charAt(0); + for (int r = 0; r < rows; ++r) { + for (int c = 0; c < columns; ++c) { + + if (firstchar == array[r][c]) { + PositionX = c; + PositionY = r; + + + // 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. + + if (searchRow(PositionX, PositionY, word)) { + System.out.println("Word found in the row"); + displayWord(); + WordCount += 1; + + } + if (searchColumn(PositionX, PositionY, word)) { + displayWord(); + System.out.println("Word found in the column"); + WordCount += 1; + + } + + } + + } + } + + } + return (WordCount != 0);// 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 x, int y, String word) { + int wordlength = word.length(); + int i = 0; + String wordfound = ""; + + + + for ( int chary = 0; chary < wordlength; chary++ ) { + if (word.charAt(i) == array[PositionX][chary + y]) { + i += 1; + + wordfound = wordfound + array[PositionX][chary + y]; + } + else return false; + + } + if (wordfound.equals(word)){ + EndY = wordlength -1 + y; + EndX = PositionX; + } + + + 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 x, int y, String word) { + + int wordlength = word.length(); + int i = 0; + String wordfound = ""; + + for ( int charx = 0; charx < wordlength; charx++ ) { + if (word.charAt(i) == array[charx + x][PositionY]) { + i += 1; + + wordfound = wordfound + array[charx + x][PositionY]; + + } + else return false; + + } + if (wordfound.equals(word)){ + EndX = wordlength -1 + x; + EndY = PositionY; + } + + + + 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 int countWord(String word) { + return WordCount; + } + + +} + diff --git a/tp2/CrosswordGUI.java b/tp2/CrosswordGUI.java new file mode 100644 index 0000000000000000000000000000000000000000..5e69cde946fd4e57eb5ce55bde5ec68445f49404 --- /dev/null +++ b/tp2/CrosswordGUI.java @@ -0,0 +1,100 @@ +/** + * Classe pour visualiser le tableau "Crossword" + * ATTENTION: NE PAS MODIFIER CETTE CLASSE +**/ +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.FlowLayout; +import java.awt.GridLayout; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.SwingUtilities; + + +public class CrosswordGUI +{ + public enum Directions{HORIZONTAL, VERTICAL}; + + public static void display(char array[][]) + { + final CrosswordPanel panel = new CrosswordPanel(array); + displayPanel(panel); + } + + public static void display(char array[][], int y1, int x1, int y2, int x2) + { + final CrosswordPanel panel = new CrosswordPanel(array); + if(y1==y2 && x1<=x2) + panel.highlight(y1,x1,1+x2-x1, CrosswordGUI.Directions.HORIZONTAL); + if(x1==x2 && y1<=y2) + panel.highlight(y1,x1,1+y2-y1, CrosswordGUI.Directions.VERTICAL); + displayPanel(panel); + } + + private static void displayPanel(CrosswordPanel panel) + { + JFrame f = new JFrame(); + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + f.getContentPane().setLayout(new BorderLayout()); + + JPanel container = new JPanel(new FlowLayout()); + container.add(panel); + f.getContentPane().add(container, BorderLayout.CENTER); + + f.setSize(800, 800); + f.setLocationRelativeTo(null); + f.setVisible(true); + } +} + +class CrosswordPanel extends JPanel +{ + private JTextField textFields[][]; + + public CrosswordPanel(char array[][]) + { + int height = array.length; + int width = array[0].length; + setLayout(new GridLayout(height, width)); + textFields = new JTextField[height][width]; + + for (int y=0; y<height; y++) + { + for (int x=0; x<width; x++) + { + char c = array[y][x]; + if (c != 0) + { + textFields[y][x] = new JTextField(String.valueOf(c)); + textFields[y][x].setFont(textFields[y][x].getFont().deriveFont(20.0f)); + add(textFields[y][x]); + } + else + { + add(new JLabel()); + } + } + } + repaint(); + } + + public void highlight(int startY, int startX, int length, CrosswordGUI.Directions dir) { + int endY = startY+1; + int endX = startX+1; + if (dir == CrosswordGUI.Directions.HORIZONTAL) endX += (length-1); + else endY += (length-1); + + for (int y=startY; y < endY; y++) + for (int x=startX; x < endX; x++) + { + textFields[y][x].setBackground(Color.yellow); + } + repaint(); + } + +} diff --git a/tp2/Main.java b/tp2/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..70d96cd49d1464ed873d7c9a77316ee39f5ee905 --- /dev/null +++ b/tp2/Main.java @@ -0,0 +1,41 @@ +import java.io.*; +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 */ + +public class Main { + + public static void main(String args[]) throws IOException { + + + + String filename; + Scanner input = new Scanner(System.in); + // Demander le nom de Fichier à l'utisateur + System.out.println("filename:"); + + filename = input.next(); + File file = new File(filename); + if(file.exists()) + System.out.println("File exists"); + else System.exit(1); + // Construire une instance de la classe "Crossword" + Crossword tableau = new Crossword(file); + + tableau.display(); + tableau.search("MA"); + + + System.out.println("Nombre de fois : " + tableau.countWord("MA")); + + + + + // 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. + + } +} diff --git a/tp2/WORDS.txt b/tp2/WORDS.txt new file mode 100644 index 0000000000000000000000000000000000000000..314a1f4c5a71f50e3ebdb30ea54cd70c13eac2c2 --- /dev/null +++ b/tp2/WORDS.txt @@ -0,0 +1,10 @@ +KSHFJGIVCBVN +ASDVKNADLDSS +MANASVADFERT +UMARSEILLEOA +WATERXXHOTID +POKBSEEYOURE +RRRQUOI?SDFG +KARAMBALOOKR +PLOCKDLROOFJ +MNMSIURGNIPD