#include #include #include #include "defs.h" #define ELEFANTES 8 const int height = 40; const int width = 80; double distancia(struct coord c, struct coord e); struct coord fuxir(struct coord c, struct coord e); void cuadra(WINDOW * win, int width, int height){ int medioX = width/2; int medioY = height/2; int anchoCaja = 5; int altoCaja = 2; int bordeL = medioX - anchoCaja; int bordeR = medioX + anchoCaja; int bordeU = medioY - altoCaja; int bordeD = medioY + altoCaja; /* bordesuperior */ for(int i = bordeL; i <= bordeR; i++){ mvwprintw(win, bordeU, i, "*"); } /* borde inferior */ for(int i = bordeL; i <= bordeR; i++){ mvwprintw(win, bordeD, i, "*"); } /* borde izquierdo */ for(int i = bordeU; i <= bordeD; i++){ mvwprintw(win, i, bordeL, "*"); } /* borde dereito */ for(int i = bordeU; i <= bordeD; i++){ mvwprintw(win, i, bordeR, "*"); } /* porta */ mvwprintw(win, height/2+altoCaja, width/2, " "); mvwprintw(win, height/2+altoCaja, width/2-1, " "); mvwprintw(win, height/2+altoCaja, width/2+1, " "); wrefresh(win); } void generarElefantes(struct coord *buff){ srand ( time(NULL) ); for(int i = 0; i < ELEFANTES; ++i) { int nWidthMax=width-10; int nWidthMin=10; int nHeightMax=height-10; int nHeightMin=10; int rX = rand() % ((nWidthMax+1)-nWidthMin) + nWidthMin; int rY = rand() % ((nHeightMax+1)-nHeightMin) + nHeightMin; struct coord elefante = {rX+2,rY+2}; buff[i] = elefante; } } int screen() { initscr(); noecho(); curs_set(0); //cursor invisible /* int height, width, start_y, start_x; */ /* height = 40; */ /* width = 80; */ int start_y, start_x; start_y = 0; start_x = 0; WINDOW * win = newwin(height, width, start_y, start_x); keypad(win, TRUE); //para ter as flechas refresh(); box(win, 0, 0); cuadra(win, width, height); /* struct coord cazador = {height/3,width/2}; */ struct coord cazador = {10,10}; mvwprintw(win, cazador.y, cazador.x, "^"); struct coord elefantes[ELEFANTES] = {0}; generarElefantes(elefantes); for(int i = 0; i < ELEFANTES; i++){ mvwprintw(win, elefantes[i].y, elefantes[i].x, "?"); /* mvwprintw(win, i+1, 1, "elefante x:%i, y:%i",elefantes[i].x, elefantes[i].y); */ } while(true){ int c = wgetch(win); switch(c){ case KEY_UP: /* mvwprintw(win, 1, 1, "up"); */ if (mvwinch(win, cazador.y-1, cazador.x) == ' '){ mvwprintw(win, cazador.y, cazador.x, " "); cazador.y = cazador.y - 1; mvwprintw(win, cazador.y, cazador.x, "^"); wrefresh(win); } break; case KEY_DOWN: /* mvwprintw(win, 1, 1, "down"); */ if (mvwinch(win, cazador.y+1, cazador.x) == ' '){ mvwprintw(win, cazador.y, cazador.x, " "); cazador.y = cazador.y + 1; mvwprintw(win, cazador.y, cazador.x, "^"); wrefresh(win); } break; case KEY_RIGHT: /* mvwprintw(win, 1, 1, "right"); */ if (mvwinch(win, cazador.y, cazador.x+1) == ' '){ mvwprintw(win, cazador.y, cazador.x, " "); cazador.x = cazador.x + 1; mvwprintw(win, cazador.y, cazador.x, "^"); wrefresh(win); } break; case KEY_LEFT: /* mvwprintw(win, 1, 1, "left"); */ if (mvwinch(win, cazador.y, cazador.x-1) == ' '){ mvwprintw(win, cazador.y, cazador.x, " "); cazador.x = cazador.x - 1; mvwprintw(win, cazador.y, cazador.x, "^"); wrefresh(win); } break; } // IA :D for(int i = 0; i < ELEFANTES; i++){ struct coord elefante = elefantes[i]; double d = distancia(cazador, elefante); if (d < 3) { struct coord nova; nova = fuxir(cazador, elefante); if (mvwinch(win, nova.y, nova.x) == ' '){ mvwprintw(win, elefante.y, elefante.x, " "); elefante = nova; elefantes[i] = nova; //garda no array a posicion deste elefante mvwprintw(win, elefante.y, elefante.x, "?"); wrefresh(win); } } } } endwin(); // dealocate memory and end ncurses return 0; }