Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Problemas al cambiar variables globales por locales

Estas en el tema de Problemas al cambiar variables globales por locales en el foro de C/C++ en Foros del Web. Lo bueno es que el programa no tiene errores, pero oh sorpresa no tiene el mismo funcionamiento; lo he intentado reescribir ya 5 veces pero ...
  #1 (permalink)  
Antiguo 07/08/2011, 22:27
 
Fecha de Ingreso: mayo-2011
Mensajes: 3
Antigüedad: 12 años, 11 meses
Puntos: 0
Problemas al cambiar variables globales por locales

Lo bueno es que el programa no tiene errores, pero oh sorpresa no tiene el mismo funcionamiento; lo he intentado reescribir ya 5 veces pero no logro ver mi error.

Este es el código original, donde deseo quitar variables globales:

Código:
#include <stdio.h>

void inicializa_escenario();
int valida_posicion();
void mover ();
void vector_carac();
void imprimir();
char escenario[13][16],s1,s2,s3,s4,s5,s6,s7,s8;
int x,y,x1=0,x2=0,x3=0,x4=0;

main()
{
    int valida=0;
        
    inicializa_escenario();
    
     while(valida==0)
    {
    printf("Introduce tu posicion inicial: ");
    scanf("%d,%d",&y,&x);
    valida=valida_posicion(y,x);
    if(valida==0)printf("Posicion invalida, presione enter para introducir una nueva posicion\n");
    getchar();
    getchar();
    system("clear");
    }
    
    escenario[y][x]='O';
    printf("Tu Agente iniciara en la pos: %d,%d\n",y,x);
    imprimir(y,x);
    
   while(1)
    {
    system("clear"); 
    vector_carac();
    imprimir(y,x);
    printf("\nVector de Caracteristicas:       X1-> %d|X2-> %d|X3-> %d|X4-> %d\n",x1,x2,x3,x4);
    printf("Vector de Sensores:        S1-> %c  | S2-> %c | S3-> %c | S4-> %c | S5-> %c | S6-> %c | S7-> %c | S8-> %c\n",s1,s2,s3,s4,s5,s6,s7,s8);
    mover(y,x);
    sleep(1);
    }
    
   
}
void inicializa_escenario()
{
  int fil,col,i;
  
  for(fil=0;fil<13;fil++)
    {	      
      for(col=0;col<16;col++)
      {
	escenario[fil][col]=' ';
      }
    }
  
 for(fil=0;fil<13;fil++)
    {
            	
      for(col=0;col<16;col++)
      {
	if(fil==0)
	 if(col<12) escenario[fil][col]='*'; 	
	
	if(fil==4 || fil==8)	
	 if(col>11) escenario[fil][col]='*'; 	
	
	if(fil==10)	
	 if(col>5 && col<9) escenario[fil][col]='*';
	 
	 if(fil==12)
	   if(col<6 || (col>8 && col<12)) escenario[fil][col]='*'; 	
	 	
      }
      
    }
    
    for(col=0;col<16;col++)
    {
            	
      for(fil=0;fil<13;fil++)
      {
	if(col==0) escenario[fil][col]='*'; 	
	
	if(col==12)
	  if(fil<5 || fil>7) escenario[fil][col]='*'; 	
	  
	if(col==15)
	  if(fil>4 && fil<8) escenario[fil][col]='*'; 	
	  
	if(col==6 || col==8) 
	  if(fil>9) escenario[fil][col]='*'; 	
      }
      
    }
    
    for(i=3;i<9;i++)
    {
      escenario[5][i]='X';
      if(i<5 || i>6)
      {
	escenario[6][i]='X';
	escenario[7][i]='X';
      }
    }
    
}

int valida_posicion()
{
  int v=1;
  if(escenario[y][x]=='*' || escenario[y][x]=='X')v=0;
  if(y>11 || y<1) v=0;
  else
  {
  if(x==0) v=0;
    else
    {
     if(y<5 || y>7)
     {
      if(x>11)v=0;
      if(y>9)
      {
	if(x>5 && x<8) v=0;
      }
     }
     else
     {
      if(x==7)
      {
	if(y>9)v=0;
	
      }
      
     }
    }
  }

 
 return v;
 
}

void mover()
{
   x1=0,x2=0,x3=0,x4=0;
  
    vector_carac();

    if((x1==0 && x2==0) && (x3==0 && x4==0))
    {
	escenario[y][x]=' ';
	y=y-1;
	escenario[y][x]='O';    
    }
    
    else
    {

      if(x1==1 && x2==0)
      {	
	escenario[y][x]=' ';
	x=x-1;
	escenario[y][x]='O';		
      }
      else
      {
	if(x2==1 && x3==0)
	{	
	escenario[y][x]=' ';
	y=y+1;
	escenario[y][x]='O';		
	}
	else
	{
	  
	    if(x3==1 && x4==0)
	    {	
	      
	    escenario[y][x]=' ';
	    x=x+1;
	    escenario[y][x]='O';		
	    }
	    else
	    {
		if(x4==1 && x1==0)
		{	
		escenario[y][x]=' ';
		y=y-1;
		escenario[y][x]='O';		
		}
	    }
	}
	
      }
    }
}

void vector_carac()
{
  x1=0,x2=0,x3=0,x4=0;
  
  s1=escenario[y-1][x];
  s2=escenario[y-1][x-1];
  s3=escenario[y][x-1];
  s4=escenario[y+1][x-1];
  s5=escenario[y+1][x];
  s6=escenario[y+1][x+1];
  s7=escenario[y][x+1];
  s8=escenario[y-1][x+1];
  
  if(escenario[y-1][x-1]=='*' || escenario[y-1][x-1]=='X')x1=1;
      if(x1==0)
      {
	if(escenario[y-1][x]=='*' || escenario[y-1][x]=='X')x1=1;
      }
      
      if(escenario[y][x-1]=='*' || escenario[y][x-1]=='X')x2=1;
      if(x2==0)
      {
	if(escenario[y+1][x-1]=='*' || escenario[y+1][x-1]=='X')x2=1;
      }
      
      if(escenario[y+1][x]=='*' || escenario[y+1][x]=='X')x3=1;
      if(x3==0)
      {
	
	if(escenario[y+1][x+1]=='*' || escenario[y+1][x+1]=='X')x3=1;
	  
	
      }
      
      if(escenario[y][x+1]=='*' || escenario[y][x+1]=='X')x4=1;
      if(x4==0)
      {
	if(escenario[y-1][x+1]=='*' || escenario[y-1][x+1]=='X')x4=1;
      }
}

void imprimir()
{
  int fil,col;  

    for(fil=0;fil<13;fil++)
    {
      
      for(col=0;col<16;col++)
      {	
	printf("%c  ",escenario[fil][col]);	
      }
      printf("\n");
    }
    
}
Este fue mi ultimo intento:

Código:
#include <stdio.h>

void crea_Escenario();
void imprimir(int, int);
int valida_Posicion(int, int);
void vector_carac(int, int, char, char, char, char, char, char, char, char);
void mover (int, int, char, char, char, char, char, char, char, char);

char escenario[13][16];

int main (void)
{
	int valida, x, y;
	int x1=0,x2=0,x3=0,x4=0;
	char s1,s2,s3,s4,s5,s6,s7,s8;
	
	crea_Escenario();
	
	do
	{
		printf("Introduce tu posicion inicial: ");
		scanf("%d,%d",&y,&x);
		valida=valida_posicion(y,x);
		
		if(valida==0)
			printf("Posicion invalida, presione enter para introducir una nueva posicion\n");
		
		getchar();
		system("clear");
	
	}while(valida==0);
	
	escenario[y][x]='O';
	imprimir(y, x);
	
	   while(1)//Aqui vas
    {
    system("clear"); 
    vector_carac(y, x, s1, s2, s3, s4, s5, s6, s7, s8);
    imprimir(y,x);
    printf("\nVector de Caracteristicas: X1-> %d|X2-> %d|X3-> %d|X4-> %d\n",x1,x2,x3,x4);
    printf("Vector de Sensores:        S1-> %c|S2-> %c|S3-> %c|S4-> %c|S5-> %c|S6-> %c|S7-> %c|S8-> %c\n",s1,s2,s3,s4,s5,s6,s7,s8);
    mover(y, x, s1, s2, s3, s4, s5, s6, s7, s8);
    sleep(1);
    }
	 
	return 0;
}

void crea_Escenario()
{
	int fil,col,i;
  
  for(fil=0;fil<13;fil++)
    {	      
      for(col=0;col<16;col++)
      {
	escenario[fil][col]=' ';
      }
    }
  
 for(fil=0;fil<13;fil++)
    {
            	
      for(col=0;col<16;col++)
      {
	if(fil==0)
	 if(col<12) escenario[fil][col]='*'; 	
	
	if(fil==4 || fil==8)	
	 if(col>11) escenario[fil][col]='*'; 	
	
	if(fil==10)	
	 if(col>5 && col<9) escenario[fil][col]='*';
	 
	 if(fil==12)
	   if(col<6 || (col>8 && col<12)) escenario[fil][col]='*'; 	
	 	
      }
      
    }
    
    for(col=0;col<16;col++)
    {
            	
      for(fil=0;fil<13;fil++)
      {
	if(col==0) escenario[fil][col]='*'; 	
	
	if(col==12)
	  if(fil<5 || fil>7) escenario[fil][col]='*'; 	
	  
	if(col==15)
	  if(fil>4 && fil<8) escenario[fil][col]='*'; 	
	  
	if(col==6 || col==8) 
	  if(fil>9) escenario[fil][col]='*'; 	
      }
      
    }
    
    for(i=3;i<9;i++)
    {
      escenario[5][i]='X';
      if(i<5 || i>6)
      {
	escenario[6][i]='X';
	escenario[7][i]='X';
      }
    }
}

void imprimir(int y, int x)
{
  int fil,col;  

    for(fil=0;fil<13;fil++)
    {
      
      for(col=0;col<16;col++)
      {	
	printf("%c  ",escenario[fil][col]);	
      }
      printf("\n");
    }
    
}

int valida_posicion(int y, int x)
{
  int v=1;
  
  if(escenario[y][x]=='*' || escenario[y][x]=='X')
  v=0;
  
  if(y>11 || y<1)
  v=0;
  else
  {
  if(x==0) 
  v=0;
    else
    {
     if(y<5 || y>7)
     {
      if(x>11)v=0;
      if(y>9)
      {
	if(x>5 && x<8) v=0;
      }
     }
     else
     {
      if(x==7)
      {
	if(y>9)v=0;
	
      }
      
     }
    }
  }
}

void vector_carac(int y, int x, char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8)
{
  int x1=0,x2=0,x3=0,x4=0;
  
  s1=escenario[y-1][x];
  s2=escenario[y-1][x-1];
  s3=escenario[y][x-1];
  s4=escenario[y+1][x-1];
  s5=escenario[y+1][x];
  s6=escenario[y+1][x+1];
  s7=escenario[y][x+1];
  s8=escenario[y-1][x+1];
  
  if(escenario[y-1][x-1]=='*' || escenario[y-1][x-1]=='X')x1=1;
      if(x1==0)
      {
	if(escenario[y-1][x]=='*' || escenario[y-1][x]=='X')x1=1;
      }
      
      if(escenario[y][x-1]=='*' || escenario[y][x-1]=='X')x2=1;
      if(x2==0)
      {
	if(escenario[y+1][x-1]=='*' || escenario[y+1][x-1]=='X')x2=1;
      }
      
      if(escenario[y+1][x]=='*' || escenario[y+1][x]=='X')x3=1;
      if(x3==0)
      {
	
	if(escenario[y+1][x+1]=='*' || escenario[y+1][x+1]=='X')x3=1;
	  
	
      }
      
      if(escenario[y][x+1]=='*' || escenario[y][x+1]=='X')x4=1;
      if(x4==0)
      {
	if(escenario[y-1][x+1]=='*' || escenario[y-1][x+1]=='X')x4=1;
      }
}

void mover(int y, int x, char s1, char s2, char s3, char s4, char s5, char s6, char s7, char s8)
{
   int x1=0,x2=0,x3=0,x4=0;
  
    vector_carac(y, x, s1, s2, s3, s4, s5, s6, s7, s8);

    if((x1==0 && x2==0) && (x3==0 && x4==0))
    {
	escenario[y][x]=' ';
	y=y-1;
	escenario[y][x]='O';    
    }
    
    else
    {

      if(x1==1 && x2==0)
      {	
	escenario[y][x]=' ';
	x=x-1;
	escenario[y][x]='O';		
      }
      else
      {
	if(x2==1 && x3==0)
	{	
	escenario[y][x]=' ';
	y=y+1;
	escenario[y][x]='O';		
	}
	else
	{
	  
	    if(x3==1 && x4==0)
	    {	
	      
	    escenario[y][x]=' ';
	    x=x+1;
	    escenario[y][x]='O';		
	    }
	    else
	    {
		if(x4==1 && x1==0)
		{	
		escenario[y][x]=' ';
		y=y-1;
		escenario[y][x]='O';		
		}
	    }
	}
	
      }
    }
}
Porfavor ayudenme la verdad es que ya me desespero y me da coraje por que la verdad pensaba que podia lograrlo =..(

Lo seguire intentando...
  #2 (permalink)  
Antiguo 12/08/2011, 22:13
Avatar de CapacProg  
Fecha de Ingreso: marzo-2010
Ubicación: Lima - Perú
Mensajes: 324
Antigüedad: 14 años
Puntos: 71
Respuesta: Problemas al cambiar variables globales por locales

¿Haz intentado usar punteros?
Se usan variables globales cuando el valor debe de estar presente en todo el código, pero para salvar eso se puede usar punteros, ya que así pasamos la variable (por referencia) y su valor se puede usar e incluso modificar (si no precedemos por const) sin la necesidad de que se global.
__________________
%
El mejor guerrero no es el que triunfa siempre sino el que vuelve sin miedo a la batalla.
%

Etiquetas: globales, locales, variables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:41.