diff --git a/tp2/Crossword.java b/tp2/Crossword.java new file mode 100644 index 0000000000000000000000000000000000000000..110a866886a6190d4c3263eb23e08a2a05d31abd --- /dev/null +++ b/tp2/Crossword.java @@ -0,0 +1,86 @@ +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" + + // Code pour créer le tableau "array" + + // Code pour remplir "array" avec les caractères du Fichier "file" + + } + + + /*** 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); + } + +} + 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..18bd6435124757315547773625f0385a4236a366 --- /dev/null +++ b/tp2/Main.java @@ -0,0 +1,25 @@ +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; + + // Demander le nom de Fichier à l'utisateur + // Constriure une instance de la classe "Crossword" + // Crossword tableau = ... + + //tableau.display(); + + // 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