2021-10-24 14:01:57 +02:00
|
|
|
#include <stdlib.h>
|
2021-10-24 19:00:52 +02:00
|
|
|
#include <time.h>
|
2021-10-24 14:01:57 +02:00
|
|
|
#include <curses.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
#define ELEFANTES 8
|
2021-10-24 19:00:52 +02:00
|
|
|
const int height = 40;
|
|
|
|
const int width = 80;
|
2021-10-24 14:01:57 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-10-24 19:17:22 +02:00
|
|
|
struct coord randomCoord(WINDOW * win){
|
|
|
|
int nWidthMax=width-10;
|
|
|
|
int nWidthMin=10;
|
|
|
|
int nHeightMax=height-10;
|
|
|
|
int nHeightMin=10;
|
2021-10-24 19:00:52 +02:00
|
|
|
|
2021-10-24 19:17:22 +02:00
|
|
|
int rX = rand() % ((nWidthMax+1)-nWidthMin) + nWidthMin;
|
|
|
|
int rY = rand() % ((nHeightMax+1)-nHeightMin) + nHeightMin;
|
|
|
|
|
|
|
|
struct coord res = {rX, rY};
|
|
|
|
|
2021-10-24 19:21:14 +02:00
|
|
|
if (mvwinch(win, res.y, res.x) != ' '){
|
2021-10-24 19:17:22 +02:00
|
|
|
randomCoord(win);
|
|
|
|
}
|
2021-10-24 19:21:14 +02:00
|
|
|
|
|
|
|
return res;
|
2021-10-24 19:17:22 +02:00
|
|
|
}
|
2021-10-24 19:00:52 +02:00
|
|
|
|
2021-10-24 19:17:22 +02:00
|
|
|
void generarElefantes(WINDOW * win, struct coord *buff){
|
2021-10-24 14:01:57 +02:00
|
|
|
|
2021-10-24 19:17:22 +02:00
|
|
|
for(int i = 0; i < ELEFANTES; ++i) {
|
|
|
|
struct coord elefante = randomCoord(win);
|
2021-10-24 14:01:57 +02:00
|
|
|
buff[i] = elefante;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int screen() {
|
|
|
|
initscr();
|
|
|
|
noecho();
|
|
|
|
curs_set(0); //cursor invisible
|
2021-10-24 19:17:22 +02:00
|
|
|
srand ( time(NULL) ); //semilla para rand
|
2021-10-24 14:01:57 +02:00
|
|
|
|
2021-10-24 19:00:52 +02:00
|
|
|
/* int height, width, start_y, start_x; */
|
|
|
|
/* height = 40; */
|
|
|
|
/* width = 80; */
|
|
|
|
int start_y, start_x;
|
2021-10-24 14:01:57 +02:00
|
|
|
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);
|
|
|
|
|
2021-10-24 19:00:52 +02:00
|
|
|
/* struct coord cazador = {height/3,width/2}; */
|
|
|
|
struct coord cazador = {10,10};
|
|
|
|
mvwprintw(win, cazador.y, cazador.x, "^");
|
2021-10-24 14:01:57 +02:00
|
|
|
|
|
|
|
struct coord elefantes[ELEFANTES] = {0};
|
2021-10-24 19:17:22 +02:00
|
|
|
generarElefantes(win, elefantes);
|
2021-10-24 14:01:57 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < ELEFANTES; i++){
|
2021-10-24 19:00:52 +02:00
|
|
|
mvwprintw(win, elefantes[i].y, elefantes[i].x, "?");
|
|
|
|
/* mvwprintw(win, i+1, 1, "elefante x:%i, y:%i",elefantes[i].x, elefantes[i].y); */
|
2021-10-24 14:01:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while(true){
|
|
|
|
int c = wgetch(win);
|
|
|
|
switch(c){
|
|
|
|
case KEY_UP:
|
|
|
|
/* mvwprintw(win, 1, 1, "up"); */
|
2021-10-24 19:00:52 +02:00
|
|
|
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, "^");
|
2021-10-24 14:01:57 +02:00
|
|
|
wrefresh(win);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
|
|
/* mvwprintw(win, 1, 1, "down"); */
|
2021-10-24 19:00:52 +02:00
|
|
|
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, "^");
|
2021-10-24 14:01:57 +02:00
|
|
|
wrefresh(win);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
|
|
|
/* mvwprintw(win, 1, 1, "right"); */
|
2021-10-24 19:00:52 +02:00
|
|
|
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, "^");
|
2021-10-24 14:01:57 +02:00
|
|
|
wrefresh(win);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_LEFT:
|
|
|
|
/* mvwprintw(win, 1, 1, "left"); */
|
2021-10-24 19:00:52 +02:00
|
|
|
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, "^");
|
2021-10-24 14:01:57 +02:00
|
|
|
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);
|
|
|
|
|
2021-10-24 19:00:52 +02:00
|
|
|
if (mvwinch(win, nova.y, nova.x) == ' '){
|
|
|
|
mvwprintw(win, elefante.y, elefante.x, " ");
|
2021-10-24 14:01:57 +02:00
|
|
|
elefante = nova;
|
|
|
|
elefantes[i] = nova; //garda no array a posicion deste elefante
|
2021-10-24 19:00:52 +02:00
|
|
|
mvwprintw(win, elefante.y, elefante.x, "?");
|
2021-10-24 14:01:57 +02:00
|
|
|
wrefresh(win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endwin(); // dealocate memory and end ncurses
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|