Advertisement
yoel_monsalve

Pelota

Mar 9th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. /* Define sistema operativo (estandar o Windows) */
  6. #if defined(_UNIX) || defined(_LINUX) || defined(_UNIX)
  7.     #define _STD
  8.     #ifdef _WIN
  9.         #undef _WIN
  10.     #endif
  11. #else
  12.     #ifndef _WIN   
  13.         #define _WIN
  14.     #endif
  15.     #ifdef _STD
  16.         #undef _STD
  17.     #endif
  18. #endif
  19.  
  20. /* fichero de cabecera necesario para Windows */
  21. #ifdef _WIN
  22.     #include <windows.h>
  23. #endif
  24.  
  25. /* necesario para termios (Linux) */
  26. #ifdef _STD
  27.     #include <termios.h>
  28.     #include <unistd.h>
  29.     #include <fcntl.h>
  30.     #include <sys/ioctl.h>
  31.     #include <linux/kd.h>
  32. #endif
  33.  
  34. /* Define constantes de secuencias de escape de color */
  35. #define RESET "\x1B[0m"
  36. #define BOLD "\x1B[1m"
  37. #define BRIGHT "\x1B[2m"
  38. #define HIGHLIGHT "\x1B[4m"
  39. /* texto */
  40. #define BLACK "\x1B[30m"
  41. #define RED "\x1B[31m"
  42. #define GREEN "\x1B[32m"
  43. #define BROWN "\x1B[33m"
  44. #define BLUE "\x1B[34m"
  45. #define PINK "\x1B[35m"
  46. #define CYAN "\x1B[36m"
  47. #define WHITE "\x1B[37m"
  48. /* fondo (background) */
  49. #define BBLACK "\x1B[40m"
  50. #define BRED "\x1B[41m"
  51. #define BGREEN "\x1B[42m"
  52. #define BBROWN "\x1B[44m"
  53. #define BBLUE "\x1B[44m"
  54. #define BPINK "\x1B[45m"
  55. #define BCYAN "\x1B[46m"
  56. #define BWHITE "\x1B[47m"
  57.  
  58. /* Vocales acentuadas */
  59. #define a_acute '\xA2'
  60. #define e_acute '\x82'
  61. #define u_acute '\xA3'
  62.  
  63. #ifdef _WIN
  64. void color( int );
  65. #endif
  66. void imprimir (int matriu[50][50]);
  67. void pelota (int matriu[50][50]);
  68. void cambiartabla (int matriu[50][50]);
  69. void clrscr( void );
  70. void draw_point (int , int );
  71. void draw_x (int , int );
  72.  
  73. #if defined(_LINUX) || defined(_UNIX)
  74.     #define STDINFD fileno(stdin)
  75.     int getch( void );
  76.     int getch3( void );
  77. #endif
  78.  
  79. int main()
  80. {
  81.     //system( "MODE 110,150");
  82.     int num;
  83.     int matriu[50][50] = {
  84.     {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  85.     {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
  86.     {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
  87.     {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
  88.     {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
  89.     {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  90.     {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  91.     {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,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130.     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  131.     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  132.     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  133.     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  134.     };
  135.      
  136.     while (num != 5)
  137.     {
  138.         clrscr();
  139.         fflush(stdout);
  140.         printf("Que acci%cn desea realizar:\n", a_acute);
  141.         printf("\t1.- Mostrar la tabla.\n");
  142.         printf("\t2.- Tirar una bola.\n");
  143.         printf("\t3.- Tirar 20 bolas.\n");
  144.         printf("\t4.- Cambiar la tabla.\n");
  145.         printf("\t5.- Salida.\n");
  146.         scanf("%d",&num);
  147.         getchar();
  148.        
  149.         switch(num)
  150.         {
  151.         case 1:
  152.             imprimir (matriu);
  153.             getchar();
  154.             break;
  155.         case 2:
  156.             pelota (matriu);
  157.             break;
  158.         case 3:
  159.             printf("caso 3\n");
  160.             break;
  161.         case 4:
  162.             cambiartabla (matriu);
  163.             break;
  164.         case 5:
  165.             printf("Adios.\n");
  166.             printf("\n");
  167.             break;
  168.         default:
  169.             printf("El n%cmero introducido no pertenece al menos, int%cntelo de nuevo.\n", u_acute, e_acute);
  170.         }
  171.     }
  172.     return 0;
  173. }
  174.  
  175. void imprimir (int matriu[50][50]) {
  176.  
  177.     int i,j;
  178.     for (i = 0; i < 50; i++){
  179.         for (j = 0; j < 50; j++){
  180.               if (matriu[i][j]==1)
  181.                  printf("X");
  182.               else
  183.                  printf(".");
  184.             }
  185.     printf ("\n");
  186.     }
  187. }
  188.  
  189. void pelota (int matriu[50][50]){
  190.  
  191.     int inicio, i, j, cont=0, colum_final;
  192.     char dir;      
  193.     /* NOTA.- los posibles valores de dir son
  194.      * 'u': up (arriba)
  195.      * 'd': down (abajo)
  196.      * 'l': left (izq)
  197.      * 'r': right (der)
  198.      */
  199.     char blocked;   /* si la pelota se haya "bloqueada" */
  200.     char c;
  201.    
  202.     #if 0
  203.     do {
  204.         printf("\nIntroducir el n\xA3mero de columna por el cual desea lanzar la primera bola (1-50): ");
  205.         scanf ("%d", &inicio);
  206.         getchar();
  207.     } while (inicio < 0 || inicio > 50);
  208.     inicio--;
  209.  
  210.     do {
  211.         printf("\nEn caso de encontrarse con un obst\xAculo, en que direcci\xA2n\nquiere que se dirija la bola, derecha (D-d) o izquierda (I-i): ");
  212.         scanf(" %c", &direccion);
  213.         getchar();
  214.     }   while ( tolower(direccion) != 'd' && tolower(direccion) != 'i' );
  215.     #endif
  216.    
  217.     /* Dibujar el tablero */
  218.     for (i = 0; i < 50 ; i++)
  219.     {
  220.         for (j = 0; j < 50; j++)
  221.         {
  222.             if( matriu[i][j] == 1 ) {
  223.                 draw_x( i, j);
  224.                 /* fondo blanco o gris */
  225.                 #ifdef _STD
  226.                     printf(BWHITE);
  227.                 #else
  228.                     color (7);
  229.                 #endif
  230.             }
  231.             else {
  232.                 /* verde sobre blanco */
  233.                 draw_point( i, j);
  234.                 /* fondo blanco */
  235.                 #ifdef _STD
  236.                     printf(BWHITE);
  237.                 #else
  238.                     color(7);
  239.                 #endif
  240.             }
  241.         }
  242.         /* restaura colores al defecto */
  243.         #ifdef _STD
  244.             printf(RESET);
  245.         #endif
  246.         printf("\n");
  247.     }
  248.    
  249.     /* Ahora vamos a mover la pelota. Empezamos siempre en (0,0), y el juego
  250.      * termina al alcanzar la linea inferior del tablero, o después de 100 intentos */
  251.     i = 0;
  252.     j = 0;
  253.     gotoxy(0, 0);
  254.    
  255.     dir = '\0';     /* al inicio no hay direccion definida, la establece el usuario */
  256.     while ( i < 49 && cont++ < 100 ) {
  257.  
  258.         /* determinamos si la bola tiene libertad de movimiento */
  259.         blocked = 0;
  260.         switch ( dir ) {
  261.             case 'd':
  262.                 if ( matriu[i+1][j] == 1 )
  263.                     blocked = 1;
  264.                 else {
  265.                     blocked = 0;
  266.                     draw_point( i, j);  /* borra de la posición anterior */
  267.                     i++;
  268.                 }
  269.                 break;
  270.             case 'u':
  271.                 if ( i == 0 || ( i > 0 && matriu[i-1][j] == 1 ) )
  272.                     blocked = 1;
  273.                 else {
  274.                     blocked = 0;
  275.                     draw_point( i, j);  /* borra de la posición anterior */
  276.                     i--;
  277.                 }
  278.                 break;
  279.             case 'r':
  280.                 if ( j == 49 || matriu[i][j+1] == 1 )
  281.                     blocked = 1;
  282.                 else {
  283.                     blocked = 0;
  284.                     draw_point( i, j);  /* borra de la posición anterior */
  285.                     j++;
  286.                 }
  287.                 break;
  288.             case 'l':
  289.                 if ( j == 0 || matriu[i][j-1] == 1 )
  290.                     blocked = 1;
  291.                 else {
  292.                     blocked = 0;
  293.                     draw_point( i, j);  /* borra de la posición anterior */
  294.                     j--;
  295.                 }
  296.                 break;
  297.         }
  298.    
  299.         /* Representar la posicion de la bola */
  300.         #ifdef _STD
  301.             /* Linux: azul sobre fondo rojo */
  302.             printf( BLUE );
  303.             printf( BRED );
  304.         #else
  305.             /* Windows: amarillo sobre rojo */
  306.             color( 206 );
  307.         #endif
  308.         gotoxy( j, i );
  309.         printf( "%d", 0);
  310.          
  311.         /* restaura colores al defecto, y espera */
  312.         #ifdef _STD
  313.             printf(RESET);
  314.             //usleep( 200000 );
  315.         #else
  316.             color( 7 );
  317.             Sleep( 500 )
  318.         #endif
  319.        
  320.         /* definir la direccion del proximo movimiento */
  321.         c = getch();
  322.         if ( c == '8' )
  323.             dir = 'u';
  324.         else if ( c == '2' )
  325.             dir = 'd';
  326.         else if ( c == '4' )
  327.             dir = 'l';
  328.         else if ( c == '6' )
  329.             dir = 'r';
  330.         else if ( c == 'q' )        /* 'q' para salir */
  331.             return;
  332.     }
  333.    
  334.     /* Mensaje final  */
  335.     gotoxy( 0, 51);
  336.     if ( i == 49 )
  337.         printf("Has ganado !!!\n");
  338.     else
  339.         printf("Debes intentarlo de nuevo\n");
  340.     getchar();
  341. }
  342.  
  343. void cambiartabla (int matriu[50][50]){
  344.  
  345. int i,j,fila,columna;
  346. char seguir;
  347. while(seguir!=2)
  348. {
  349. printf("\n");
  350.    printf("Presiona 1 si quieres seguir cambiando valores.\n");
  351. printf("Presiona 2 si quieres ver la tabla.\n");
  352. scanf("%c",&seguir);
  353. switch(seguir)
  354. {
  355. case '1':
  356.  
  357. do{
  358.              printf("\nIntroducir la fila del valor que quieres cambiar (1-50): ");
  359.              scanf("%d",&columna);
  360.              printf("\nIntroducir la columna del valor que quieres cambiar (1-50): ");
  361.              scanf("%d",&fila);
  362.  
  363. }while(columna>=51 && fila>=51 );
  364.          i=columna,j=fila;
  365.          if (matriu[i][j] == 0)
  366.            matriu[i][j] = 1;
  367.          break;
  368.  
  369. case '2':
  370.  
  371.          for(i=0;i<49;i++)
  372.            for(j=0;j<48;j++)
  373.              if (matriu[i][j]==1 && matriu[i+1][j+1]==1 && matriu[i][j+2]==1)
  374.            puts("ERROR NO SE HA HECHO NINGUN CAMBIO");
  375.            matriu[columna][fila] = 0;
  376.  
  377.        for(i=0;i<50;i++)
  378.        {
  379.           //printf("\t\t");
  380.            for(j=0;j<50;j++)
  381.            {
  382.               if(matriu[i][j]==1)
  383.                  printf("X  ");
  384.               else if(matriu[i][j]==0 )
  385.                  printf(".  ");
  386.           }
  387.           printf("\n");
  388.        }
  389.        break;
  390.      default:
  391. printf("El n\xA3mero introducido es incorrecto.\n");
  392.  
  393. }
  394. }
  395. }
  396.  
  397. /***********************************************
  398.       ***   FUNCION COLOR (solo Windows)  ***
  399. ***********************************************/
  400.  
  401. #ifdef _WIN
  402.     void color (int n)
  403.     {
  404.         SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), n );
  405.     }
  406. #endif
  407.  
  408. /***********************************************/
  409.            /***   FUNCION GOTOXY   ***/
  410. /***********************************************/
  411.  
  412. int gotoxy(int x, int y)
  413. {
  414.    
  415.     /* estandar */
  416.     #ifdef _STD
  417.         printf("\x1B[%d;%dH", y, x);
  418.     #endif
  419.    
  420.     /* windows */
  421.     #ifdef _WIN
  422.         COORD coord;
  423.         HANDLE h_stdout;
  424.         coord.X = x;
  425.         coord.Y = y;
  426.         if ((h_stdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE)
  427.           return 0;
  428.         if (SetConsoleCursorPosition(h_stdout, coord) == 0)
  429.           return 0;
  430.         return 1;
  431.     #endif
  432. }
  433.  
  434. /* Dibujar un punto en el tablero */
  435. void draw_point (int i, int j) {
  436.    
  437.     #ifdef _STD
  438.         printf(WHITE);
  439.         printf(BGREEN);
  440.     #else
  441.         color (114);
  442.     #endif
  443.     gotoxy(j, i);
  444.     printf(".");
  445. }
  446.  
  447. /* Dibujar una 'x' en el tablero */
  448. void draw_x (int i, int j) {
  449.  
  450.     /* blanco sobre verde */
  451.     #ifdef _STD
  452.         printf(GREEN);
  453.         printf(BWHITE);
  454.     #else
  455.         color (191);
  456.     #endif
  457.     gotoxy (j,i);
  458.     printf("X");
  459. }
  460. /* Borra la pantalla */
  461. void clrscr( void ) {
  462.    
  463.     #ifdef _STD
  464.         system("clear");
  465.     #endif
  466.    
  467.     #ifdef _WIN
  468.         system("cls");
  469.     #endif
  470. }
  471.  
  472. #if defined(_LINUX) || defined(_UNIX)
  473. /*----------------------------------------------------------------------------------------
  474.    Lee un carácter de la entrada estándar.
  475.    Devuelve el código de la tecla pulsada, sin necesidad de pulsar ENTER al final.
  476.    Imita el comportamiento de la función getch() de la librería <conio.h> de Borland.
  477. ----------------------------------------------------------------------------------------*/
  478. int getch(void) {
  479.  
  480.     unsigned char c;
  481.     int c_extend;
  482.     struct termios p, old_p;
  483.     int fd;
  484.     int n_bytes;
  485.     FILE *fPtr;
  486.    
  487.     // rescar la estructura de configuración termios, con tcgetattr()
  488.     // (save the termios configuration struct, with tcgetattr())
  489.     tcgetattr(STDINFD, &old_p);
  490.  
  491.     /* activar modo RAW
  492.        (activate RAW mode) */
  493.     p = old_p;
  494.     cfmakeraw(&p);
  495.  
  496.     /* pasar los nuevos paramentros
  497.        (pass the new parameters) */
  498.     tcsetattr(STDINFD, TCSANOW, &p);
  499.    
  500.     fflush(stdout);
  501.     fflush(stderr);
  502.        
  503.     if ((n_bytes = read(STDINFD, &c_extend, 2*sizeof(char))) <= 0)
  504.         return -1;
  505.    
  506.     if (n_bytes == 1)       /* se leyó un carácter simple mono-byte */
  507.         c_extend &= 0x00ff;
  508.  
  509.     if (n_bytes == 2) { /* si leyeron 2 bytes */
  510.         if (c_extend = 0x5b1b) {        /* se leyó la secuencia ESC [, ahora leemos el siguiente carácter */
  511.             read(STDINFD, &c, sizeof(char));
  512.             c_extend = 0x1b00 | c;
  513.         }
  514.     }
  515.    
  516.     // restablecemos la struct termios, con tcsetattr
  517.     // (restore the termios struct, with tcsetattr)
  518.     tcsetattr(STDINFD, TCSANOW, &old_p);
  519.    
  520.     return c_extend;
  521. }
  522. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement