Skip to content
Snippets Groups Projects
Commit 4d4e6e65 authored by Teo Blaise Kaplanski's avatar Teo Blaise Kaplanski
Browse files

fini

parents 11b6c68b 8d65438d
No related branches found
No related tags found
No related merge requests found
<?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>
/Crossword.class
/CrosswordGUI$Directions.class
/CrosswordGUI.class
/CrosswordPanel.class
/Main.class
<?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>
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;
}
}
/**
* 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();
}
}
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.
}
}
KSHFJGIVCBVN
ASDVKNADLDSS
MANASVADFERT
UMARSEILLEOA
WATERXXHOTID
POKBSEEYOURE
RRRQUOI?SDFG
KARAMBALOOKR
PLOCKDLROOFJ
MNMSIURGNIPD
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment