230 lines
6.4 KiB
C
230 lines
6.4 KiB
C
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <curses.h>
|
|
#include "defs.h"
|
|
|
|
#define ELEFANTES 6
|
|
#define PLAYER_PAIR 1
|
|
|
|
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);
|
|
|
|
int* 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);
|
|
|
|
static int esquinas[4];
|
|
esquinas[0] = bordeL;
|
|
esquinas[1] = bordeU;
|
|
esquinas[2] = bordeR;
|
|
esquinas[3] = bordeD;
|
|
|
|
return esquinas;
|
|
}
|
|
|
|
struct coord randomCoord(WINDOW * win){
|
|
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 res = {rX, rY};
|
|
|
|
if (mvwinch(win, res.y, res.x) != ' '){
|
|
randomCoord(win);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
void generarElefantes(WINDOW * win, struct coord *buff){
|
|
|
|
for(int i = 0; i < ELEFANTES; ++i) {
|
|
struct coord elefante = randomCoord(win);
|
|
buff[i] = elefante;
|
|
}
|
|
}
|
|
|
|
int capturas(WINDOW * win, int *esquinas){
|
|
// recorrer cada posición comprobando si existe un elefante en ela
|
|
// así cando puntuacion == ELEFANTES é que acabou a partida
|
|
// poñerlle un resumo do tempo que lle levou
|
|
//
|
|
/* esquinas = [xIni, yIni, xFin, yFin] */
|
|
/* restolles 1 por que quero o interior do rectangulo */
|
|
int xIni = esquinas[0]-1;
|
|
int xFin = esquinas[2]-1;
|
|
int yIni = esquinas[1]-1;
|
|
int yFin = esquinas[3]-1;
|
|
|
|
int elef = 0;
|
|
for(int x = xIni; x <= xFin; x++ ){
|
|
for(int y = yIni; y <= yFin; y++ ){
|
|
if (mvwinch(win, y, x) == '?'){
|
|
elef++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return elef;
|
|
}
|
|
|
|
int screen() {
|
|
/* if (has_colors() == FALSE) { */
|
|
/* endwin(); */
|
|
/* printf("Your terminal does not support color\n"); */
|
|
/* exit(1); */
|
|
/* } */
|
|
start_color();
|
|
init_pair(PLAYER_PAIR, COLOR_RED, COLOR_MAGENTA);
|
|
|
|
int capt = 0; //capturas
|
|
|
|
initscr();
|
|
noecho();
|
|
curs_set(0); //cursor invisible
|
|
srand ( time(NULL) ); //semilla para rand
|
|
|
|
/* 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();
|
|
|
|
/* dibuxa caixa e cuadra */
|
|
box(win, 0, 0);
|
|
int* esquinasCaja = cuadra(win, width, height);
|
|
wrefresh(win);
|
|
|
|
/* struct coord cazador = {height/3,width/2}; */
|
|
struct coord cazador = {10,10};
|
|
mvwprintw(win, cazador.y, cazador.x, "^");
|
|
|
|
struct coord elefantes[ELEFANTES] = {0};
|
|
generarElefantes(win, 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){
|
|
// Puntuacion
|
|
capt = capturas(win, esquinasCaja);
|
|
mvwprintw(win, 1, 2, "Capturados: %i", capt);
|
|
|
|
// Fin
|
|
if (capt == ELEFANTES) {
|
|
break;
|
|
}
|
|
|
|
// Movemento
|
|
int c = wgetch(win);
|
|
attron(COLOR_PAIR(PLAYER_PAIR));
|
|
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;
|
|
}
|
|
attroff(COLOR_PAIR(PLAYER_PAIR));
|
|
|
|
// 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 capt;
|
|
}
|