From 15bb936c1591370f4e8e1b50397712b5803b9a8a Mon Sep 17 00:00:00 2001 From: Anzhelika Nikolaieva Date: Sun, 23 Apr 2023 11:34:22 +0000 Subject: [PATCH] Update 'a3/game.c' --- a3/game.c | 170 +++++++++++++++++++----------------------------------- 1 file changed, 58 insertions(+), 112 deletions(-) diff --git a/a3/game.c b/a3/game.c index f3a0d34..f5a5b90 100644 --- a/a3/game.c +++ b/a3/game.c @@ -3,119 +3,65 @@ #include #include "world.h" #include "game.h" +#include +#include - void* init_game(struct game *game){ - // Allocate memory for the state - struct game* st = calloc(1,(sizeof(struct game))); - // Initialize state - st->mousex = 5; - st->mousey = 5; - st->catx = 0; - st->caty = 0; - st->catx_position = 15; - st->caty_position = 15; - // Store pointer to the state to the world variable - return st; + #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; } -// Step is called in a loop once in interval. -// It should modify the state and draw it. -int game_event(struct event* event,void* game){ - // Get state pointer - struct game* state = game; - char msg[200]; - sprintf(msg,"%d",event->type); - set_message(msg,10,0); - if ( event->type == EVENT_ESC){ - // Non zero means finish the loop and stop the game. - return 1; - } - // Read game variable and update the eventstate - - void update_game(struct game *game, int key){ -// Move the cat based on the pressed key - switch(key) { - case 'w': - game->caty -= 1; - break; - case 's': - game->caty += 1; - break; - case 'a': - game->catx -= 1; - break; - case 'd': - game->catx += 1; - break; - } - - // Is mouse caught ? - if ((state->caty_position == state->mousey) && (state->catx_position == state->mousex)){ - clear_screen(); - set_message("HAM",12,13); - return 0; - } - else if(event->type == EVENT_TIMEOUT) { - // Move cat - //state->catx_position += state->catx; - //state->caty_position += state->caty; - int cx = state->catx_position + state->catx; - int cy = state->caty_position + state->caty; - if (cx < 0 || cy < 0 || cx > event->width || cy > event->height){ - - } - else { - state->catx_position = cx; - state->caty_position = cy; - } - - //state->catx_position += state->catx; - //state->caty_position += state->caty; - //state->caty_position += state->caty; - // random mouse movement - int m = rand() % 6; - if (m == 0){ - state->mousey -= 1; - } - else if (m == 1){ - state->mousey += 1; - } - else if (m == 2){ - state->mousex -= 1; - } - else if (m == 3){ - state->mousex += 1; - } - // Je myska mimo plochy - } - else if (event->type == EVENT_KEY){ - // Move cat according to keyboard - if ( event->key == KEY_UP){ - state->catx = 0; - state->caty = -1; - } - else if ( event->key == KEY_DOWN){ - state->catx = 0; - state->caty = 1; - } - else if ( event->key == KEY_LEFT){ - state->catx = -1; - state->caty = 0; - } - else if ( event->key == KEY_RIGHT){ - state->catx = +1; - state->caty = 0; - } - } - // Draw world state - // - // Draw cat - clear_screen(); - set_color_cell('c',state->catx_position,state->caty_position,COLOR_YELLOW,COLOR_RED); - set_color_cell('-',state->catx_position-1,state->caty_position,COLOR_YELLOW,COLOR_GREEN); - //set_cell('c',state->catx_position,state->caty_position); - // Draw mouse - set_cell('m',state->mousex,state->mousey); - set_message( state->message,1,0); - return 0; +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; +} \ No newline at end of file