back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/pac.c
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-04-07 03:07:42 +0300
committerscratko <m@scratko.xyz>2024-04-07 03:07:42 +0300
commit2c2448cc94b8f17ac699814a75110411d57f3bea (patch)
tree4107fa66264cda94dd01c3bb00da29945c8d6131 /pac.c
parentbdee2852c13f6b02ec5207ded584839a3118233e (diff)
downloadpacman-2c2448cc94b8f17ac699814a75110411d57f3bea.tar.gz
pacman-2c2448cc94b8f17ac699814a75110411d57f3bea.tar.bz2
pacman-2c2448cc94b8f17ac699814a75110411d57f3bea.zip
BFS, queue files
Fixed remaining direction check for pacman (old version was commented out). Breadth First Search for red ghost. Changed switch style.
Diffstat (limited to 'pac.c')
-rw-r--r--pac.c153
1 files changed, 91 insertions, 62 deletions
diff --git a/pac.c b/pac.c
index 50256d8..1fd4b73 100644
--- a/pac.c
+++ b/pac.c
@@ -10,34 +10,36 @@ void initialize_pac(struct pacman *pac)
pac->direction = none;
}
-static enum movement_direction get_correct_path(struct pacman *pac,
- struct free_directions
- free_path,
- enum movement_direction
+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_path.left ? left : none;
- case right:
- return free_path.right ? right : none;
- case up:
- return free_path.up ? up : none;
- case down:
- return free_path.down ? down : none;
- default:
- return none;
+ 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;
}
}
-void check_remaining_direction(game_space field, struct pacman *pac,
+#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 =
+ struct free_directions free_path =
find_free_directions(field, pac->position.y, pac->position.x);
- enum movement_direction current_direction =
+ 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)
@@ -49,8 +51,35 @@ void check_remaining_direction(game_space field, struct pacman *pac,
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,
+void change_pac_direction(game_space field, struct pacman *pac, int key,
enum movement_direction *stored_direction)
{
enum state { no, yes } is_changed_direction;
@@ -58,56 +87,56 @@ void change_pac_direction(game_space field, struct pacman *pac, int key,
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(key == KEY_RIGHT && path.right) {
- pac->direction = right;
- is_changed_direction = yes;
- } else
- *stored_direction = right;
- break;
- case KEY_UP:
- if(key == KEY_UP && path.up) {
- pac->direction = up;
- is_changed_direction = yes;
- } else
- *stored_direction = up;
- break;
- case KEY_DOWN:
- if(key == KEY_DOWN && path.down) {
- pac->direction = down;
- is_changed_direction = yes;
- } else
- *stored_direction = down;
- break;
+ 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(field, pac, NULL);
+ check_remaining_direction_after_pressed_key(field, pac);
}
void make_pac_move(game_space field, struct pacman *pac)
{
- clear_symbol(field, pac->position.y, pac->position.x, pac_char);
+ eat_or_revert_symbol(field, pac->position.y, pac->position.x, pac_char);
switch(pac->direction) {
- case left:
- --pac->position.x;
- return;
- case right:
- ++pac->position.x;
- return;
- case up:
- --pac->position.y;
- return;
- case down:
- ++pac->position.y;
- return;
- default:
- return;
+ case left:
+ --pac->position.x;
+ return;
+ case right:
+ ++pac->position.x;
+ return;
+ case up:
+ --pac->position.y;
+ return;
+ case down:
+ ++pac->position.y;
+ return;
+ default:
+ return;
}
}