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

ir chequeando numeros en una matriz

Estas en el tema de ir chequeando numeros en una matriz en el foro de C/C++ en Foros del Web. Tengo un problema con esto espero puedan ayudarme, si to tengo una matriz 116456 115456 234566 789123 Ejemplo si yo estoy en la casilla (0,0) ...
  #1 (permalink)  
Antiguo 28/04/2016, 17:17
 
Fecha de Ingreso: julio-2015
Mensajes: 15
Antigüedad: 8 años, 9 meses
Puntos: 0
ir chequeando numeros en una matriz

Tengo un problema con esto espero puedan ayudarme, si to tengo una matriz

116456
115456
234566
789123
Ejemplo si yo estoy en la casilla (0,0) & al parecer en este momento soy el numero "1" el cual esta rodeado por los numeros "2 , 3 , 4 , 5 , 6" Lo que yo quiero hacer es que cuando le de al numero EJEMPLO "6" mi matriz quede así, osea transforme todos los numeros que llevaba en el numero que voy a presionar.

666456
665456
234566
789123

& si le vuelvo a dar al numero 4 mi matriz quedaría así.

444456
445456
234566
789123 & si vuelvo a presionar el 5 quedaría así

555556
555556
234566
789123 Bueno así hasta llenar la matriz con 1 solo numero, el problema es que en mi código cuando yo presiono el numero cambia todos los números, no solo los que tengo alrededor.

Los números que llevo deberían transformarse en el nuevo que acabo de presionar. espero su ayuda gracias.

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4.  
  5. #define filas 10
  6. #define columnas 10
  7. #define fico 10
  8.  
  9. void cargar_matriz(int matriz[][columnas]);
  10. void pintar_matriz(int matriz[][columnas]);
  11. void movimiento_matriz(int matriz[][columnas]);
  12. void jugar(int matriz[][columnas]);
  13.  
  14.     int i,j;
  15.  
  16.     int main()  {
  17.    
  18.         int matriz[filas][columnas];
  19.        
  20.         jugar(matriz);
  21.        
  22.     }
  23.    
  24.     void cargar_matriz(int matriz[][columnas])  {
  25.        
  26.         for(i = 0; i < filas; i++)  {
  27.             for(j = 0; j < columnas; j++)   {
  28.                 matriz[i][j] = rand()%6+1;
  29.             }
  30.         }
  31.         return;
  32.     }
  33.    
  34.     void pintar_matriz(int matriz[][columnas])  {
  35.        
  36.         for(i = 0; i < filas; i++)  {
  37.             printf("\n\t\t\t");
  38.                 for(j = 0; j < columnas; j++)   {
  39.                     printf("%d",matriz[i][j]);
  40.                 }
  41.             }
  42.             printf("\n\n");
  43.             SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),7);
  44.             printf("\t1 \t2 \t3 \t4 \t5 \t6 \n");
  45.             SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),1);
  46.             printf("\t%c",219);
  47.             SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),2);
  48.             printf("\t%c",219);
  49.             SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),3);
  50.             printf("\t%c",219);
  51.             SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),4);
  52.             printf("\t%c",219);
  53.             SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),5);
  54.             printf("\t%c",219);
  55.             SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),6);
  56.             printf("\t%c",219);
  57.            
  58.            
  59.             movimiento_matriz(matriz);
  60.         }
  61.        
  62.         void movimiento_matriz(int matriz[][columnas]){
  63.        
  64.         int mycolor;
  65.        
  66.         mycolor = matriz[0][0];
  67.         matriz[0][0] = 10;
  68.        
  69.         char tecla;
  70.            
  71.             tecla = getch();
  72.             //matriz[0][0] = 10;
  73.            
  74.             switch (tecla)  {
  75.                
  76.                 case '1' :  {
  77.                     for(i = 0; i < filas; i++)  {
  78.                         for(j = 0; j < columnas; j++)   {
  79.                             if(matriz[i][j] == mycolor ){
  80.                             matriz[i][j] = 10;
  81.                             }
  82.                         }
  83.                     }
  84.                     for(i = 0; i < fico; i++)   {
  85.                         for (j = 0; j < fico; j++){
  86.                             if(matriz[i][j] == 10)  {
  87.                                 matriz[i][j] = 1;
  88.                             }
  89.                         }
  90.                     }
  91.                    
  92.                     break;
  93.                 }
  94.                
  95.                 case '2' :  {
  96.                     for(i = 0; i < filas; i++)  {
  97.                             for(j = 0; j < columnas; j++)   {
  98.                             if(matriz[i][j] == mycolor )    {
  99.                             matriz[i][j] = 10;
  100.                         }
  101.                     }
  102.                    
  103.                     }
  104.                     for(i = 0; i < fico; i++)   {
  105.                         for (j = 0; j < fico; j++){
  106.                             if(matriz[i][j] == 10)  {
  107.                                 matriz[i][j] = 2;
  108.                             }
  109.                         }
  110.                     }
  111.                     break;
  112.                 }
  113.                
  114.                 case '3' :  {
  115.                     for(i = 0; i < filas; i++)  {
  116.                             for(j = 0; j < columnas; j++)   {
  117.                             if(matriz[i][j] == mycolor )    {
  118.                             matriz[i][j] = 10;
  119.                         }
  120.                     }
  121.                     }
  122.                    
  123.                     for(i = 0; i < fico; i++)   {
  124.                         for (j = 0; j < fico; j++){
  125.                             if(matriz[i][j] == 10)  {
  126.                                 matriz[i][j] = 3;
  127.                             }
  128.                         }
  129.                     }
  130.                     break;
  131.                 }
  132.                
  133.                 case '4' :  {
  134.                     for(i = 0; i < filas; i++)  {
  135.                             for(j = 0; j < columnas; j++)   {
  136.                             if(matriz[i][j] == mycolor )    {
  137.                             matriz[i][j] = 10;
  138.                             }
  139.                         }
  140.                     }
  141.                     for(i = 0; i < fico; i++)   {
  142.                         for (j = 0; j < fico; j++){
  143.                             if(matriz[i][j] == 10)  {
  144.                                 matriz[i][j] = 4;
  145.                             }
  146.                         }
  147.                     }
  148.                     break;
  149.                 }
  150.                
  151.                 case '5' :  {
  152.                     for(i = 0; i < filas; i++)  {
  153.                             for(j = 0; j < columnas; j++)   {
  154.                             if(matriz[i][j] == mycolor )    {
  155.                             matriz[i][j] = 10;
  156.                             }
  157.                         }
  158.                     }
  159.                     for(i = 0; i < fico; i++)   {
  160.                         for (j = 0; j < fico; j++){
  161.                             if(matriz[i][j] == 10)  {
  162.                                 matriz[i][j] = 5;
  163.                             }
  164.                         }
  165.                     }
  166.                     break;
  167.                 }
  168.                
  169.                 case '6' :  {
  170.                     for(i = 0; i < filas; i++)  {
  171.                             for(j = 0; j < columnas; j++)   {
  172.                             if(matriz[i][j]== mycolor ) {
  173.                             matriz[i][j] = 10;
  174.                             }
  175.                         }
  176.                     }
  177.                     for(i = 0; i < fico; i++)   {
  178.                         for (j = 0; j < fico; j++){
  179.                             if(matriz[i][j] == 10)  {
  180.                                 matriz[i][j] = 6;
  181.                             }
  182.                         }
  183.                     }
  184.                     break;
  185.                 }
  186.                
  187.                 default : {
  188.                    
  189.                     printf("\nError\n");
  190.                     break;
  191.                 }
  192.             }
  193.            
  194.             system("cls");
  195.             pintar_matriz(matriz);
  196.            
  197.         }
  198.        
  199.         void jugar(int matriz[][columnas])  {
  200.            
  201.             cargar_matriz(matriz);
  202.             pintar_matriz(matriz);
  203.             movimiento_matriz(matriz);
  204.            
  205.         }
  #2 (permalink)  
Antiguo 28/04/2016, 17:48
 
Fecha de Ingreso: octubre-2014
Ubicación: Madrid
Mensajes: 1.212
Antigüedad: 9 años, 6 meses
Puntos: 204
Respuesta: ir chequeando numeros en una matriz

Yo crearia una segunda matriz, del mismo tamaño que la primera e inicializada a 0.

El siguiente paso sería poner un 1 en mi posición actual.

Después crearía un algoritmo que empezando en la posición actual recorriese toda la matriz en todas direcciones comprobando si el número coincide con el inicial. En caso afirmativo se marca esa posición con un 1 en la matriz secundaria. En caso negativo se deja de buscar. Si no hay más posibilidades (tu posición actual está rodeada de '1' en la matriz secundaria) dejas de buscar.

Finalmente recorres la matriz secundaria y por cada 1 sustituyes el valor de la posición actual en la matriz buena por el número que corresponda.

Ejemplo:

Matriz inicial:

Código:
116456
115456
234566
789123
Posición (0,0) y pulso el 6

Código:
116456     110000     666456
115456     110000     665456
234566     000000     234566
789123     000000     789123
Posicion(0,0) y pulso el 4

Código:
666456     111000     444456
665456     110000     445456
234566     000000     234566
789123     000000     789123
Posicion(0,0) y pulso el 5

Código:
444456     111100     555556
445456     110100     555556
234566     000000     234566
789123     000000     789123
Posicion(1,5) y pulso el 2

Código:
555556     000001     555552
555556     000001     555552
234566     000011     234522
789123     000000     789123
PD.: El algoritmo de búsqueda podrías implementarlo recursivo, creo que es la opción más sencilla.

Un saludo
__________________
La ayuda se paga con esfuerzo o con dinero. Si no estás dispuesto a esforzarte y quieres que te hagan los deberes pide presupuesto, al menos así ahorrarás tiempo.

Etiquetas: char, int, matriz, numero, numeros
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 10:05.