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 108 109 110 111 112 113 114 115
| import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Test {
public static class JeuElement {
int x, y;
public JeuElement(int x, int y) {
this.x = x;
this.y = y;
}
}
public static class Triangle extends JeuElement {
public Triangle(int x, int y) {
super(x, y);
}
}
public static class Carre extends JeuElement {
public Carre(int x, int y) {
super(x, y);
}
}
public static class Arbre extends JeuElement {
public Arbre(int x, int y) {
super(x, y);
}
}
public static class Fleurs extends JeuElement {
public Fleurs(int x, int y) {
super(x, y);
}
}
private static String[][] EditeurJeu = new String[15][15];
public static ArrayList<Fleurs> Fleurss = new ArrayList<Fleurs>();
public static ArrayList<Arbre> Arbres = new ArrayList<Arbre>();
public static ArrayList<Carre> Carres = new ArrayList<Carre>();
public static ArrayList<Triangle> Triangles = new ArrayList<Triangle>();
public static void main(String[] args) {
String filePath = "map.txt";
try {
// Création du flux bufférisé sur un FileReader, immédiatement suivi par un
// try/finally, ce qui permet de ne fermer le flux QUE s'il le reader
// est correctement instancié (évite les NullPointerException)
BufferedReader buff = new BufferedReader(new FileReader(filePath));
try {
String line;
// Lecture du fichier ligne par ligne. Cette boucle se termine
// quand la méthode retourne la valeur null.
int x = 0;
while ((line = buff.readLine()) != null) {
traiterLigne(line, x);
x++;
// faites ici votre traitement
}
} finally {
// dans tous les cas, on ferme nos flux
buff.close();
}
} catch (IOException ioe) {
// erreur de fermeture des flux
System.out.println("Erreur --" + ioe.toString());
}
for (int x = 0; x < EditeurJeu.length; x++) {
for (int y = 0; y < EditeurJeu[x].length; y++) {
if(EditeurJeu[x][y] != null){
System.out.println("En position "+x+"/"+y+" "+EditeurJeu[x][y]);
}
}
}
}
private static void traiterLigne(String line, int ligneIndex) {
char[] ligneAsTab = line.toCharArray();
for (int i = 0; i < ligneAsTab.length; i++) {
char charImg = ligneAsTab[i];
if (i >= EditeurJeu[ligneIndex].length) {
throw new RuntimeException("Ligne trop longue pour le jeu !");
}
switch (charImg) {
case '0':
EditeurJeu[ligneIndex][i] = "FLEUR";
Fleurs fleurs = new Fleurs(ligneIndex * 25, i * 25);
Fleurss.add(fleurs);
case '1':
EditeurJeu[ligneIndex][i] = "ARBRE";
Arbre arbre = new Arbre(ligneIndex * 25, i * 25);
Arbres.add(arbre);
case '2':
EditeurJeu[ligneIndex][i] = "CARRE";
Carre carre = new Carre(ligneIndex * 25, i * 25);
Carres.add(carre);
break;
case '3':
EditeurJeu[ligneIndex][i] = "TRIANGLE";
Triangle triangle = new Triangle(ligneIndex * 25, i * 25);
Triangles.add(triangle);
break;
default:
break;
}
}
}
} |
Partager