#include "game.h" #include "world.h" #include #include #include #include #include #define MOUSE_COUNT 5 void* init_game(void) { struct game* g = calloc(1, sizeof(struct game)); if (!g) return NULL; g->cat_x = 5; g->cat_y = 5; for (int i = 0; i < MOUSE_COUNT; i++) { g->mouse_x[i] = 10 + rand() % (COLS - 12); g->mouse_y[i] = 2 + rand() % (LINES - 4); g->mouse_alive[i] = 1; } g->caught = 0; snprintf(g->message, sizeof(g->message), "Catch all mice (%d left)", MOUSE_COUNT); return g; } int game_event(struct event* e, void* state) { struct game* g = (struct game*)state; if (!g) return 0; if (e->type == EVENT_KEY) { if (e->key == KEY_UP) g->cat_y--; if (e->key == KEY_DOWN) g->cat_y++; if (e->key == KEY_LEFT) g->cat_x--; if (e->key == KEY_RIGHT) g->cat_x++; } if (g->cat_x < 1) g->cat_x = 1; if (g->cat_y < 1) g->cat_y = 1; if (g->cat_x >= COLS - 1) g->cat_x = COLS - 2; if (g->cat_y >= LINES - 1) g->cat_y = LINES - 2; for (int i = 0; i < MOUSE_COUNT; i++) { if (!g->mouse_alive[i]) continue; g->mouse_x[i] += (rand() % 3) - 1; g->mouse_y[i] += (rand() % 3) - 1; if (g->mouse_x[i] < 1) g->mouse_x[i] = 1; if (g->mouse_y[i] < 1) g->mouse_y[i] = 1; if (g->mouse_x[i] >= COLS - 1) g->mouse_x[i] = COLS - 2; if (g->mouse_y[i] >= LINES - 1) g->mouse_y[i] = LINES - 2; if (g->mouse_x[i] == g->cat_x && g->mouse_y[i] == g->cat_y) { g->mouse_alive[i] = 0; g->caught++; } } clear_screen(); box(stdscr, 0, 0); set_color_cell('C', g->cat_x, g->cat_y, COLOR_CYAN, COLOR_BLACK); for (int i = 0; i < MOUSE_COUNT; i++) { if (g->mouse_alive[i]) { set_color_cell('M', g->mouse_x[i], g->mouse_y[i], COLOR_RED, COLOR_BLACK); } } if (g->caught == MOUSE_COUNT) { clear_screen(); box(stdscr, 0, 0); const char* msg = "Congratulations! You won!"; int len = strlen(msg); int cx = (COLS - len) / 2; int cy = LINES / 2; for (int i = 0; i < len; i++) { set_color_cell(msg[i], cx + i, cy, COLOR_YELLOW, COLOR_BLACK); } refresh(); napms(4000); endwin(); exit(0); } snprintf(g->message, sizeof(g->message), "Caught: %d / %d", g->caught, MOUSE_COUNT); set_message(g->message, 2, 0); return 0; }