Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: eduardo1012 en 24 Abril 2016, 02:13 am



Título: Puzzle 8 en C
Publicado por: eduardo1012 en 24 Abril 2016, 02:13 am
Tengo este programa, que simula el juego del puzzle . El cuadro de 3x3 en el que hay 8 numeros con un espacio vacio y se tienen que ordenar para ganar.
Mi problema es que no se como hacer el cuadro y meter ahi los numeros:
(http://imageshack.com/a/img924/9090/YNkNtR.png)
Habia escuchado de gotoxy pero ni idea de como implemantarlo y los numeros no se como hacer que no se repitan y me muestre el espacio vacio.
Tengo este codigo que es lo que llevo, aunque no se que podria cambiar
Citar
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>         
#include <time.h>
#include <windows.h>
#include <ctype.h>

int start, tam=0,x, cont, cuadro[8], bidi[3][3];
char nom[50];
void rand_num(int bidi[3][3]);


main()
{
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
   
      printf("CARGANDO, ESPERE UN MOMENTO POR FAVOR.");
      
      Sleep(1000);
      system("cls");
      Sleep(1000);
   
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85);
   
      printf("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.\n");
      printf("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.\n");
      printf("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
      
      SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),14); printf("                                                                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
   
      SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),14); printf("                                                                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
      
      SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),14); printf("                                                                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
         
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
      
      printf("                        *BIENVENIDO*                            ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
      
         SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),14); printf("                                                                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
      
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
      
      printf("                Presione INTRO para continuar...                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
      
      SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),14); printf("                                                                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
      
      SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),14); printf("                                                                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-");
      
      SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE),14); printf("                                                                ");
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85); printf(".-.-\n");
      
      
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),85);
         
      printf("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.\n");
      printf("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.\n");
      printf("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.\n\n");
      
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);

      
      
      
      getchar();
      system("cls");
      Sleep(1000);
      
         
      
      do{
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
      printf("Desea continuar con el juego? (1=SI,2=NO)\n");      
      scanf("%d", &cont);
      
      switch(cont){
         
         case 1: system("cls");
               printf("ES LA HORA DE JUGAR!.\n\n");
               Sleep(1000);  
               system("cls");  
               
         
         break;
         
         case 2: printf("Nos vemos! no dudes en volver si quieres probar este asombroso juego!");
         exit(0);
         break;
         
         default:
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
         system("cls");
         printf("<------------Caracter incorrecto------------->\n\n");  
         Sleep(1000);  
         system("cls");
         cont=3;                  
      }
   
      }while(cont!=1 && cont!=2 );
      
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
      printf("El juego tiene como nombre 'El cuadro magico', en este programa mostraras tu\ndestreza en este sencillo,");
      printf("pero retador juego de destreza absoluta, donde\nte mediras ante un recuadro con 8");
      printf("numeros que tendras queordenar de menor\na mayor.\n");
      printf("\n\nMUCHA SUERTE!\n\n");
      
      

      printf("Cual es su nombre?\n\n");
      fflush(stdin);
      gets(nom);
      
      
      tam=strlen(nom);
      x=0;
      do
      {
            if(!isalpha(nom
  • ))
            {
         
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);
         
            printf("<-- Error, ingrese solo el primer nombre, sin ningun numero ni espacio.-->\n\n");
            
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
            
            printf("<-- Vuelva a introducir su nombre-->\n\n");
         
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);
         
            x=0;
            gets(nom);
            }
            else
             x++;
         
         
      }while(nom
  • !='\0');
         
      
      printf("\nHola: %s\n", nom);
      
      rand_num(bidi);
      
      getch();
}

void rand_num(int bidi[3][3])
{
      srand(time(0));
      int i; int j;
      for(i=0;i<3;i++){
         
         printf("\n ");
         
         for(j=0;j<3;j++){
         

         bidi[j]=rand()%8;
         
         printf(" %d", bidi[j]);
         }
      }
}
Si tienen alguna idea o sugerencia bienvenida


Título: Re: Puzzle 8 en C
Publicado por: HardForo en 24 Abril 2016, 02:37 am
Haces gets(nom) pero despues quieres hacer isalpha(nom) y esa funcion trabaja caracter a caracter no sobre un "string" (cadena de chars)

Código
  1.      gets(nom);
  2.  
  3.      ...
  4.      do
  5.      {            
  6.            if(!isalpha(nom))
  7.            ...
  8.  

Esa parte esta mal....... no usas el indice y el bucle no es asi, mira como es:

Código
  1.   x=0;      
  2.      while (nom[x])
  3.      {
  4.            if(!isalpha(nom[x]) )
  5.            {        
  6.               SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);        
  7.               printf("<-- Error, ingrese solo el primer nombre, sin ningun numero ni espacio.-->\n\n");            
  8.               SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);            
  9.               printf("<-- Vuelva a introducir su nombre-->\n\n");        
  10.               SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),11);        
  11.               x=0;
  12.               gets(nom);
  13.            }
  14.              else x++;
  15.      }
  16.  

Aca otro error:

Código
  1. bidi[j]=rand()%8;

Debe ser  bidi[j]=rand()%8;

Despues miro lo que preguntas! buen domingo


Título: Re: Puzzle 8 en C
Publicado por: eduardo1012 en 24 Abril 2016, 02:57 am
Haces gets(nom) pero despues quieres hacer isalpha(nom) y esa funcion trabaja caracter a caracter no sobre un "string" (cadena de chars)

Código
  1.      gets(nom);
  2.  
  3.      ...
  4.      do
  5.      {            
  6.            if(!isalpha(nom))
  7.            ...
  8.  


Aca otro error:

Código
  1. bidi[j]=rand()%8;

Debe ser  bidi[j]=rand()%8;

Ok lo cambiaré , tienes alguna idea de como mostrar los 8 números y hacer que se vayan cambiando con gotoxy?



Título: Re: Puzzle 8 en C
Publicado por: HardForo en 24 Abril 2016, 03:12 am
Has visto este otro post (http://foro.elhacker.net/programacion_cc/juego_slide_puzzle-t448564.0.html) ? 

Por cierto, para hacer lineas y por tanto el cuadro lo haces con ASCII como ├ └ ┴ etc, revisa una tabla de caracteres ASCII

Cuidado que con rand_num() no haces nada porque solo muestras numeros que no puedes recibir en el resto del programa, estas confundido respecto de como le pasas un array como parametro..... estas creando una copia que se destruye al terminar la funcion y de todas formas no deberias imprimir ahi.


Título: Re: Puzzle 8 en C
Publicado por: eduardo1012 en 26 Abril 2016, 03:57 am
Cambie el codigo y lo hice 3 x 3, pero al momento de validar los movimientos no logro hacer que se muevan correctamente.

Código:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
 
void gotoxy(short x, short y){
    COORD pos = {x , y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}
 
main()
{
    int r, x, y, j, vm, k, v,i;
    int a[9], b[3][3],c[3][3];
 
    srand (time(NULL));
     for (i=0; i<=7; i++) {
        a[i] = rand()%(7+1);
    }
    r = 1;
 
    while (r == 1) {
        r = 0;
        for (i=0; i<=7; i++) {
            if (a[i]==0)
        a[i]++;
            for (j=i+1; j<=7; j++)
{
 
                if (a[i] == a[j] && a[i] !=8) {
                    a[i] = a[i] +1;
                    r = 1;
 
                }
                if (a[i] == a[j] && a[i] ==8) {
                    a[i] = a[i] -rand()%(7+1);
                    r = 1; }
            }}}
 
            j=0;
    for (x=0; x<=2; x++) {
    for(y=0; y<=2; y++)
{b[x][y]=a[j];j++;
         }}
  x=2;
  y=2;
  fflush(stdin);
 
b[2][2]=0;
c[0][0]=1;
         c[0][1]=2;
         c[0][2]=3;
         c[1][0]=4;
         c[1][1]=5;
         c[1][2]=6;
         c[2][0]=7;
         c[2][1]=8;
         c[2][2]=0;
       
 
 
    do{
  ///////////////////IZQ////////////////////
gotoxy(1,2);printf("%c",201); 
        gotoxy(1,3);printf("%c",186);
        gotoxy(1,4);printf("%c",204);
        gotoxy(1,5);printf("%c",186);
        gotoxy(1,6);printf("%c",204);
        gotoxy(1,7);printf("%c",186);
        gotoxy(1,8);printf("%c",200);

///////////////////Arriba////////////////////
gotoxy(2,2);printf("%c",205);   
        gotoxy(3,2);printf("%c",205); 
        gotoxy(4,2);printf("%c",203);
        gotoxy(5,2);printf("%c",205);
        gotoxy(6,2);printf("%c",205);
        gotoxy(7,2);printf("%c",203);
        gotoxy(8,2);printf("%c",205); 
gotoxy(9,2);printf("%c",205);
gotoxy(10,2);printf("%c",187);



/////////////////linea 1///////////////////   
 
        gotoxy(4,3);printf("%c",186);
        gotoxy(4,5);printf("%c",186);
        gotoxy(4,7);printf("%c",186); 

 
 /////////////////linea 2///////////////////   
 
        gotoxy(7,3);printf("%c",186);
        gotoxy(7,5);printf("%c",186);
        gotoxy(7,7);printf("%c",186); 

 /////////////////linea 3///////////////////   
 
        gotoxy(10,3);printf("%c",186);
        gotoxy(10,5);printf("%c",186);
        gotoxy(10,7);printf("%c",186); 
   
//////////////////ABAJO 1/////////////////////
gotoxy(2,4);printf("%c",205);   
        gotoxy(3,4);printf("%c",205); 
        gotoxy(4,4);printf("%c",206);
        gotoxy(5,4);printf("%c",205);
        gotoxy(6,4);printf("%c",205);
        gotoxy(7,4);printf("%c",206);
        gotoxy(8,4);printf("%c",205); 
gotoxy(9,4);printf("%c",205);
gotoxy(10,4);printf("%c",185);

 
//////////////////ABAJO 1/////////////////////
gotoxy(2,6);printf("%c",205);   
        gotoxy(3,6);printf("%c",205); 
        gotoxy(4,6);printf("%c",206);
        gotoxy(5,6);printf("%c",205);
        gotoxy(6,6);printf("%c",205);
        gotoxy(7,6);printf("%c",206);
        gotoxy(8,6);printf("%c",205); 
gotoxy(9,6);printf("%c",205);
gotoxy(10,6);printf("%c",185);


//////////////////ABAJO 1/////////////////////
gotoxy(2,8);printf("%c",205);   
        gotoxy(3,8);printf("%c",205); 
        gotoxy(4,8);printf("%c",202);
        gotoxy(5,8);printf("%c",205);
        gotoxy(6,8);printf("%c",205);
        gotoxy(7,8);printf("%c",202);
        gotoxy(8,8);printf("%c",205); 
gotoxy(9,8);printf("%c",205);
gotoxy(10,8);printf("%c",188);
 
////////////////////////////////////////////
  /////////////////////////
 
  gotoxy(2,3);printf("%d",b[0][0]);
   gotoxy(5,3);printf("%d",b[0][1]);
   gotoxy(8,3);printf("%d",b[0][2]);
   gotoxy(2,7);printf("%d",b[1][0]);
   gotoxy(2,5);printf("%d",b[1][1]);
   gotoxy(5,5);printf("%d",b[1][2]);
   gotoxy(8,5);printf("%d",b[2][0]);
   gotoxy(5,7);printf("%d",b[2][1]);
   gotoxy(8,7);printf("%d",b[2][2]);
 
   //////////////////////////////////////
 
 
    vm=getch();
    system("cls");
 
 //////////////////////////////////////
 
 
    vm=getch();
    system("cls");
 
    switch(vm)
    {
    case 80: //FLECHA DERECHA
    if(x <= 2){
 
v=b[x+1][y];
b[x][y]=v;
b[x+1][y]=0;
x++;
}   
 
    break;
    case 72: //FLECHA IZQUIERDA
    if(x>0)
{
 
v=b[x-1][y];
b[x-1][y]=0;
b[x][y]=v;
    x--;
}
 
    break;
    case 77:   //FLECHA ABAJO
    if(y <= 2)
    {
    v=b[x][y+1];
    b[x][y+1]=0;
    b[x][y]=v;
    y++;
}
 
    break;
    case 75:    //FLECHA ARRIBA
    if(y > 0)
    {
    v=b[x][y-1];
    b[x][y-1]=0;
    b[x][y]=v;
    y--;
}
 
    break;
 
   } }while(vm!=13);
 
}


Título: Re: Puzzle 8 en C
Publicado por: + 1 Oculto(s) en 26 Abril 2016, 05:22 am
uy te falto las etiquetas geshi!!!


Título: Re: Puzzle 8 en C
Publicado por: eduardo1012 en 26 Abril 2016, 05:26 am
uy te falto las etiquetas geshi!!!

Me funciona pero al momento de cambiar los numeros por el 0, no lo hace en el orden que deberia lo hace cruzado y asi. Alguna ayuda?