67 lines
1.8 KiB
C
67 lines
1.8 KiB
C
#include <curses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "world.h"
|
|
#include "game.h"
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
#define EMPTY 0
|
|
#define CAT 1
|
|
#define MOUSE 2
|
|
|
|
struct game* create_world(int width, int height, int mouse_count) {
|
|
srand(time(NULL));
|
|
|
|
struct game* game = malloc(sizeof(struct game));
|
|
game->catx = rand() % width;
|
|
game->caty = rand() % height;
|
|
for (int i = 0; i < MOUSE_COUNT; i++) {
|
|
game->mousex[i] = rand() % width;
|
|
game->mousey[i] = rand() % height;
|
|
game->mouse_state[i] = 0;
|
|
}
|
|
|
|
return game;
|
|
}
|
|
|
|
void destroy_world(struct game* game) {
|
|
free(game);
|
|
}
|
|
|
|
void move_cat(struct game* game, char direction) {
|
|
if (direction == 'w' && game->caty > 0) {
|
|
game->caty -= 1;
|
|
} else if (direction == 's' && game->caty < 9) {
|
|
game->caty += 1;
|
|
} else if (direction == 'a' && game->catx > 0) {
|
|
game->catx -= 1;
|
|
} else if (direction == 'd' && game->catx < 9) {
|
|
game->catx += 1;
|
|
}
|
|
}
|
|
|
|
void move_mouse(struct game* game, int mouse_index) {
|
|
if (game->mouse_state[mouse_index] == 0) {
|
|
int m = rand() % 4;
|
|
if (m == 0 && game->mousey[mouse_index] > 0) {
|
|
game->mousey[mouse_index] -= 1;
|
|
} else if (m == 1 && game->mousey[mouse_index] < 9) {
|
|
game->mousey[mouse_index] += 1;
|
|
} else if (m == 2 && game->mousex[mouse_index] > 0) {
|
|
game->mousex[mouse_index] -= 1;
|
|
} else if (m == 3 && game->mousex[mouse_index] < 9) {
|
|
game->mousex[mouse_index] += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
int get_living_mouse_count(struct game* game) {
|
|
int living_count = 0;
|
|
for (int i = 0; i < MOUSE_COUNT; i++) {
|
|
if (game->mousex[i] >= 0 && game->mouse_state[i] == 0) {
|
|
living_count++;
|
|
}
|
|
}
|
|
return living_count;
|
|
} |