82 lines
2.0 KiB
C
82 lines
2.0 KiB
C
#include <curses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "world.h"
|
|
#include "game.h"
|
|
|
|
void init_game(struct game *game)
|
|
{
|
|
// Initialize random number generator
|
|
srand(time(NULL));
|
|
|
|
// Initialize cat position
|
|
game->catx = rand() % 10;
|
|
game->caty = rand() % 10;
|
|
|
|
// Initialize mouse positions
|
|
for (int i = 0; i < MOUSE_COUNT; i++) {
|
|
game->mousex[i] = rand() % 10;
|
|
game->mousey[i] = rand() % 10;
|
|
game->mouse_state[i] = 0; // Mouse is alive
|
|
}
|
|
}
|
|
|
|
void update_game(struct game *game, int key)
|
|
{
|
|
// Move cat according to key
|
|
if (key == 'w') {
|
|
game->caty--;
|
|
} else if (key == 's') {
|
|
game->caty++;
|
|
} else if (key == 'a') {
|
|
game->catx--;
|
|
} else if (key == 'd') {
|
|
game->catx++;
|
|
}
|
|
|
|
// Check if any mouse is caught by the cat
|
|
for (int i = 0; i < MOUSE_COUNT; i++) {
|
|
if (game->mouse_state[i] == 0 && game->mousex[i] == game->catx && game->mousey[i] == game->caty) {
|
|
game->mouse_state[i] = 1; // Mouse is eaten
|
|
}
|
|
}
|
|
|
|
// Move alive mice randomly
|
|
for (int i = 0; i < MOUSE_COUNT; i++) {
|
|
if (game->mouse_state[i] == 0) { // Mouse is alive
|
|
int m = rand() % 4;
|
|
if (m == 0) {
|
|
game->mousey[i]--;
|
|
} else if (m == 1) {
|
|
game->mousey[i]++;
|
|
} else if (m == 2) {
|
|
game->mousex[i]--;
|
|
} else if (m == 3) {
|
|
game->mousex[i]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void draw_game(struct game *game)
|
|
{
|
|
// Draw cat
|
|
printf("Cat position: (%d, %d)\n", game->catx, game->caty);
|
|
|
|
// Draw alive mice
|
|
int alive_count = 0;
|
|
for (int i = 0; i < MOUSE_COUNT; i++) {
|
|
if (game->mouse_state[i] == 0) { // Mouse is alive
|
|
printf("Mouse %d position: (%d, %d)\n", i, game->mousex[i], game->mousey[i]);
|
|
alive_count++;
|
|
}
|
|
}
|
|
|
|
if (alive_count == 0) {
|
|
printf("All mice are eaten! Game over.\n");
|
|
} else {
|
|
printf("Number of alive mice: %d\n", alive_count);
|
|
}
|
|
}
|
|
|