display time and mark info

This commit is contained in:
2024-12-20 17:58:35 +03:00
parent 0944b03fc0
commit 3fce5522e0

View File

@@ -26,6 +26,7 @@ typedef struct minefield_t {
int row; int row;
int col; int col;
cell* cells; cell* cells;
int mine_count;
} Minefield; } Minefield;
static inline cell get_cell(Minefield* mf, int r, int c){ static inline cell get_cell(Minefield* mf, int r, int c){
@@ -40,7 +41,7 @@ Minefield make_minefield(int row, int col, int mine_count){
Minefield mf = {.row = row, .col = col}; Minefield mf = {.row = row, .col = col};
int size = mf.row * mf.col; int size = mf.row * mf.col;
mf.cells = (cell *)calloc(size, sizeof(cell)); mf.cells = (cell *)calloc(size, sizeof(cell));
mf.mine_count = mine_count;
// 0 initialization // 0 initialization
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
mf.cells[i] = 0; mf.cells[i] = 0;
@@ -109,6 +110,7 @@ void print_minefield(Minefield *mf, FILE *stream) {
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
float cell_size; float cell_size;
int mark_count;
bool dead; bool dead;
void draw_minefield(Minefield* mf){ void draw_minefield(Minefield* mf){
@@ -128,6 +130,12 @@ void draw_minefield(Minefield* mf){
} }
} }
} }
char time_str[20];
float time = GetTime();
snprintf(time_str, 100, "%02d.%02d - %d/%d",
(int) time / 60, (int) time % 60,
mark_count, mf->mine_count);
DrawText(time_str, screenWidth / 2 - 20, screenHeight - 30, 20, BLACK);
} }
cell* clicked_cell(Minefield* mf, int x, int y){ cell* clicked_cell(Minefield* mf, int x, int y){
@@ -196,7 +204,7 @@ void handle_click(Minefield* mf){
printf("Click outside range, (%d, %d)\n", r, c); printf("Click outside range, (%d, %d)\n", r, c);
return; return;
} }
mark_count += mf->cells[r * mf->col + c] & MARKED ? -1 : 1;
mf->cells[r * mf->col + c] ^= MARKED; mf->cells[r * mf->col + c] ^= MARKED;
} }
@@ -211,7 +219,7 @@ int main(){
Minefield mf = make_minefield(10, 20, 30); Minefield mf = make_minefield(10, 20, 30);
/* print_minefield(&mf, stdout); */ /* print_minefield(&mf, stdout); */
dead = false; dead = false;
mark_count = 0;
{ {
float width = (float) screenWidth / mf.col; float width = (float) screenWidth / mf.col;
float height = (float) screenHeight / mf.row; float height = (float) screenHeight / mf.row;