From bf311472f61fc247c72eda93fe9e15c855a851ab Mon Sep 17 00:00:00 2001 From: Gladosse Date: Sat, 8 Jan 2022 10:12:16 +0100 Subject: [PATCH] Upload files to 'src' --- src/board.c | 21 +++++++++- src/board.h | 12 ++---- src/main.c | 112 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 120 insertions(+), 25 deletions(-) diff --git a/src/board.c b/src/board.c index 9fcd93a..3c43566 100644 --- a/src/board.c +++ b/src/board.c @@ -125,7 +125,7 @@ int show_cell(int x,int y, struct Board *brd){ return 0; } -int reveal(int x, int y, struct Board *brd){ +int reveal(int x, int y, struct Board *brd, int* rvl){ int height = brd->height; int width = brd->width; int flags = 0; @@ -143,8 +143,11 @@ int reveal(int x, int y, struct Board *brd){ if(z=0 && v>=0){ if(brd->board_state[v][z].flagged==0){ brd->board_state[v][z].state=1; - if(brd->board_state[v][z].value==69) + if(brd->board_state[v][z].value==69){ + rvl[0] = z; + rvl[1] = v; return 1; + } } } } @@ -158,6 +161,8 @@ int reveal(int x, int y, struct Board *brd){ } } } + rvl[0] = 0; + rvl[1] = 0; return 0; } @@ -211,4 +216,16 @@ void lose(int a, int b, struct Board *brd){ } } dsubimage(a*20+7,b*20+40,&img_red_bomb,0,0,20,20, DIMAGE_NONE); +} + +int won(struct Board* brd){ + int height = brd->height; + int width = brd->width; + for(int y=0; yboard_state[y][x].state == 0 && brd->board_state[y][x].value != 69) + return 0; + } + } + return 1; } \ No newline at end of file diff --git a/src/board.h b/src/board.h index 11ca32d..ab687f7 100644 --- a/src/board.h +++ b/src/board.h @@ -1,14 +1,6 @@ #ifndef _PRINCEOFMYNES_BOARD_H #define _PRINCEOFMYNES_BOARD_H -#define MINE 1 -#define ENGINE_TICK 25 -#define DIR_UP 1 -#define DIR_DOWN 2 -#define DIR_RIGHT 3 -#define DIR_LEFT 4 -#define DIR_IDLE 0 - #include #include #include @@ -32,9 +24,11 @@ void draw_board(struct Board*); void draw_cursor(struct Board*); int show_cell(int ,int, struct Board*); void flag_cell(int ,int, struct Board*); -int reveal(int ,int, struct Board*); +int reveal(int ,int, struct Board*, int*); void set_board(struct Board*); void lose(int, int, struct Board*); +int won(struct Board*); int around(int, int, int, int, struct Board*); + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6239c40..dbee63a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,18 +1,65 @@ #include #include +#include +#include #include "board.h" #include "timer.h" +#define SHIFT 1 +#define OPTN 2 +#define ALPHA 3 +#define DIR_UP 4 +#define DIR_DOWN 5 +#define DIR_LEFT 6 +#define DIR_RIGHT 7 +#define ENGINE_TICK 25 + +static int callback_tick(volatile int *tick) +{ + *tick = 1; + return TIMER_CONTINUE; +} + +static int get_inputs(void) +{ + int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS & ~GETKEY_MOD_SHIFT & ~GETKEY_MOD_ALPHA; + int timeout = 1; + + while(1) + { + key_event_t ev = getkey_opt(opt, &timeout); + if(ev.type == KEYEV_NONE) return -1; + + //int key = ev.key; + // if(key == KEY_SHIFT) + // return KEY_SHIFT; + // if(key == KEY_OPTN) + // return KEY_OPTN; + // if(key == KEY_ALPHA) + // return KEY_ALPHA; + // if(key == KEY_DOWN) + // return KEY_DOWN; + // if(key == KEY_RIGHT) + // return KEY_RIGHT; + // if(key == KEY_UP) + // return KEY_UP; + // if(key == KEY_LEFT) + // return KEY_LEFT; + return ev.key; + } +} + int main(void) { extern bopti_image_t img_bugdenial; + extern bopti_image_t img_alive; + extern bopti_image_t img_dead; dclear(C_WHITE); dupdate(); int key = 0; while(key!=KEY_MENU){ key = 0; int hasgen = 0; - int time = 0; struct Board board; board.height = 9; board.width = 19; @@ -26,16 +73,30 @@ int main(void) dimage(brd->width*20+7,10,&img_bugdenial); int trollface = 0; int trollfaced[2] = {5,5}; - - while(!trollface){ + static volatile int tick = 1; + int t = timer_configure(TIMER_ANY, ENGINE_TICK*1000, GINT_CALL(callback_tick, &tick)); + if(t >= 0) timer_start(t); + + int s_time = 0; + float f_time = 0; + while(trollface == 0){ + while(!tick) sleep(); + tick = 0; + f_time += 25; + s_time = (int)(f_time/1000); dclear(C_WHITE); + int win = won(brd); + if(win==1){ + trollface=2; + } draw_board(brd); dimage(brd->width*20+7,10,&img_bugdenial); draw_cursor(brd); - draw_timer(time, brd); + draw_timer(s_time, brd); + dimage(182, 10, &img_alive); dupdate(); - key = getkey_opt(GETKEY_DEFAULT & ~GETKEY_MOD_SHIFT & ~GETKEY_MOD_ALPHA, NULL).key; - + key = get_inputs(); + if(key==KEY_DOWN && brd->pos[1]height-1) brd->pos[1]++; else if(key==KEY_UP && brd->pos[1]>0) @@ -59,23 +120,45 @@ int main(void) break; } show_cell(brd->pos[0],brd->pos[1],brd); - int rvl = reveal(brd->pos[0],brd->pos[1],brd); - if(rvl == 1){ + int rvl[2]; + int rvl_state = reveal(brd->pos[0],brd->pos[1],brd,rvl); + if(rvl_state==1){ trollface = 1; - trollfaced[0] = brd->pos[0]; - trollfaced[1] = brd->pos[1]; + trollfaced[0] = rvl[0]; + trollfaced[1] = rvl[1]; break; } } - - draw_cursor(brd); + draw_cursor(brd); } while(trollface == 1){ + while(!tick) sleep(); + tick = 0; dclear(C_WHITE); lose(trollfaced[0],trollfaced[1], brd); dimage(brd->width*20+7,10,&img_bugdenial); + draw_timer(s_time, brd); + dimage(182, 10, &img_dead); dupdate(); - key = getkey_opt(GETKEY_DEFAULT & ~GETKEY_MOD_SHIFT & ~GETKEY_MOD_ALPHA, NULL).key; + key = get_inputs(); + + if(key == KEY_OPTN){ + trollface = 0; + trollfaced[0] = 5; + trollfaced[1] = 5; + hasgen = 0; + } + } + while(trollface == 2){ + while(!tick) sleep(); + tick = 0; + dclear(C_WHITE); + draw_board(brd); + dimage(brd->width*20+7,10,&img_bugdenial); + draw_timer(s_time, brd); + dimage(182, 10, &img_alive); + dupdate(); + key = get_inputs(); if(key == KEY_OPTN){ trollface = 0; @@ -87,4 +170,5 @@ int main(void) } return 1; -} \ No newline at end of file +} +