From bc099eef1096b3fe55f06c25a43d552d1225c882 Mon Sep 17 00:00:00 2001 From: serxoz Date: Sun, 24 Oct 2021 19:17:22 +0200 Subject: [PATCH] =?UTF-8?q?elefantes=20sobre=20sitios=20vac=C3=ADos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- screen.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/screen.c b/screen.c index 05fdffe..e8e81b1 100644 --- a/screen.c +++ b/screen.c @@ -46,19 +46,28 @@ void cuadra(WINDOW * win, int width, int height){ wrefresh(win); } -void generarElefantes(struct coord *buff){ - srand ( time(NULL) ); +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) == ' '){ + return res; + } else { + randomCoord(win); + } +} + +void generarElefantes(WINDOW * win, struct coord *buff){ 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}; + struct coord elefante = randomCoord(win); buff[i] = elefante; } } @@ -67,6 +76,7 @@ int screen() { initscr(); noecho(); curs_set(0); //cursor invisible + srand ( time(NULL) ); //semilla para rand /* int height, width, start_y, start_x; */ /* height = 40; */ @@ -87,11 +97,10 @@ int screen() { mvwprintw(win, cazador.y, cazador.x, "^"); struct coord elefantes[ELEFANTES] = {0}; - generarElefantes(elefantes); + 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); */ } @@ -154,8 +163,6 @@ int screen() { } } } - - } endwin(); // dealocate memory and end ncurses