elefantes/screen.c

229 lines
6.4 KiB
C
Raw Normal View History

2021-10-24 14:01:57 +02:00
#include <stdlib.h>
#include <time.h>
2021-10-24 14:01:57 +02:00
#include <curses.h>
#include "defs.h"
2021-10-24 21:20:30 +02:00
#define ELEFANTES 6
2021-11-17 11:51:45 +01:00
#define PLAYER_PAIR 1
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);
2021-10-24 21:20:30 +02:00
int* cuadra(WINDOW * win, int width, int height){
2021-10-24 14:01:57 +02:00
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 21:20:30 +02:00
static int esquinas[4];
esquinas[0] = bordeL;
esquinas[1] = bordeU;
esquinas[2] = bordeR;
esquinas[3] = bordeD;
return esquinas;
2021-10-24 14:01:57 +02:00
}
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: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: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;
}
}
2021-10-24 21:20:30 +02:00
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;
}
2021-10-24 14:01:57 +02:00
int screen() {
2021-11-17 11:51:45 +01:00
/* 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);
2021-10-24 21:20:30 +02:00
int capt = 0; //capturas
2021-10-24 14:01:57 +02:00
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
/* 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);
2021-10-24 21:20:30 +02:00
int* esquinasCaja = cuadra(win, width, height);
2021-11-17 11:51:45 +01:00
wrefresh(win);
2021-10-24 14:01:57 +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++){
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){
2021-10-24 21:20:30 +02:00
// Puntuacion
capt = capturas(win, esquinasCaja);
mvwprintw(win, 1, 2, "Capturados: %i", capt);
// Fin
if (capt == ELEFANTES) {
break;
}
// Movemento
2021-10-24 14:01:57 +02:00
int c = wgetch(win);
2021-11-17 11:51:45 +01:00
attron(COLOR_PAIR(PLAYER_PAIR));
2021-10-24 14:01:57 +02:00
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, "^");
2021-10-24 14:01:57 +02:00
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, "^");
2021-10-24 14:01:57 +02:00
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, "^");
2021-10-24 14:01:57 +02:00
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, "^");
2021-10-24 14:01:57 +02:00
wrefresh(win);
}
break;
}
2021-11-17 11:51:45 +01:00
attroff(COLOR_PAIR(PLAYER_PAIR));
2021-10-24 14:01:57 +02:00
// 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, " ");
2021-10-24 14:01:57 +02:00
elefante = nova;
elefantes[i] = nova; //garda no array a posicion deste elefante
mvwprintw(win, elefante.y, elefante.x, "?");
2021-10-24 14:01:57 +02:00
wrefresh(win);
}
}
}
}
endwin(); // dealocate memory and end ncurses
2021-10-24 21:20:30 +02:00
return capt;
2021-10-24 14:01:57 +02:00
}