From 2c2448cc94b8f17ac699814a75110411d57f3bea Mon Sep 17 00:00:00 2001 From: scratko Date: Sun, 7 Apr 2024 03:07:42 +0300 Subject: BFS, queue files Fixed remaining direction check for pacman (old version was commented out). Breadth First Search for red ghost. Changed switch style. --- field.c | 94 ++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 42 deletions(-) (limited to 'field.c') diff --git a/field.c b/field.c index df61097..5bcb813 100644 --- a/field.c +++ b/field.c @@ -54,7 +54,7 @@ game_space get_new_field() static int is_has_point(int i, int j) { - return !((i == 12 && j == 9) || (i == 12 && j == 18) || + return !((i == 12 && j == 9) || (i == 12 && j == 18) || (i == 14 && j == 18) || (i == 15 && j == 9) || (i == 17 && j == 9) || (i == 17 && j == 18)); } @@ -69,30 +69,30 @@ void print_field(game_space field) symbol = field[i][j]; move(i, j); switch(symbol) { - case '1': - case '2': - case '3': - if(is_has_point(i, j)) - addch('.'); - else - addch(' '); - break; - case '/': - addch('/'); - break; - case '.': + case '1': + case '2': + case '3': + if(is_has_point(i, j)) addch('.'); - break; - case '#': - addch('#'); - break; + else + addch(' '); + break; + case '/': + addch('/'); + break; + case '.': + addch('.'); + break; + case '#': + addch('#'); + break; } refresh(); } } } -void display_character(int y, int x, int symbol) +void display_character(int y, int x, enum select_character symbol) { move(y, x); addch(symbol); @@ -104,28 +104,31 @@ void display_ghosts_on_field(struct ghost_type *red_ghost, struct ghost_type *blue_ghost, struct ghost_type *orange_ghost) { - display_character(red_ghost->position.y, red_ghost->position.x, '&'); - display_character(pink_ghost->position.y, pink_ghost->position.x, '&'); - display_character(blue_ghost->position.y, blue_ghost->position.x, '&'); - display_character(orange_ghost->position.y, orange_ghost->position.x, '&'); + display_character(red_ghost->position.y, red_ghost->position.x, ghost_char); + display_character(pink_ghost->position.y, pink_ghost->position.x, + ghost_char); + display_character(blue_ghost->position.y, blue_ghost->position.x, + ghost_char); + display_character(orange_ghost->position.y, orange_ghost->position.x, + ghost_char); } -void clear_symbol(game_space field, int y, int x, +void eat_or_revert_symbol(game_space field, int y, int x, enum select_character character) { int symbol = field[y][x]; move(y, x); if(character == ghost_char) { switch(symbol) { - case '#': - addch('#'); - break; - case '.': - addch('.'); - break; - case ' ': - addch(' '); - break; + case '#': + addch('#'); + break; + case '.': + addch('.'); + break; + case ' ': + addch(' '); + break; } } else if(character == pac_char) @@ -133,7 +136,7 @@ void clear_symbol(game_space field, int y, int x, refresh(); } -enum intersection_type get_intersection(const game_space field, +enum intersection_type get_intersection(const game_space field, struct ghost_type *ghost) { int y, x, symbol; @@ -141,18 +144,19 @@ enum intersection_type get_intersection(const game_space field, x = ghost->position.x; symbol = field[y][x]; switch(symbol) { - case one_path: - return one_path; - case two_paths: - return two_paths; - case three_paths: - return three_paths; - default: - return direct_path; + case one_path: + return one_path; + case two_paths: + return two_paths; + case three_paths: + return three_paths; + default: + return direct_path; } } -struct free_directions find_free_directions(game_space field, int y, int x) +struct free_directions find_free_directions(const game_space field, int y, + int x) { struct free_directions found_paths; found_paths.left = field[y][x-1] != '/' && field[y][x-1] != '#' ? 1 : 0; @@ -161,3 +165,9 @@ struct free_directions find_free_directions(game_space field, int y, int x) found_paths.down = field[y+1][x] != '/' && field[y+1][x] != '#' ? 1 : 0; return found_paths; } + +int is_obstacle(const game_space field, int x, int y) +{ + int symbol = field[y][x]; + return symbol == door || symbol == block; +} -- cgit v1.2.3