#include "field.h" #include "ghosts.h" #include "pac.h" #include "queue.h" #include #include #include enum { timeout_duration = 0, sleep_duration = 190000, key_escape = 27, count_get_out_moves = 5 }; static void pathfinder_stage(game_space field, struct pacman pac, struct ghost_type *red_ghost, struct ghost_type *pink_ghost, struct ghost_type *blue_ghost, struct ghost_type *orange_ghost) { void (*search_method)(game_space, struct ghost_type*, struct coordinates); enum intersection_type intersection; int color_priority; struct ghost_type *current_ghost; struct coordinates target_point; for(color_priority = 0; color_priority <= orange; ++color_priority) { switch(color_priority) { case red: current_ghost = red_ghost; target_point = pac.position; search_method = breadth_first_search; break; case pink: current_ghost = pink_ghost; target_point = identify_target_in_front(pac, pink_shift); search_method = compute_distance_between_points; break; case blue: current_ghost = blue_ghost; target_point = identify_blue_target(pac, red_ghost); search_method = compute_distance_between_points; break; case orange: current_ghost = orange_ghost; target_point = pac.position; search_method = breadth_first_search; break; } intersection = get_intersection(field, current_ghost); if(intersection != direct_path) redirect(field, intersection, current_ghost, target_point, search_method); } } static void initialize_ghosts(struct ghost_type *red_ghost, struct ghost_type *pink_ghost, struct ghost_type *blue_ghost, struct ghost_type *orange_ghost) { initialize_ghost(red_ghost, red); initialize_ghost(pink_ghost, pink); initialize_ghost(blue_ghost, blue); initialize_ghost(orange_ghost, orange); } static void ncurses_params() { initscr(); start_color(); cbreak(); noecho(); curs_set(0); keypad(stdscr, 1); timeout(timeout_duration); start_color(); } int main() { game_space field = NULL; struct ghost_type red_ghost, pink_ghost, blue_ghost, orange_ghost; struct pacman pac; struct queue eaten_coins; int key, get_out_stage; enum movement_direction stored_direction; stored_direction = none; get_out_stage = count_get_out_moves; ncurses_params(); field = get_new_field(); print_field(field); initialize_ghosts(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); initialize_pac(&pac); queue_init(&eaten_coins); display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); display_character(pac.position.y, pac.position.x, pac_char); usleep(sleep_duration); while((key = getch()) != key_escape) { if(key != ERR) change_pac_direction(field, &pac, key, &stored_direction); else check_remaining_direction(field, &pac, &stored_direction); if(get_out_stage) pull_out_ghosts(&get_out_stage, &red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); else pathfinder_stage(field, pac, &red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); make_pac_move(field, &pac, &eaten_coins); make_ghost_moves(field, &red_ghost, &pink_ghost, &blue_ghost, &orange_ghost, &eaten_coins); display_character(pac.position.y, pac.position.x, pac_char); display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); usleep(sleep_duration); } queue_clear(&eaten_coins); endwin(); return 0; }