/
Inicio :: Foros

 F.A.Q.F.A.Q.                  Conéctese para revisar sus mensajesConéctese para revisar sus mensajes   

Conseguir resolver un juego de puzzle automaticamente

 
      Índice del Foro elrincondelc.com -> Algoritmos
Ver tema anterior :: Ver siguiente tema  
AutorMensaje
juanjo__r19



Registrado: 20 Ene 2012
Mensajes: 1

MensajePublicado: 20/01/2012 3:43 am
Título: Conseguir resolver un juego de puzzle automaticamente

Hola buenas.

Quería saber como es posible hacer que una matriz con números aleatorios y un hueco se ordenen automáticamente dejando el hueco al final en c++. Se supone que se debe hacer con pilas, colas y listas, pero no sabemos cómo.

Hemos conseguido hacer el juego y que funcione de forma manual. Aquí os adjunto el codigo:



Código:
void Tablero::IniciaTablero(int filas, int columnas)
{
   nfils = filas;
   ncols = columnas;
     
   int i, contador, j; 
   
   matriz = new int* [nfils];
   
   for(i=0;i<nfils;i++)
   {
      matriz[i] = new int[ncols];
   }
   
   contador = 0;
   
   for(i=0;i<nfils;i++)
   {
      for(j=0;j<ncols;j++)
      {
         matriz[i][j] = contador;
         contador++;
      }
     
      cout<<endl;
   }cout<<endl;
   
}

void Tablero::DibujaTablero()
{
   int i, j;
   system("cls");
   cout<<"Juego del puzzle deslizante";

   for(i = 0; i < nfils; i ++ )
   {
      for(j = 0; j < ncols; j ++ )
      {
         cout << " --";
      }
      cout << endl;
         for(j = 0; j < ncols; j ++ )
         {
             cout << "|";
             cout.width(2);
             if ( matriz[i][j] != 0 )
                cout << matriz[i][j];
             else
                cout << " ";
         }
         cout << "|" << endl;
   }
   for(j = 0; j < ncols; j ++ )
   {
      cout << " --";
   }
   cout << endl << endl;
   
}

void Tablero::BuscaFicha(int movida1)
{
     int i,j;
     
     movida = movida1;
     
     for(i=0;i<nfils;i++)
     {
        for(j=0;j<ncols;j++)
        {
           if(matriz[i][j] == movida)
           {
               pos_i = i;
               pos_j = j;
           }
        }
     }
}

void Tablero::MueveFicha(int movida1)
{
     int i,j,x,y,pos_x,pos_y;
     
     
     
     movida = movida1;
     
     for(x=0;x<nfils;x++)
     {
        for(y=0;y<ncols;y++)
        {
           if(matriz[x][y] == 0)
           {
              pos_x = x;
              pos_y = y;
           }
        }
     
     }
     if ((pos_i == pos_x) && ((pos_j+1 == pos_y) || (pos_j-1 == pos_y)))
     {
        for(i=0;i<nfils;i++)
        {
           for(j=0;j<ncols;j++)
           {
              if(matriz[i][j] == 0)
              {
                 matriz[i][j] = movida;
                 matriz[pos_i][pos_j] = 0;
              }
           }
        }
     }
     if ((pos_j == pos_y) && ((pos_i+1 == pos_x) || (pos_i-1 == pos_x)))
     {
        for(i=0;i<nfils;i++)
        {
           for(j=0;j<ncols;j++)
           {
              if(matriz[i][j] == 0)
              {
                 matriz[i][j] = movida;
                 matriz[pos_i][pos_j] = 0;
              }
           }
        }
     }
}

bool Tablero::Finalizado()
{
     int i ,j;
     final = false;
     int a = 0, b = 1;
     
     
     
     for (i=0 ; i<nfils ; i++)
         {
            for(j=0 ; j<ncols ; j++)
            {
                    if (matriz[i][j] == b)
                    {
                       a++;
                    }
               b++;
            }
         }
     if (a == b-2)
     {
        final = true;
        cout<<"**********************************"<<endl;
        cout<<"****                          ****"<<endl;
        cout<<"**** HAS COMPLETADO EL PUZZLE ****"<<endl;
        cout<<"****                          ****"<<endl;
        cout<<"**********************************"<<endl;
     }   
     return final;
}

Muchas gracias de antemano. Saludos.
Volver arriba
      Índice del Foro elrincondelc.com -> Algoritmos
Página 1 de 1Todas las horas están en GMT - 8 Horas

 
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas

(c) ElRincondelC.com

Un proyecto de UrlanHeat.com