#include "pac.h" #include "ncurses.h" #include "queue.h" void initialize_pac(struct pacman *pac) { pac->lives = max_live; pac->coins_eaten = 0; pac->position.y = pac_y; pac->position.x = pac_x; pac->direction = none; } static enum movement_direction get_matching_for_directions(game_space field, struct pacman *pac, enum movement_direction direction) { struct free_directions free_paths = find_free_directions(field, pac->position.y, pac->position.x); switch(direction) { case left: return free_paths.left ? left : none; case right: return free_paths.right ? right : none; case up: return free_paths.up ? up : none; case down: return free_paths.down ? down : none; default: return none; } } #if 0 void check_remaining_direction(game_space field, struct pacman *pac, enum movement_direction *stored_direction) { enum movement_direction temp_direction; struct free_directions free_path = find_free_directions(field, pac->position.y, pac->position.x); enum movement_direction current_direction = stored_direction ? *stored_direction : pac->direction; temp_direction = get_correct_path(pac, free_path, current_direction); if(temp_direction != none) pac->direction = temp_direction; else { current_direction = pac->direction; pac->direction = get_correct_path(pac, free_path, current_direction); } if(stored_direction) *stored_direction = none; } #endif void check_remaining_direction(game_space field, struct pacman *pac, enum movement_direction *stored_direction) { enum movement_direction matching_result; if(*stored_direction == none) pac->direction = get_matching_for_directions(field, pac, pac->direction); else { matching_result = get_matching_for_directions(field, pac, *stored_direction); if(matching_result != none) pac->direction = matching_result; else pac->direction = get_matching_for_directions(field, pac, pac->direction); *stored_direction = none; } } static void check_remaining_direction_after_pressed_key(game_space field, struct pacman *pac) { pac->direction = get_matching_for_directions(field, pac, pac->direction); } void change_pac_direction(game_space field, struct pacman *pac, int key, enum movement_direction *stored_direction) { enum state { no, yes } is_changed_direction; is_changed_direction = no; struct free_directions path = find_free_directions(field, pac->position.y, pac->position.x); switch(key) { case KEY_LEFT: if(path.left) { pac->direction = left; is_changed_direction = yes; } else *stored_direction = left; break; case KEY_RIGHT: if(path.right) { pac->direction = right; is_changed_direction = yes; } else *stored_direction = right; break; case KEY_UP: if(path.up) { pac->direction = up; is_changed_direction = yes; } else *stored_direction = up; break; case KEY_DOWN: if(path.down) { pac->direction = down; is_changed_direction = yes; } else *stored_direction = down; break; } if(is_changed_direction == no) check_remaining_direction_after_pressed_key(field, pac); } static int is_coin_symbol(int symbol) { return symbol == coin || symbol == one_path || symbol == two_paths || symbol == three_paths; } static int check_coin(game_space field, struct coordinates position, struct queue *eaten_coins) { int x, y; x = position.x; y = position.y; return is_coin_symbol(field[y][x]) && field_has_coin(x, y) && !queue_consists_point(eaten_coins, position); } void make_pac_move(game_space field, struct pacman *pac, struct queue *eaten_coins) { clear_or_revert_symbol(field, pac->position, pac_char, eaten_coins); switch(pac->direction) { case left: --pac->position.x; break; case right: ++pac->position.x; break; case up: --pac->position.y; break; case down: ++pac->position.y; break; default: return; } if(check_coin(field, pac->position, eaten_coins)) queue_push(eaten_coins, &pac->position); change_point_if_outside_tunnel(&pac->position); }