










Main
package aspiradora;
public class Main {
public static void main(String[] args) {
areaLimpiar interfaz = new areaLimpiar();
interfaz.setVisible(true);
}
}
Clase AgenteSimple
package aspiradora;
public class AgenteSimple {
int percepcionActual;
int accion;
AgenteSimple()
{ }
void recibePercepcion(int percep)
{
percepcionActual = percep;
}
int escogeLaMejorAccion(int percepcionActual)
{
if(percepcionActual == 1)
{
return 7;
}
if(percepcionActual == 2)
{
return 8;
}
if(percepcionActual == 3)
{
return 9;
}
if(percepcionActual == 4)
{
return 10;
}
if(percepcionActual == 5)
{
return 11;
}
if(percepcionActual == 6)
{
return 12;
}
return 0;
}
String interpretaAccion (int accion)
{
if(accion == 7)
{
return("Percibí que hay pared a la derecha de mi y está limpio, por lo tanto camino hacia la izquierda");
}
if(accion == 8)
{
return("Percibí que hay pared a la izquierda de mi y está limpio, por lo tanto camino hacia la derecha");
}
if(accion == 9)
{
return("Percibí que hay basura, por lo tanto aspiraré");
}
if(accion == 10)
{
return("Percibí papeles, por lo tanto aspiraré");
}
if(accion == 11)
{
return("Percibí agua, por lo tanto aspiraré");
}
if(accion == 12)
{
return("Percibí que posiblemente está limpio, por lo observaré mi entorno para moverme o rectificar si aspiro");
}
return ("No sé que hacer, por lo tanto me detengo");
}
}
Interfaz
package aspiradora;
import java.awt.Color;
public class areaLimpiar extends javax.swing.JFrame {
public areaLimpiar() {
initComponents();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Agente-(Aspiradora)");
jButton1.setText("Opciones!!!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setEnabled(false);
jLabel1.setText("Percepción y acción a realizar:");
jLabel4.setText("Posición Actual");
jLabel5.setText("Posición Anterior");
jTextArea1.setBackground(java.awt.Color.pink);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextArea2.setBackground(java.awt.Color.yellow);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspiradora/aspiradora.jpg"))); // NOI18N
jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(78, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(165, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(127, 127, 127)
.addComponent(jLabel5)
.addGap(134, 134, 134))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(432, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(189, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(187, 187, 187))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addContainerGap())
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int area;
int random = (int) (Math.random() * 6);
area=random+1;
jTextField1.setText(""+area);
AgenteSimple ag1 = new AgenteSimple();
int percep = Integer.parseInt(jTextField1.getText());
ag1.recibePercepcion( percep);
int accion = ag1.escogeLaMejorAccion(ag1.percepcionActual);
String accionInterpretada = ag1.interpretaAccion(accion);
jTextField2.setText(accionInterpretada);
if(area==1){
jTextArea2.setBackground(Color.yellow);
jTextArea1.setBackground(Color.pink);
jLabel4.setText("Posición Actual");
jLabel5.setText("Posición Anterior");
}
if(area==2){
jTextArea1.setBackground(Color.yellow);
jTextArea2.setBackground(Color.pink);
jLabel4.setText("Posición Anterior");
jLabel5.setText("Posición Actual");
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new areaLimpiar().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
No hay comentarios:
Publicar un comentario