From 8fe2319e189b4d830a892b8c686d08cff5556962 Mon Sep 17 00:00:00 2001 From: scratko Date: Thu, 31 Jul 2025 03:55:59 +0300 Subject: Add README and revise distance metric Added many inline comments for clarity. Introduced initial version of README with gameplay details, controls, and AI logic. Replaced Euclidean distance with Manhattan distance in standard_distance() to better match grid-based movement without diagonal traversal. --- ghosts.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'ghosts.c') diff --git a/ghosts.c b/ghosts.c index 72e4f81..87e2350 100644 --- a/ghosts.c +++ b/ghosts.c @@ -157,6 +157,11 @@ struct coordinates identify_blue_target(struct pacman pac, return result_point; } +/* + * determining the target point for the pink ghost (4 cells in front of + * Pac-Man). + * Also involved in determining the midpoint for the blue ghost + */ struct coordinates identify_target_in_front(struct pacman pac, int shift) { int x, y; @@ -243,6 +248,10 @@ static int is_bfs_search_for_orange(enum search_flag *current_width, return *move_count > orange_search_limit; } +/* + * The search is performed from Pac-Man to the ghost so that at the end + * of the algorithm we immediately know the closest point to the ghost. + */ void breadth_first_search(game_space field, struct ghost_type *ghost, struct coordinates target_point) { @@ -283,6 +292,10 @@ void breadth_first_search(game_space field, struct ghost_type *ghost, if(ghost->color == orange) { if(!is_bfs_search_for_orange(¤t_width, &move_count)) { + /* + * Instead of two_paths, it could be anything except + * direct_path + */ redirect(field, two_paths, ghost, ghost->home_position, compute_distance_between_points); @@ -315,10 +328,14 @@ void breadth_first_search(game_space field, struct ghost_type *ghost, static int standard_distance(int x1, int y1, int x2, int y2) { int dx, dy; + dx = abs(x1 - x2); - dx = dx > field_width ? field_width - dx : dx; + if (dx > field_width / 2) + dx = field_width - dx; + dy = abs(y1 - y2); - return sqrt(dx*dx + dy*dy); + /* Manhattan distance */ + return dx + dy; } static int is_minimal_distance(int min_distance, int tmp_distance, -- cgit v1.2.3