Created
June 3, 2016 22:30
-
-
Save roramirez/5933f1c486f6a71442d8ac84d09e0f85 to your computer and use it in GitHub Desktop.
Agente emula una Aspiradora
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************************** | |
* | |
* Autor : Rodrigo Ramírez N. | |
* e-mail : [email protected] | |
* Descripcion : Emula un agente aspiradora | |
* con las persepciones de avanzar, | |
* doblar, presencia de obstaculo, | |
* muro, recordar lugar por donde | |
* a pasado y haber limpiado todo. | |
* | |
* Version : 1.1 | |
* Fecha : 2005/04/29 | |
* Nombre : Tuiqui | |
* | |
* | |
* | |
*--------------------------------------------------------- | |
* | |
* Copyright Rodrigo Ramírez (c) 2005 | |
* | |
* Este programa es software libre. Usted lo puede | |
* modificar, distribuir y/o vender bajos los | |
* terminos de la licencia GPL General Public Licence | |
* de GNU publicada por la FSF Free Software Foundation. | |
* | |
* | |
*********************************************************/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <strings.h> | |
//Define estados | |
#define LIMPIO 0 | |
#define ASPIRADO 1 | |
#define BASURA 2 | |
#define OBSTACULO 3 | |
#define IZQ 1 | |
#define DER 2 | |
#define ABA 3 | |
#define ARR 4 | |
#define MAX 10 //DIMENSION MAXIMA DEL ESPACIO | |
int espacio [MAX][MAX]; | |
int espacio_euristico [MAX][MAX]; | |
int I=0, J=0; //POSICION DE ASPIRADORA | |
int ANCHO, LARGO; //maximo de espacio | |
int N_BASURA=0; | |
int VUELTAS=0; | |
int PASO=1; | |
int LADO; | |
//PROGRAMA PRINCIPAL | |
int main(){ | |
int n=0; | |
//Pide largo | |
while (1!=n){ | |
printf("\n Ingrese largo: "); | |
scanf ("%i", &LARGO); | |
if (LARGO>1 && MAX>=LARGO){ | |
n=1; | |
} | |
} | |
n=0; | |
//Pide ancho | |
while (1!=n){ | |
printf("\n Ingrese Ancho: "); | |
scanf ("%i", &ANCHO); | |
if (ANCHO>1 && MAX>=ANCHO){ | |
n=1; | |
} | |
} | |
INICIALIZA_ESPACIO(); | |
LLENA_OBSTACULOS(); | |
LLENA_BASURAS(); | |
N_BASURA=CUENTA_BASURA(); | |
IMPRIME_ESPACIO(); | |
LADO=ABA; | |
BAJA(); | |
} | |
//Dobla Izquierda | |
int IZQUIERDA(){ | |
LADO=IZQ; | |
PAUSA(); | |
if (N_BASURA!=0){ | |
if (J+1 < ANCHO && espacio[I][J+1]!=OBSTACULO){ | |
if (espacio_euristico[I][J+1]!=PASO){ | |
J++; | |
printf("\n avanza a %i , %i", I+1, J+1); | |
VUELTAS=0; //setea vueltas | |
if (espacio[I][J]==BASURA){ | |
printf("\n Limpia Basura"); | |
espacio[I][J]=LIMPIO; | |
N_BASURA--; | |
} | |
espacio_euristico[I][J]=PASO; //Anota que paso por ese lugar | |
IZQUIERDA(); | |
}else{ | |
if (VUELTAS > 2){ | |
J++; | |
printf("\n avanza a %i , %i", I+1, J+1); | |
VUELTAS=0; //setea vueltas | |
IZQUIERDA(); | |
}else{ | |
VUELTAS++; | |
printf("\n Sube"); | |
ARRIBA(); | |
} | |
} | |
}else{ | |
VUELTAS++; | |
printf("\n Sube"); | |
ARRIBA(); | |
} | |
}else{ | |
printf ("\n TERMINO ==================\n"); | |
} | |
} | |
//Dobla derecha | |
int DERECHA(){ | |
LADO=DER; | |
PAUSA(); | |
if (N_BASURA!=0){ | |
if (J-1>=0 && espacio[I][J-1]!=OBSTACULO){ | |
if (espacio_euristico[I][J-1]!=PASO){ | |
J--; | |
printf("\n avanza a %i , %i", I+1, J+1); | |
VUELTAS=0; //setea vueltas | |
if (espacio[I][J]==BASURA){ | |
printf("\n Limpia Basura"); | |
espacio[I][J]=LIMPIO; | |
N_BASURA--; | |
} | |
espacio_euristico[I][J]=PASO; //Anota que paso por ese lugar | |
DERECHA(); | |
}else{ | |
if (VUELTAS > 2){ | |
J--; | |
printf("\n avanza a %i , %i", I+1, J+1); | |
VUELTAS=0; //setea vueltas | |
DERECHA(); | |
}else{ | |
VUELTAS++; | |
printf("\n Baja"); | |
BAJA(); | |
} | |
} | |
}else{ | |
VUELTAS++; | |
printf("\n Baja"); | |
BAJA(); | |
} | |
}else{ | |
printf ("\n TERMINO ==================\n"); | |
} | |
} | |
//ARRIBA | |
int ARRIBA(){ | |
LADO=ARR; | |
PAUSA(); | |
if (N_BASURA!=0){ | |
if (I-1 >=0 && espacio[I-1][J]!=OBSTACULO){ | |
if (espacio_euristico[I-1][J]!=PASO){ | |
I--; | |
printf("\n avanza a %i , %i", I+1, J+1); | |
VUELTAS=0; //setea vueltas | |
if (espacio[I][J]==BASURA){ | |
printf("\n Limpia Basura"); | |
espacio[I][J]=LIMPIO; | |
N_BASURA--; | |
} | |
espacio_euristico[I][J]=PASO; //Anota que paso por ese lugar | |
ARRIBA(); | |
}else{ | |
if (VUELTAS > 2){ | |
I--; | |
printf("\n avanza a %i , %i", I, J); | |
ARRIBA(); | |
VUELTAS=0; //setea vueltas | |
}else{ | |
VUELTAS++; | |
printf("\n Doblo a derecha"); | |
DERECHA(); | |
} | |
} | |
}else{ | |
VUELTAS++; | |
printf("\n Doblo a derecha"); | |
DERECHA(); | |
} | |
}else{ | |
printf ("\n TERMINO ==================\n"); | |
} | |
} | |
//BAJA | |
int BAJA(){ | |
LADO=ABA; | |
PAUSA(); | |
if (N_BASURA!=0){ | |
if (I+1 < LARGO && espacio[I+1][J]!=OBSTACULO){ | |
if (espacio_euristico[I+1][J]!=PASO){ | |
I++; | |
printf("\n avanza a %i , %i", I+1, J+1); | |
VUELTAS=0; //setea vueltas | |
if (espacio[I][J]==BASURA){ | |
printf("\n Limpia Basura"); | |
espacio[I][J]=LIMPIO; | |
N_BASURA--; | |
} | |
espacio_euristico[I][J]=PASO; //Anota que paso por ese lugar | |
BAJA(); | |
}else{ | |
if (VUELTAS > 2){ | |
I++; | |
printf("\n avanza a %i , %i", I, J); | |
BAJA(); | |
VUELTAS=0; //setea vueltas | |
}else{ | |
VUELTAS++; | |
printf("\n Doblo a izquierda"); | |
IZQUIERDA(); | |
} | |
} | |
}else{ | |
VUELTAS++; | |
printf("\n Doblo a izquierda"); | |
IZQUIERDA(); | |
} | |
}else{ | |
printf ("\n TERMINO ================== \n"); | |
} | |
} | |
int IMPRIME_ESPACIO(){ | |
int i=0,j=0; | |
char LUGAR; | |
for (i=0; i<LARGO; i++){ | |
for (j=0; j<ANCHO; j++){ | |
if (i==I && j==J){ | |
switch (LADO){ | |
case ARR: | |
LUGAR='^'; | |
break; | |
case ABA: | |
LUGAR='V'; | |
break; | |
case DER: | |
LUGAR='<'; | |
break; | |
case IZQ: | |
LUGAR='>'; | |
break; | |
} | |
}else{ | |
switch (espacio[i][j]){ | |
case LIMPIO: | |
LUGAR = 'L'; | |
break; | |
case ASPIRADO: | |
LUGAR = 'A'; | |
break; | |
case BASURA: | |
LUGAR = 'B'; | |
break; | |
case OBSTACULO: | |
LUGAR = 'O'; | |
break; | |
} | |
} | |
printf("\t %c",LUGAR); | |
} | |
printf ("\n "); | |
} | |
} | |
int INICIALIZA_ESPACIO(){ | |
int i=0,j=0; | |
char LUGAR; | |
for (i=0; i<LARGO; i++){ | |
for (j=0; j<ANCHO; j++){ | |
espacio[i][j]=LIMPIO; //Inicializa espacio | |
espacio_euristico[i][j]=0; | |
} | |
} | |
} | |
int CUENTA_BASURA(){ | |
int i=0,j=0, n=0; | |
for (i=0; i<LARGO; i++){ | |
for (j=0; j<ANCHO; j++){ | |
if (espacio[i][j]==BASURA){ | |
n=n+1; | |
} | |
} | |
} | |
return n; | |
} | |
int LLENA_TODO_BASURA(){ | |
int i=0,j=0; | |
for (i=0; i<LARGO; i++){ | |
for (j=0; j<ANCHO; j++){ | |
if (espacio[i][j]==LIMPIO){ | |
espacio[i][j]=BASURA; | |
} | |
} | |
} | |
} | |
int LLENA_MANUAL_BASURA(){ | |
int n=0,X, Y ; | |
IMPRIME_ESPACIO_EJEMPLO(); | |
printf("\n Para terminar de ingresar basura presine 99"); | |
while (n!=99){ | |
printf ("\n INGRESE CORDENADA (X,Y)"); | |
printf("\n Ingrese eje X :"); | |
scanf("%i", &X); | |
if (X!=99){ | |
printf("\n Ingrese eje Y :"); | |
scanf("%i", &Y); | |
if (Y!=99){ | |
if ( X+Y> 2 && X<=LARGO && Y<=ANCHO ){ | |
if (espacio[X-1][Y-1]!=OBSTACULO){ | |
espacio[X-1][Y-1]=BASURA; | |
}else{ | |
printf("\n Corresponde a obstaculo:"); | |
} | |
}else{ | |
if (X!=99 && Y!=99){ | |
printf("\nLA cordenada no es valida"); | |
printf("\n debe ser mayor a 1,1 y Menor o igual %i , %i", LARGO, ANCHO); | |
}else{ | |
n=99; | |
} | |
} | |
}else{ | |
n=99; | |
} | |
}else{ | |
n=99; | |
} | |
} | |
} | |
int LLENA_BASURAS(){ | |
int opcion=0; | |
while (opcion==0){ | |
printf("\n INGRESO DE BASURA"); | |
printf("\n 1.- LLENA TODO EL ESPACIO CON BASURA"); | |
printf("\n 2.- LLENA MANUAL \n Opcion : "); | |
scanf("%i", &opcion); | |
} | |
switch (opcion){ | |
case 1: | |
LLENA_TODO_BASURA(); | |
break; | |
case 2: | |
LLENA_MANUAL_BASURA(); | |
break; | |
} | |
} | |
int LLENA_OBSTACULOS (){ | |
int n=0,X, Y ; | |
IMPRIME_ESPACIO_EJEMPLO(); | |
printf("\n Para terminar de ingresar obstaculos presine 99"); | |
while (n!=99){ | |
printf ("\n INGRESE CORDENADA (X,Y)"); | |
printf("\n Ingrese eje X :"); | |
scanf("%i", &X); | |
if (X!=99){ | |
printf("\n Ingrese eje Y :"); | |
scanf("%i", &Y); | |
if (Y!=99){ | |
if ( X+Y> 2 && X<=LARGO && Y<=ANCHO ){ | |
espacio[X-1][Y-1]=OBSTACULO; | |
}else{ | |
if (X!=99 && Y!=99){ | |
printf("\nLA cordenada no es valida"); | |
printf("\n debe ser mayor a 1,1 y Menor o igual %i , %i", ANCHO, LARGO); | |
}else{ | |
n=99; | |
} | |
} | |
}else{ | |
n=99; | |
} | |
}else{ | |
n=99; | |
} | |
} | |
} | |
int IMPRIME_ESPACIO_EJEMPLO(){ | |
int i=0,j=0; | |
for (i=0; i<LARGO; i++){ | |
for (j=0; j<ANCHO; j++){ | |
printf("\t (%i,%i) ", i+1, j+1); | |
if (espacio[i][j]==OBSTACULO){ | |
printf("OBST "); | |
}else{ | |
printf(" "); | |
} | |
} | |
printf ("\n "); | |
} | |
} | |
int TERMINO(){ | |
int c=0; | |
int i, j; | |
for (i=0; i<LARGO; i++){ | |
for (j=0; j<ANCHO; j++){ | |
if (espacio_euristico==0){ | |
c++; | |
} | |
} | |
} | |
if (c=0){ | |
return 1; | |
} | |
} | |
int PAUSA(){ | |
printf("\n"); | |
sleep(2); | |
IMPRIME_ESPACIO(); | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment