154 lines
4.4 KiB
C
154 lines
4.4 KiB
C
#include <stdlib.h>
|
|
#include <curses.h>
|
|
#include "defs.h"
|
|
|
|
#define ELEFANTES 8
|
|
|
|
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){
|
|
for(int i = 0; i < ELEFANTES; ++i) {
|
|
int nMax=40;
|
|
int nMin=10;
|
|
int rX = rand() % ((nMax+1)-nMin) + nMin;
|
|
int rY = rand() % ((nMax+1)-nMin) + nMin;
|
|
|
|
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;
|
|
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};
|
|
mvwprintw(win, cazador.x, cazador.y, "^");
|
|
|
|
struct coord elefantes[ELEFANTES] = {0};
|
|
generarElefantes(elefantes);
|
|
|
|
for(int i = 0; i < ELEFANTES; i++){
|
|
mvwprintw(win, 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.x-1, cazador.y) == ' '){
|
|
mvwprintw(win, cazador.x, cazador.y, " ");
|
|
cazador.x = cazador.x - 1;
|
|
mvwprintw(win, cazador.x, cazador.y, "^");
|
|
wrefresh(win);
|
|
}
|
|
break;
|
|
case KEY_DOWN:
|
|
/* mvwprintw(win, 1, 1, "down"); */
|
|
if (mvwinch(win, cazador.x+1, cazador.y) == ' '){
|
|
mvwprintw(win, cazador.x, cazador.y, " ");
|
|
cazador.x = cazador.x + 1;
|
|
mvwprintw(win, cazador.x, cazador.y, "^");
|
|
wrefresh(win);
|
|
}
|
|
break;
|
|
case KEY_RIGHT:
|
|
/* mvwprintw(win, 1, 1, "right"); */
|
|
if (mvwinch(win, cazador.x, cazador.y+1) == ' '){
|
|
mvwprintw(win, cazador.x, cazador.y, " ");
|
|
cazador.y = cazador.y + 1;
|
|
mvwprintw(win, cazador.x, cazador.y, "^");
|
|
wrefresh(win);
|
|
}
|
|
break;
|
|
case KEY_LEFT:
|
|
/* mvwprintw(win, 1, 1, "left"); */
|
|
if (mvwinch(win, cazador.x, cazador.y-1) == ' '){
|
|
mvwprintw(win, cazador.x, cazador.y, " ");
|
|
cazador.y = cazador.y - 1;
|
|
mvwprintw(win, cazador.x, cazador.y, "^");
|
|
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.x, nova.y) == ' '){
|
|
mvwprintw(win, elefante.x, elefante.y, " ");
|
|
elefante = nova;
|
|
elefantes[i] = nova; //garda no array a posicion deste elefante
|
|
mvwprintw(win, elefante.x, elefante.y, "?");
|
|
wrefresh(win);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
endwin(); // dealocate memory and end ncurses
|
|
|
|
return 0;
|
|
}
|
|
|