Skip to content

Commit

Permalink
refactoreing creation of game view
Browse files Browse the repository at this point in the history
  • Loading branch information
Judro committed Nov 19, 2024
1 parent 332b5fa commit abb06a6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 122 deletions.
63 changes: 32 additions & 31 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ char *get_left_margin(unsigned int l) {
return ret;
}

void print_header(PrintableHeaderInstance gph, unsigned int terminal_x,
void print_header(GameView view, unsigned int terminal_x,
unsigned int game_x) {
print_left_margin(terminal_x, game_x);
printw("│");
int mtw = 15;
if (gph->width >= 25) {
printw(" %03d │", gph->mines);
if (view->width >= 25) {
printw(" %03d │", view->mines);
mtw = 17;
} else {
printw(" %03d │", gph->mines);
printw(" %03d │", view->mines);
}
for (int i = 0; i < gph->width - mtw; i++) {
if (i == (gph->width - mtw) / 2) {
switch (gph->state) {
for (int i = 0; i < view->width - mtw; i++) {
if (i == (view->width - mtw) / 2) {
switch (view->state) {
case Playing:
printw(" ");
break;
Expand All @@ -60,14 +60,14 @@ void print_header(PrintableHeaderInstance gph, unsigned int terminal_x,
}
printw(" ");
}
printw(" │ %02ld:%02ld │\n", gph->time / 60, gph->time % 60);
printw(" │ %02ld:%02ld │\n", view->time / 60, view->time % 60);
print_left_margin(terminal_x, game_x);
printw("└");
int mrb = 5;
if (gph->width >= 25)
if (view->width >= 25)
mrb = 7;
for (int i = 0; i < gph->width; i++) {
if (i == mrb || i == gph->width - 8) {
for (int i = 0; i < view->width; i++) {
if (i == mrb || i == view->width - 8) {
printw("┴");
continue;
}
Expand All @@ -76,7 +76,7 @@ void print_header(PrintableHeaderInstance gph, unsigned int terminal_x,
printw("┘\n");
}

void print(PrintableInstance gp, unsigned int terminal_x, unsigned int game_x) {
void print(GameView view, unsigned int terminal_x, unsigned int game_x) {
char is_in_radius = 0;
extern char g_helper_mode;
init_pair(1, COLOR_BLUE, COLOR_BLACK);
Expand All @@ -87,29 +87,29 @@ void print(PrintableInstance gp, unsigned int terminal_x, unsigned int game_x) {
init_pair(6, COLOR_CYAN, COLOR_BLACK);
print_left_margin(terminal_x, game_x);
printw("┌");
for (int i = 0; i < gp->width; i++) {
for (int i = 0; i < view->width; i++) {
printw("─");
}
printw("┐\n");
for (int i = 0; i < gp->width * gp->height; i++) {
if (i % gp->width == 0 && i > 0) {
for (int i = 0; i < view->width * view->height; i++) {
if (i % view->width == 0 && i > 0) {
printw("│\n");
}
if (i % gp->width == 0) {
if (i % view->width == 0) {
print_left_margin(terminal_x, game_x);
printw("│");
}
if (i == (gp->player.y * gp->width + gp->player.x))
if (i == (view->player.y * view->width + view->player.x))
attron(A_STANDOUT);
if ((i == gp->player.y * gp->width + gp->player.x + 1 ||
i == gp->player.y * gp->width + gp->player.x - 1 ||
i == gp->player.y * gp->width + gp->player.x + 1 + gp->width ||
i == gp->player.y * gp->width + gp->player.x - 1 + gp->width ||
i == gp->player.y * gp->width + gp->player.x + 1 - gp->width ||
i == gp->player.y * gp->width + gp->player.x - 1 - gp->width ||
i == gp->player.y * gp->width + gp->player.x - gp->width ||
i == gp->player.y * gp->width + gp->player.x + gp->width) &&
i % gp->width >= 0 && abs(i % gp->width - gp->player.x) < 2 &&
if ((i == view->player.y * view->width + view->player.x + 1 ||
i == view->player.y * view->width + view->player.x - 1 ||
i == view->player.y * view->width + view->player.x + 1 + view->width ||
i == view->player.y * view->width + view->player.x - 1 + view->width ||
i == view->player.y * view->width + view->player.x + 1 - view->width ||
i == view->player.y * view->width + view->player.x - 1 - view->width ||
i == view->player.y * view->width + view->player.x - view->width ||
i == view->player.y * view->width + view->player.x + view->width) &&
i % view->width >= 0 && abs(i % view->width - view->player.x) < 2 &&
g_helper_mode) {
is_in_radius = 1;
attron(A_BOLD);
Expand All @@ -118,7 +118,7 @@ void print(PrintableInstance gp, unsigned int terminal_x, unsigned int game_x) {
is_in_radius = 0;
}

switch (gp->cells[i]) {
switch (view->cells[i]) {
case UNTOUCHED:
if (is_in_radius) {
printw("▒");
Expand Down Expand Up @@ -181,21 +181,22 @@ void print(PrintableInstance gp, unsigned int terminal_x, unsigned int game_x) {
attroff(COLOR_PAIR(5));
break;
}
if (i == (gp->player.y * gp->width + gp->player.x))
if (i == (view->player.y * view->width + view->player.x))
attroff(A_STANDOUT);
}
printw("│\n");
print_left_margin(terminal_x, game_x);
printw("├");
int mrb = 5;
if (gp->width >= 25)
if (view->width >= 25)
mrb = 7;
for (int i = 0; i < gp->width; i++) {
if (i == mrb || i == gp->width - 8) {
for (int i = 0; i < view->width; i++) {
if (i == mrb || i == view->width - 8) {
printw("┬");
continue;
}
printw("─");
}
printw("┤\n");
print_header(view, terminal_x, game_x);
}
3 changes: 1 addition & 2 deletions src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#define DISPLAY_H
char *get_top_margin(unsigned int);
char *get_left_margin(unsigned int);
void print(PrintableInstance, unsigned int, unsigned int);
void print_header(PrintableHeaderInstance, unsigned int, unsigned int);
void print(GameView, unsigned int, unsigned int);
void print_top_margin(unsigned int, unsigned int);
void print_left_margin(unsigned int, unsigned int);
#endif
109 changes: 55 additions & 54 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,124 +121,125 @@ void deleteGameInstance(GameInstance g) {
free(g);
}

PrintableInstance createPrintable(GameInstance g) {
PrintableInstance gp = calloc(1, sizeof(struct GPrintable));
CellType *gpr = calloc(g->length, sizeof(CellType));
gp->cells = gpr;
gp->player = g->cord;
GameView createView(GameInstance g) {
GameView view = calloc(1, sizeof(struct GameView));
CellType *cells = calloc(g->length, sizeof(CellType));
view->cells = cells;
view->player = g->cord;
time_t current;
time(&current);
view->mines = g->flagstotal - g->flagsfound;
view->time = current - g->started;
view->width = g->width;
view->state = g->state;
for (int i = 0; i < g->length; i++) {
if (is_flagged(g->mines[i])) {
gp->cells[i] = FLAG;
view->cells[i] = FLAG;
} else if (is_unveiled(g->mines[i])) {
switch (g->mines[i] & 0b1111) {
case 0:
gp->cells[i] = UNVEILED;
view->cells[i] = UNVEILED;
break;
case 1:
gp->cells[i] = ONE;
view->cells[i] = ONE;
break;
case 2:
gp->cells[i] = TWO;
view->cells[i] = TWO;
break;
case 3:
gp->cells[i] = THREE;
view->cells[i] = THREE;
break;
case 4:
gp->cells[i] = FOUR;
view->cells[i] = FOUR;
break;
case 5:
gp->cells[i] = FIVE;
view->cells[i] = FIVE;
break;
case 6:
gp->cells[i] = SIX;
view->cells[i] = SIX;
break;
case 7:
gp->cells[i] = SEVEN;
view->cells[i] = SEVEN;
break;
case 8:
gp->cells[i] = EIGHT;
view->cells[i] = EIGHT;
break;
default:
break;
}
} else {
gp->cells[i] = UNTOUCHED;
view->cells[i] = UNTOUCHED;
}
}
gp->height = g->height;
gp->width = g->width;
return gp;
view->height = g->height;
view->width = g->width;
return view;
}
PrintableInstance createPrintableGameover(GameInstance g) {
PrintableInstance gp = calloc(1, sizeof(struct GPrintable));
CellType *gpr = calloc(g->length, sizeof(CellType));
gp->cells = gpr;
gp->player = g->cord;
GameView createViewGameover(GameInstance g) {
GameView view = calloc(1, sizeof(struct GameView));
CellType *cells = calloc(g->length, sizeof(CellType));
view->cells = cells;
view->player = g->cord;
time_t current;
time(&current);
view->mines = g->flagstotal - g->flagsfound;
view->time = current - g->started;
view->width = g->width;
view->state = g->state;
for (int i = 0; i < g->length; i++) {
if (is_flagged(g->mines[i])) {
if (is_mine(g->mines[i])) {
gp->cells[i] = FLAG;
view->cells[i] = FLAG;
} else {
gp->cells[i] = FALSE_FLAG;
view->cells[i] = FALSE_FLAG;
}
} else {
if (is_mine(g->mines[i])) {
gp->cells[i] = FLAG_NOT_FOUND;
view->cells[i] = FLAG_NOT_FOUND;
continue;
}
switch (g->mines[i] & 0b1111) {
case 0:
gp->cells[i] = UNVEILED;
view->cells[i] = UNVEILED;
break;
case 1:
gp->cells[i] = ONE;
view->cells[i] = ONE;
break;
case 2:
gp->cells[i] = TWO;
view->cells[i] = TWO;
break;
case 3:
gp->cells[i] = THREE;
view->cells[i] = THREE;
break;
case 4:
gp->cells[i] = FOUR;
view->cells[i] = FOUR;
break;
case 5:
gp->cells[i] = FIVE;
view->cells[i] = FIVE;
break;
case 6:
gp->cells[i] = SIX;
view->cells[i] = SIX;
break;
case 7:
gp->cells[i] = SEVEN;
view->cells[i] = SEVEN;
break;
case 8:
gp->cells[i] = EIGHT;
view->cells[i] = EIGHT;
break;
default:
break;
}
}
}
gp->cells[g->cord.y * g->width + g->cord.x] = FALSE_FLAG;
gp->height = g->height;
gp->width = g->width;
return gp;
}

PrintableHeaderInstance createPrintableHeader(GameInstance g) {
PrintableHeaderInstance gp = calloc(1, sizeof(struct GPrintableH));
time_t current;
time(&current);
gp->mines = g->flagstotal - g->flagsfound;
gp->time = current - g->started;
gp->width = g->width;
gp->state = g->state;
return gp;
view->cells[g->cord.y * g->width + g->cord.x] = FALSE_FLAG;
view->height = g->height;
view->width = g->width;
return view;
}

void deletePrintable(PrintableInstance gp) {
free(gp->cells);
free(gp);
void deleteView(GameView view) {
free(view->cells);
free(view);
}

GState g_state(GameInstance game) { return game->state; }
Expand Down
29 changes: 12 additions & 17 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ typedef enum GState {
Lost,
} GState;

//GPrint
typedef enum CellType {
UNTOUCHED,
UNVEILED,
Expand All @@ -31,25 +30,21 @@ typedef enum CellType {
EIGHT,
} CellType;

typedef struct GPrintable {
Cord player;
CellType *cells;
unsigned int width;
unsigned int height;
} *PrintableInstance;

typedef struct GPrintableH {
GState state;
unsigned int mines;
time_t time;
unsigned int width;
} *PrintableHeaderInstance;
typedef struct GameView {
CellType *cells;
Cord player;
unsigned width;
unsigned height;
GState state;
unsigned mines;
time_t time;
} *GameView;

GState g_state(GameInstance);
PrintableInstance createPrintable(GameInstance);
PrintableInstance createPrintableGameover(GameInstance);
PrintableHeaderInstance createPrintableHeader(GameInstance);
void deletePrintable(PrintableInstance);
GameView createView(GameInstance);
GameView createViewGameover(GameInstance);
void deleteView(GameView);
GameInstance createGameInstance(int, int, int);
Cord g_player_position(GameInstance);
void g_set_player_position_x(GameInstance, int);
Expand Down
Loading

0 comments on commit abb06a6

Please sign in to comment.