
Comment créer des labyrinthes sans chemins avec Java?
Nous allons mettre en pratique les leçons précédentes :
Créez une matrice bistable ou tristable, c'est-à-dire avec 2 ou 3 valeurs aléatoires. Créez la classe graphique qui dessine une ligne horizontale, verticale ou diagonale en fonction de l'état de l'élément ij-ème dans la matrice…
Voici le code :
Principal :
package maze;
import javax.swing.*;
public class Maze {
public static void main(String[] args) {
JFrame t= new JFrame(“Maze”);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tela p = new Tela();
t.add(p);
t.setSize(800, 1000);
t.setVisible(true);
}
}
package labirinto;
import javax.swing.*;
public class Labirinto {
public static void main(String[] args) {
JFrame t= new JFrame(“Labirinto”);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Tela p = new Tela();
p.Riempire();
t.add(p);
t.setSize(800, 1000);
t.setVisible(true);
}
}
Objet fichier TELA :
package maze;
import java.awt.*;;
import javax.swing.*;
public class Tela extends JPanel{
public int M[][]=new int [100][100];
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.black);
// g.drawRect(10,10,700,700);
g.setColor(Color.black);
M=inizializzaMatriceRandom(M,4);
int x=0,y=0;
int px=10;
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
x=x+px;
if(M[i][j]==0) { g.drawLine(x, y, x, y+10); //g.fillRect(10+20*j,10+20*i, 2, 20); } if(M[i][j]==1) { g.drawLine(x, y, x+10, y); //g.fillRect(10+20*j,30+20*i, 20, 2); } if(M[i][j]==2) { g.drawLine(x, y, x+10, y+10); // g.drawArc(x, y, 15, 15, 90, -90); //g.fillRect(10+20*j,30+20*i, 20, 2); } if(M[i][j]==3) { g.drawLine(x, y+10, x+10, y); // g.drawArc(x, y, 15, 15, 90, 90); //g.fillRect(10+20*j,30+20*i, 20, 2); } } x=0; y=y+px; } }
public int[][] inizializzaMatriceRandom(int m[][],int numerostati)
{ for(int i=0;i<m.length;i++){
for(int j=0;j<m.length;j++){
m[i][j]=(int)(Math.random()*numerostati);
System.out.print(” “+ m[i][j]);
}
System.out.println();
}
return m;
} }

Voici la version JavaScript du code