Advertisement
manhattanxl

viborita.c

Feb 4th, 2016
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. /* Juego ultra simple del clasico de la viborita (Snake)
  2.  * Controles: w arriba, a izquierda, s abajo, d derecha, p pausa
  3.  * ----------------------------------------------------------------------------
  4.  * "NO LICENSE" (no version)
  5.  * afperrens@gmail.com wrote this file, feel free to do whatever you
  6.  * want with this code - sientase libre de hacer lo que quiera con el codigo.
  7.  * Para sistemas unix-like: gcc -Wall -lncurses -o viborita viborita.c
  8.  * Requisitos: ncurses (en algunos tambien requiriran ncurses-devel)
  9.  * ----------------------------------------------------------------------------
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <sys/ioctl.h>
  14. #include <ncurses.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <unistd.h>
  18. #define BORDE 3
  19.  
  20. struct cViborita{
  21.     int x, y;
  22.     struct cViborita *sig; };
  23.  
  24. int drawCuadradoFULL(){
  25.     struct winsize w;
  26.     ioctl(0, TIOCGWINSZ, &w);
  27.     int x = BORDE, y = BORDE;  
  28.     for( ; x < w.ws_col-BORDE; x++) mvaddch(y,x,ACS_CKBOARD);
  29.     for( ; y < w.ws_row-BORDE; y++) mvaddch(y,x,ACS_CKBOARD);;
  30.     for( ; x > BORDE; x--) mvaddch(y,x,ACS_CKBOARD);
  31.     for( ; y > BORDE; y--) mvaddch(y,x,ACS_CKBOARD);
  32.     return 0; }
  33.  
  34. void agregarCuerpo(struct cViborita *v){
  35.     struct cViborita * nuevo = (struct cViborita *) malloc(sizeof(struct cViborita) * 1);
  36.     while(v->sig != NULL) v = v->sig;
  37.     nuevo->x = v->x;
  38.     nuevo->y = v->y;
  39.     v->sig = nuevo;
  40.     nuevo->sig = NULL; }
  41.  
  42. struct cViborita * moverViborita(struct cViborita *v, int direccion){
  43.     struct cViborita *ant, *aux;
  44.     ant = v;
  45.     aux = v->sig;
  46.     while(aux->sig != NULL){
  47.         ant = aux;
  48.         aux = aux->sig; }
  49.     ant->sig = NULL; // pongo este elemento como la cola
  50.     aux->sig = v; // aux ahora apunta a la cabeza
  51.     aux->x = v->x;
  52.     aux->y = v->y;
  53.     switch(direccion){
  54.         case 1: aux->y--;
  55.         break;
  56.         case 2: aux->x++;
  57.         break;
  58.         case 3: aux->y++;
  59.         break;
  60.         case 4: aux->x--;
  61.         break; }
  62.     ant = aux->sig; // reutilizo ant
  63.     while(ant != NULL){ // controlo que no se choque con sigo mismo
  64.         if(ant->x == aux->x && ant->y == aux->y) return NULL;
  65.         else ant = ant->sig; }
  66.     return aux; }
  67.  
  68. void dibujarVivorita(struct cViborita *v){
  69.     struct cViborita *aux = v;
  70.     move(aux->y,aux->x);
  71.     printw("@");
  72.     aux = aux->sig;
  73.     while(aux != NULL){
  74.         mvprintw(aux->y,aux->x,"#");
  75.         aux = aux->sig; }
  76.     }
  77.  
  78. int obtenerDireccion(int *direccion){
  79.     switch(getch()){
  80.           case 'w': if(*direccion!=3) *direccion = 1; return 1;
  81.           break;
  82.           case 'd': if(*direccion!=4) *direccion = 2; return 1;
  83.           break;
  84.           case 's': if(*direccion!=1) *direccion = 3; return 1;
  85.           break;
  86.           case 'a': if(*direccion!=2) *direccion = 4; return 1;
  87.           break;
  88.           case 'p': return 2; // lanzo pausa
  89.           break; }
  90.     return 0; }
  91.  
  92. int verificarMuerte(struct cViborita *v){
  93.     struct winsize w;
  94.     ioctl(0, TIOCGWINSZ, &w);
  95.     if(v == NULL) return 1; // se choco con si misma
  96.     if((BORDE >= v->x)||(v->x >= w.ws_col-BORDE)||
  97.             (BORDE >= v->y)||(v->y >= w.ws_row-BORDE)) return 1;
  98.     return 0; }
  99.    
  100. void comida(int *x, int *y, struct cViborita *v){
  101.     // le paso la viborita para que no ponga la comida en el cuerpo de la viborita
  102.     agregarCuerpo(v); // comio! le agrego un cuerpo a vivorita
  103.     struct winsize w;
  104.     ioctl(0, TIOCGWINSZ, &w);
  105.     struct cViborita * aux;
  106.     int flag;
  107.     srand(time(NULL)); // si no siempre da el mismo lugar
  108.     do {
  109.         *x = rand() % ((w.ws_col-BORDE-3)-BORDE+2) + BORDE+1;
  110.         *y = rand() % ((w.ws_row-BORDE-3)-BORDE+2) + BORDE+1;
  111.         aux = v;
  112.         flag = 0;
  113.         while(aux != NULL && !flag){
  114.             if(aux->x == *x && aux->y == *y) flag = 1;
  115.             aux = aux->sig; }
  116.         } while(flag); }
  117.  
  118. int main(int argc, char * argv[]){
  119.     initscr(); noecho(); curs_set(0); cbreak(); timeout(0); // ncurses
  120.     struct cViborita *v = (struct cViborita *) malloc(sizeof(struct cViborita));
  121.     v->x = 10; v->y = 10; v->sig = NULL; // inicializo la viborita
  122.     for(int cuerpo = 0; cuerpo < 2; cuerpo++) agregarCuerpo(v);
  123.     int direccion = 2, puntos = 0;
  124.     int comidaX, comidaY;
  125.     comida(&comidaX,&comidaY,v);
  126.     while(1){
  127.         clear();
  128.         move(0,0);
  129.         printw("puntaje: %d", puntos);
  130.         drawCuadradoFULL();
  131.         if(obtenerDireccion(&direccion) == 2){
  132.             move(1,0);
  133.             printw("PAUSA - presione 'p'w'a's'd' para volver o Ctrl+C para salir");
  134.             while(!obtenerDireccion(&direccion)); } // continuo con la pausa
  135.         v = moverViborita(v,direccion);
  136.         if(verificarMuerte(v)){
  137.                 endwin();
  138.                 printf("MURIO! puntaje: %d\n", puntos);
  139.                 return 0; }
  140.         mvaddch(comidaY,comidaX,ACS_DIAMOND); // dibujo la comida
  141.         if(comidaX == v->x && comidaY == v->y){ // la comio?
  142.             puntos++;
  143.             comida(&comidaX,&comidaY,v); }
  144.         dibujarVivorita(v);
  145.         refresh();
  146.         usleep(100000); }
  147.     // no hay necesidad de endwin aqui
  148.     // no hay necesidad de free por que libero la memoria al terminar el juego
  149.     // y las alocaciones siempre crecen, no varian ni se eliminan
  150.     return 0;
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement