Implémentation Algorithme A star dans un programme JAVA
Bonsoir à tous,
Je débute dans le JAVA et je rencontre quelques difficultés au niveau d'un programme que j'essaye d'implémenter.
J'ai créer un programme qui permet de générer une matrice de 0 et de 1 tirés aléatoirement.
En plus de créer un affichage graphique (cases noires et blanches), j'enregistre dans un fichier texte les coordonnées et la valeur à chaque point de la matrice.
Matrice: 110
001
101
Fichier texte: 001
011
020
.
.
221
Je souhaiterais maintenant utiliser ce fichier texte et implémenter l'algorithme A star ..
Ayant des connaissances assez limitées, j'ai essayé de bidouiller quelques bouts de code trouvés sur le net qui implémentaient l'algorithme.
Je n'ai pas réussit cependant à utiliser mon fichier texte.
Quelqu'un aurait il des conseils ou un "squelette" pour l'implémentation de l'algorithme de façon "propre" ?
Je vous remercie d'avance,
Cordialement,
Aurevoiiiir
PS: Voilà le code de matrice que j'utilise (Alors il est assez moche mais comme déjà expliqué, je débute!!)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
package matrice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import static matrice.matrice.NB_COLONNES;
import static matrice.matrice.NB_LIGNES;
public class main {
public static void main(String[] args) {
JFrame frame=new JFrame("Matrice");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
matrice matrice = new matrice(40);
fichier fichier = new fichier();
JPanel panel = new JPanel(new GridLayout(0,NB_COLONNES)); // on n'a pas besoin de mettre le nombre de lignes si on donne un nombre de colonnes
for( int i=0; i<NB_LIGNES; i++) {
for( int j=0; j<NB_COLONNES; j++) {
int valeur = matrice.getMatrice()[i][j];
JPanel cellule = new JPanel(); // on utilise un simple JPanel pour chaque cellule, donc on adaptera la couleur de fond (background)
cellule.setPreferredSize(new Dimension(32,32)); // donne une taille de 32x32 pixels par défaut
if ( valeur==0 ) {
cellule.setBackground(Color.WHITE);
}
else {
cellule.setBackground(Color.BLACK);
}
panel.add(cellule);
}
}
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
matrice.afficher();
System.out.println("");
fichier.ecrire("matrice.txt", matrice.toString());
fichier.lire("matrice.txt", "");
System.out.println("");
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package matrice;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class fichier {
public void ecrire(String nomFic, String texte){
//on va chercher le chemin et le nom du fichier et on met tout ca dans un String
String adressedufichier = System.getProperty("user.dir") + "/"+ nomFic;
//on met try si jamais il y a une exception
try{
FileWriter fw = new FileWriter(adressedufichier, true);
// le BufferedWriter output auquel on donne comme argument le FileWriter fw cree juste au dessus
BufferedWriter output = new BufferedWriter(fw);
//on marque dans le fichier ou plutot dans le BufferedWriter qui sert comme un tampon(stream)
output.write(texte);
//ensuite flush envoie dans le fichier, ne pas oublier cette methode pour le BufferedWriter
output.flush();
//et on le ferme
output.close();
System.out.println("Fichier Créé\n");
}
catch(IOException ioe){
System.out.print("Erreur : \n");
ioe.printStackTrace();
}
}
public void lire(String nomFic, String texte){
String fichier ="matrice.txt";
//lecture du fichier texte
try{
InputStream ips=new FileInputStream(fichier);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){
System.out.println(ligne);
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
package matrice;
import java.util.Random;
public class matrice {
public static final int NB_COLONNES = 10;
public static final int NB_LIGNES = 10;
int[][] matrice=new int[NB_LIGNES][NB_COLONNES];
public int[][] getMatrice() {
return matrice;
}
public matrice(int nombre_cases_noires) {
this.initialisation_matrice();
for(int i=0 ; i<nombre_cases_noires; i++)
{
Integer abscisses = this.generation_random(matrice.length);
Integer ordonnees = this.generation_random(matrice.length);
this.remplir_matrice(abscisses, ordonnees);
}
}
private void remplir_matrice(Integer abscisses, Integer ordonnees){
boolean modification = false;
if((abscisses == 0 && ordonnees == 0) || (abscisses == this.matrice.length-1 && ordonnees == this.matrice.length-1))
{
modification = true;
}
else if(this.matrice[abscisses][ordonnees] == 1 && modification == true)
{
this.remplir_matrice(this.generation_random(matrice.length), this.generation_random(matrice.length));
}
else
{
this.matrice[abscisses][ordonnees] = 1;
}
}
private void initialisation_matrice(){
/* for (int[] x : this.matrice)
{
for(int y : x)
{
y = 0;
}
} */
for(int i=0 ; i<this.matrice.length; i++)
{
for(int j=0 ; j<this.matrice.length; j++)
{
this.matrice[i][j]=0;
}
}
}
private Integer generation_random(final Integer limite){
return new Random().nextInt(limite);
}
// methode affichage
public String afficher() {
int i, j;
for(i=0; i<matrice.length; i++){
for(j=0; j<matrice.length ; j++){
System.out.print(matrice[i][j]);
}
System.out.println("");
}
return null;
}
@Override
public String toString() {
String str ="";
/*for (int[] x : this.matrice)
{
for (int y : x)
{
str += Integer.toString(y);
}
str += "\n";
}*/
for(int i=0; i<this.matrice.length; i++){
for(int j=0; j<this.matrice.length; j++)
{
str += Integer.toString(i) + Integer.toString(j) + Integer.toString(this.matrice[i][j]) + "\n";
}
}
return str;
}
} |