/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package LaberintoGrafico;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package LaberintoGrafico;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
*
* @author Yito
*/
public class Maze extends javax.swing.JFrame {
/**
* Creates new form Maze
*/
public Maze() {
initComponents();
}
public int [] [] Maze;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-
BEGIN:initComponents
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
Generate = new javax.swing.JButton();
Start = new javax.swing.JButton();
Settings = new javax.swing.JButton();
jSeparator1 = new javax.swing.JSeparator();
Automatic = new javax.swing.JRadioButton();
Manual = new javax.swing.JRadioButton();
Player = new javax.swing.JLabel();
panel = new javax.swing.JPanel();
Exit = new javax.swing.JButton();
Name = new javax.swing.JLabel();
Rowssize = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
Columnssize = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
Erase = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Maze");
setBackground(new java.awt.Color(0, 0, 255));
Generate.setText("Generate");
Generate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
GenerateActionPerformed(evt);
}
});
Start.setText("Start");
Start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StartActionPerformed(evt);
}
});
Settings.setText("Settings");
Settings.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SettingsActionPerformed(evt);
}
});
buttonGroup1.add(Automatic);
Automatic.setText("Automatic");
buttonGroup1.add(Manual);
Manual.setText("Manual");
Player.setText("Player:");
panel.setBackground(new java.awt.Color(0, 204, 153));
javax.swing.GroupLayout panelLayout = new
javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 407, Short.MAX_VALUE)
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 407, Short.MAX_VALUE)
);
Exit.setText("Exit");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
Name.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
Name.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
Rowssize.setHorizontalAlignment(javax.swing.JTextField.CENTER);
Rowssize.setText("11");
Rowssize.setEnabled(false);
jLabel1.setText("x");
Columnssize.setHorizontalAlignment(javax.swing.JTextField.CENTER);
Columnssize.setText("11");
Columnssize.setEnabled(false);
jLabel2.setText("Maze size");
Erase.setText("Erase");
Erase.setToolTipText("");
Erase.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EraseActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Start)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Automatic)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Manual)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Erase,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Exit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Rowssize,
javax.swing.GroupLayout.PREFERRED_SIZE, 27,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Columnssize,
javax.swing.GroupLayout.PREFERRED_SIZE, 27,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14))
.addGroup(layout.createSequentialGroup()
.addComponent(Generate,
javax.swing.GroupLayout.PREFERRED_SIZE, 81,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Settings)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Player)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Name,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addGap(13, 13, 13))
.addGroup(layout.createSequentialGroup()
.addComponent(panel,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 8, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(Name,
javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.PREFERRED_SIZE, 23,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Generate,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(Settings,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(Player,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Erase)
.addComponent(Exit)
.addComponent(Rowssize,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(Columnssize,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Start)
.addComponent(Automatic)
.addComponent(Manual)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1,
javax.swing.GroupLayout.PREFERRED_SIZE, 11,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_ExitActionPerformed
// TODO add your handling code here:
Settings Close = new Settings();
Start Close1 = new Start();
Close1.dispose();
Close.dispose();
this.dispose();
}//GEN-LAST:event_ExitActionPerformed
private void SettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_SettingsActionPerformed
// TODO add your handling code here:
Settings Open = new Settings();
Open.setVisible(true);
this.setVisible(false);
}//GEN-LAST:event_SettingsActionPerformed
private void GenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_GenerateActionPerformed
// TODO add your handling code here:
int a, b;
int js = Integer.parseInt(Rowssize.getText());
b = js;
int jss = Integer.parseInt(Columnssize.getText());
a = jss;
int[][] lab = new int[a][b];
ArrayList posibles = new ArrayList();
for (int f = 1; f < lab.length - 1; f++) {
for (int c = 1; c < lab.length - 1; c++) {
if (f % 2 != 0 && c % 2 != 0) {
lab[f][c] = 3;
}
}
}
int pf = (int) (Math.random() * (lab.length - 2));
if (pf % 2 == 0) {
pf++;
}
int pc = (int) (Math.random() * (lab.length - 2));
if (pc % 2 == 0) {
pc++;
}
posibles.add(pf * 100 + pc);
lab[1] [0] = 1;
lab[lab.length - 2] [lab.length - 1] = 1;
while (posibles.size() > 0) {
ArrayList pUnos = new ArrayList();
int x = (int) posibles.remove((int) (Math.random() *
(posibles.size())));
pf = x / 100;
pc = x % 100;
lab[pf][pc] = 1;
if (pf - 2 >= 0 && lab[pf - 2][pc] == 3) {
lab[pf - 2][pc] = 2;
posibles.add((pf - 2) * 100 + pc);
} else {
if (pf - 2 >= 0 && lab[pf - 2][pc] == 1) {
pUnos.add((pf - 1) * 100 + pc);
}
}
if (pc + 2 < lab.length && lab[pf][pc + 2] == 3) {
lab[pf][pc + 2] = 2;
posibles.add(pf * 100 + (pc + 2));
} else {
if (pc + 2 < lab.length && lab[pf][pc + 2] == 1) {
pUnos.add(pf * 100 + (pc + 1));
}
}
if (pf + 2 < lab.length && lab[pf + 2][pc] == 3) {
lab[pf + 2][pc] = 2;
posibles.add((pf + 2) * 100 + pc);
} else {
if (pf + 2 < lab.length && lab[pf + 2][pc] == 1) {
pUnos.add((pf + 1) * 100 + pc);
}
}
if (pc - 2 >= 0 && lab[pf][pc - 2] == 3) {
lab[pf][pc - 2] = 2;
posibles.add(pf * 100 + (pc - 2));
} else {
if (pc - 2 >= 0 && lab[pf][pc - 2] == 1) {
pUnos.add(pf * 100 + (pc - 1));
}
}
if (pUnos.size() > 0) {
int m = (int) pUnos.remove((int) (Math.random() *
(pUnos.size())));
pf = m / 100;
pc = m % 100;
lab[pf][pc] = 1;
}
}
int r = Integer.parseInt(Rowssize.getText());
int c = Integer.parseInt(Columnssize.getText());
int x = panel.getWidth();
int y = panel.getHeight();
int w = x/r;
int h = y/c;
for (int i = 0; i < lab.length; i++) {
for(int j = 0; j < lab.length; j++){
if(lab[j][i] == 0) {
if (r != 11 && c != 11) {
Dibujo.dibujo(panel.getGraphics(), i*w, j*h, w, h);
}else{
Dibujo.dibujo(panel.getGraphics(), i*w, j*h, w, h);
}
}
}
}
Maze = lab;
}//GEN-LAST:event_GenerateActionPerformed
private void EraseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_EraseActionPerformed
// TODO add your handling code here:
panel.removeAll();
panel.updateUI();
}//GEN-LAST:event_EraseActionPerformed
private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_StartActionPerformed
// TODO add your handling code here:
if(Automatic.isSelected() == true){
int [] [] lab = Maze;
Queue miCola = new LinkedList();
int f = 1;
int c = 0;
lab[1] [0] = 1;
while (true) {
if (f - 1 >= 0 && lab[f - 1][c] == 1) {
lab[f - 1][c] = lab[f][c] + 1;
miCola.offer((f - 1) * 100 + c);
}
if (c + 1 < lab.length && lab[f][c + 1] == 1) {
lab[f][c + 1] = lab[f][c] + 1;
miCola.offer(f * 100 + (c + 1));
}
if (f + 1 < lab.length && lab[f + 1][c] == 1) {
lab[f + 1][c] = lab[f][c] + 1;
miCola.offer((f + 1) * 100 + c);
}
if (c - 1 >= 0 && lab[f][c - 1] == 1) {
lab[f][c - 1] = lab[f][c] + 1;
miCola.offer(f * 100 + (c - 1));
}
int num = (int) miCola.poll();
f = num / 100;
c = num % 100;
if(f == lab.length - 2 && c == lab.length - 1){
break;
}
}
Stack pila = new Stack();
Stack pila2 = new Stack();
int x = lab [f] [c];
while(true){
//se mueve hacia arriba
if (f - 1 >= 0 && lab[f - 1][c] == (x - 1)){
lab[f - 1][c] = lab[f][c] + 1;
pila.push((f - 1) * 100 + c);
pila2.push((f - 1) * 100 + c);
}
//se mueve hacia derecha
if (c + 1 < lab.length && lab[f][c + 1] == (x - 1)) {
lab[f][c + 1] = lab[f][c] + 1;
pila.push(f * 100 + (c + 1));
pila2.push(f * 100 + (c + 1));
}
//se mueve hacia abajo
if (f + 1 < lab.length && lab[f + 1][c] == (x - 1)) {
lab[f + 1][c] = lab[f][c] + 1;
pila.push((f + 1) * 100 + c);
pila2.push(f * 100 + (c + 1));
}
//se mueve hacia izquierda
if (c - 1 >= 0 && lab[f][c - 1] == (x - 1)) {
lab[f][c - 1] = lab[f][c] + 1;
pila.push(f * 100 + (c - 1));
pila2.push(f * 100 + (c - 1));
}
int num = (int) pila.pop();
f = num / 100;
c = num % 100;
if(f == 1 && c == 1){
break;
}
x = x - 1;
}
int ll = 100;
pila2.push(ll);
int r = Integer.parseInt(Rowssize.getText());
int cc = Integer.parseInt(Columnssize.getText());
int xx = panel.getWidth();
int y = panel.getHeight();
int w = xx/r;
int h = y/cc;
while(true){
if(pila2 != null){
int num = (int) pila2.pop();
f = num / 100;
c = num % 100;
if(f == 9 && c == 10){
Dibujo.camino(panel.getGraphics(), c*w, f*h, w, h);
break;
}
Dibujo.camino(panel.getGraphics(), c*w, f*h, w, h);
}
}
}else if(Manual.isSelected() == true){
System.out.println("bye");
}
}//GEN-LAST:event_StartActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Maze().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JRadioButton Automatic;
public static javax.swing.JTextField Columnssize;
private javax.swing.JButton Erase;
private javax.swing.JButton Exit;
private javax.swing.JButton Generate;
private javax.swing.JRadioButton Manual;
public static javax.swing.JLabel Name;
private javax.swing.JLabel Player;
public static javax.swing.JTextField Rowssize;
private javax.swing.JButton Settings;
private javax.swing.JButton Start;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JSeparator jSeparator1;
public static javax.swing.JPanel panel;
// End of variables declaration//GEN-END:variables
}

Maze

  • 1.
    /* * To changethis license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package LaberintoGrafico; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package LaberintoGrafico; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /** * * @author Yito */ public class Maze extends javax.swing.JFrame { /** * Creates new form Maze */ public Maze() { initComponents(); } public int [] [] Maze; /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN- BEGIN:initComponents private void initComponents() { buttonGroup1 = new javax.swing.ButtonGroup(); Generate = new javax.swing.JButton(); Start = new javax.swing.JButton(); Settings = new javax.swing.JButton(); jSeparator1 = new javax.swing.JSeparator(); Automatic = new javax.swing.JRadioButton(); Manual = new javax.swing.JRadioButton(); Player = new javax.swing.JLabel(); panel = new javax.swing.JPanel(); Exit = new javax.swing.JButton(); Name = new javax.swing.JLabel(); Rowssize = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); Columnssize = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); Erase = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Maze");
  • 2.
    setBackground(new java.awt.Color(0, 0,255)); Generate.setText("Generate"); Generate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { GenerateActionPerformed(evt); } }); Start.setText("Start"); Start.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { StartActionPerformed(evt); } }); Settings.setText("Settings"); Settings.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SettingsActionPerformed(evt); } }); buttonGroup1.add(Automatic); Automatic.setText("Automatic"); buttonGroup1.add(Manual); Manual.setText("Manual"); Player.setText("Player:"); panel.setBackground(new java.awt.Color(0, 204, 153)); javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel); panel.setLayout(panelLayout); panelLayout.setHorizontalGroup( panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 407, Short.MAX_VALUE) ); panelLayout.setVerticalGroup( panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 407, Short.MAX_VALUE) ); Exit.setText("Exit"); Exit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ExitActionPerformed(evt); } }); Name.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); Name.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT); Rowssize.setHorizontalAlignment(javax.swing.JTextField.CENTER); Rowssize.setText("11"); Rowssize.setEnabled(false); jLabel1.setText("x"); Columnssize.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  • 3.
    Columnssize.setText("11"); Columnssize.setEnabled(false); jLabel2.setText("Maze size"); Erase.setText("Erase"); Erase.setToolTipText(""); Erase.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt) { EraseActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSeparator1) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(Start) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Automatic) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Manual) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Erase, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(Exit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(Rowssize, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Columnssize, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(14, 14, 14)) .addGroup(layout.createSequentialGroup() .addComponent(Generate, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Settings) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(Player)
  • 4.
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Name, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2) .addGap(13, 13,13)) .addGroup(layout.createSequentialGroup() .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 8, Short.MAX_VALUE)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(Name, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Generate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(Settings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(Player, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Erase) .addComponent(Exit) .addComponent(Rowssize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(Columnssize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Start) .addComponent(Automatic) .addComponent(Manual))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
  • 5.
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }//</editor-fold>//GEN-END:initComponents private void ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_ExitActionPerformed // TODO add your handling code here: Settings Close = new Settings(); Start Close1 = new Start(); Close1.dispose(); Close.dispose(); this.dispose(); }//GEN-LAST:event_ExitActionPerformed private void SettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_SettingsActionPerformed // TODO add your handling code here: Settings Open = new Settings(); Open.setVisible(true); this.setVisible(false); }//GEN-LAST:event_SettingsActionPerformed private void GenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_GenerateActionPerformed // TODO add your handling code here: int a, b; int js = Integer.parseInt(Rowssize.getText()); b = js; int jss = Integer.parseInt(Columnssize.getText()); a = jss; int[][] lab = new int[a][b]; ArrayList posibles = new ArrayList(); for (int f = 1; f < lab.length - 1; f++) { for (int c = 1; c < lab.length - 1; c++) { if (f % 2 != 0 && c % 2 != 0) { lab[f][c] = 3; } } } int pf = (int) (Math.random() * (lab.length - 2)); if (pf % 2 == 0) { pf++; } int pc = (int) (Math.random() * (lab.length - 2)); if (pc % 2 == 0) { pc++; } posibles.add(pf * 100 + pc); lab[1] [0] = 1; lab[lab.length - 2] [lab.length - 1] = 1;
  • 6.
    while (posibles.size() >0) { ArrayList pUnos = new ArrayList(); int x = (int) posibles.remove((int) (Math.random() * (posibles.size()))); pf = x / 100; pc = x % 100; lab[pf][pc] = 1; if (pf - 2 >= 0 && lab[pf - 2][pc] == 3) { lab[pf - 2][pc] = 2; posibles.add((pf - 2) * 100 + pc); } else { if (pf - 2 >= 0 && lab[pf - 2][pc] == 1) { pUnos.add((pf - 1) * 100 + pc); } } if (pc + 2 < lab.length && lab[pf][pc + 2] == 3) { lab[pf][pc + 2] = 2; posibles.add(pf * 100 + (pc + 2)); } else { if (pc + 2 < lab.length && lab[pf][pc + 2] == 1) { pUnos.add(pf * 100 + (pc + 1)); } } if (pf + 2 < lab.length && lab[pf + 2][pc] == 3) { lab[pf + 2][pc] = 2; posibles.add((pf + 2) * 100 + pc); } else { if (pf + 2 < lab.length && lab[pf + 2][pc] == 1) { pUnos.add((pf + 1) * 100 + pc); } } if (pc - 2 >= 0 && lab[pf][pc - 2] == 3) { lab[pf][pc - 2] = 2; posibles.add(pf * 100 + (pc - 2)); } else { if (pc - 2 >= 0 && lab[pf][pc - 2] == 1) { pUnos.add(pf * 100 + (pc - 1)); } } if (pUnos.size() > 0) { int m = (int) pUnos.remove((int) (Math.random() * (pUnos.size()))); pf = m / 100; pc = m % 100; lab[pf][pc] = 1; } } int r = Integer.parseInt(Rowssize.getText()); int c = Integer.parseInt(Columnssize.getText()); int x = panel.getWidth(); int y = panel.getHeight(); int w = x/r; int h = y/c; for (int i = 0; i < lab.length; i++) {
  • 7.
    for(int j =0; j < lab.length; j++){ if(lab[j][i] == 0) { if (r != 11 && c != 11) { Dibujo.dibujo(panel.getGraphics(), i*w, j*h, w, h); }else{ Dibujo.dibujo(panel.getGraphics(), i*w, j*h, w, h); } } } } Maze = lab; }//GEN-LAST:event_GenerateActionPerformed private void EraseActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_EraseActionPerformed // TODO add your handling code here: panel.removeAll(); panel.updateUI(); }//GEN-LAST:event_EraseActionPerformed private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_StartActionPerformed // TODO add your handling code here: if(Automatic.isSelected() == true){ int [] [] lab = Maze; Queue miCola = new LinkedList(); int f = 1; int c = 0; lab[1] [0] = 1; while (true) { if (f - 1 >= 0 && lab[f - 1][c] == 1) { lab[f - 1][c] = lab[f][c] + 1; miCola.offer((f - 1) * 100 + c); } if (c + 1 < lab.length && lab[f][c + 1] == 1) { lab[f][c + 1] = lab[f][c] + 1; miCola.offer(f * 100 + (c + 1)); } if (f + 1 < lab.length && lab[f + 1][c] == 1) { lab[f + 1][c] = lab[f][c] + 1; miCola.offer((f + 1) * 100 + c); } if (c - 1 >= 0 && lab[f][c - 1] == 1) { lab[f][c - 1] = lab[f][c] + 1; miCola.offer(f * 100 + (c - 1)); } int num = (int) miCola.poll(); f = num / 100; c = num % 100; if(f == lab.length - 2 && c == lab.length - 1){ break; } }
  • 8.
    Stack pila =new Stack(); Stack pila2 = new Stack(); int x = lab [f] [c]; while(true){ //se mueve hacia arriba if (f - 1 >= 0 && lab[f - 1][c] == (x - 1)){ lab[f - 1][c] = lab[f][c] + 1; pila.push((f - 1) * 100 + c); pila2.push((f - 1) * 100 + c); } //se mueve hacia derecha if (c + 1 < lab.length && lab[f][c + 1] == (x - 1)) { lab[f][c + 1] = lab[f][c] + 1; pila.push(f * 100 + (c + 1)); pila2.push(f * 100 + (c + 1)); } //se mueve hacia abajo if (f + 1 < lab.length && lab[f + 1][c] == (x - 1)) { lab[f + 1][c] = lab[f][c] + 1; pila.push((f + 1) * 100 + c); pila2.push(f * 100 + (c + 1)); } //se mueve hacia izquierda if (c - 1 >= 0 && lab[f][c - 1] == (x - 1)) { lab[f][c - 1] = lab[f][c] + 1; pila.push(f * 100 + (c - 1)); pila2.push(f * 100 + (c - 1)); } int num = (int) pila.pop(); f = num / 100; c = num % 100; if(f == 1 && c == 1){ break; } x = x - 1; } int ll = 100; pila2.push(ll); int r = Integer.parseInt(Rowssize.getText()); int cc = Integer.parseInt(Columnssize.getText()); int xx = panel.getWidth(); int y = panel.getHeight(); int w = xx/r; int h = y/cc; while(true){ if(pila2 != null){ int num = (int) pila2.pop(); f = num / 100; c = num % 100; if(f == 9 && c == 10){ Dibujo.camino(panel.getGraphics(), c*w, f*h, w, h); break; } Dibujo.camino(panel.getGraphics(), c*w, f*h, w, h); }
  • 9.
    } }else if(Manual.isSelected() ==true){ System.out.println("bye"); } }//GEN-LAST:event_StartActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Maze.class.getName()).log(java.util.logging.L evel.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Maze().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JRadioButton Automatic; public static javax.swing.JTextField Columnssize; private javax.swing.JButton Erase; private javax.swing.JButton Exit; private javax.swing.JButton Generate; private javax.swing.JRadioButton Manual; public static javax.swing.JLabel Name; private javax.swing.JLabel Player; public static javax.swing.JTextField Rowssize; private javax.swing.JButton Settings;
  • 10.
    private javax.swing.JButton Start; privatejavax.swing.ButtonGroup buttonGroup1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JSeparator jSeparator1; public static javax.swing.JPanel panel; // End of variables declaration//GEN-END:variables }