back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/queue.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 /queue.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 'queue.c')
-rw-r--r--queue.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/queue.c b/queue.c
new file mode 100644
index 0000000..dd3dc1b
--- /dev/null
+++ b/queue.c
@@ -0,0 +1,70 @@
+#include "queue.h"
+#include <stdlib.h>
+
+void queue_init(struct queue *q)
+{
+ q->first = NULL;
+ q->last = NULL;
+}
+
+void queue_push(struct queue *q, const struct coordinates *data)
+{
+ struct item *new_element = malloc(sizeof(struct item));
+ new_element->point = *data;
+ new_element->next = NULL;
+ if(!q->first)
+ q->first = q->last = new_element;
+ else {
+ q->last->next = new_element;
+ q->last = q->last->next;
+ }
+}
+
+int empty(const struct queue *q)
+{
+ return q->first ? 0 : 1;
+}
+
+struct coordinates queue_front(const struct queue *q)
+{
+ return q->first->point;
+}
+
+void pop(struct queue *q)
+{
+ struct item *tmp = q->first;
+ q->first = q->first->next;
+ free(tmp);
+}
+
+int equal_points(struct coordinates first_point,
+ struct coordinates second_point)
+{
+ return first_point.x == second_point.x && first_point.y == second_point.y;
+}
+
+int is_consist_point(const struct queue *q, struct coordinates target_point)
+{
+ enum { false, true };
+ struct item *tmp_item = q->first;
+ struct coordinates tmp_point;
+ while(tmp_item != NULL) {
+ tmp_point = tmp_item->point;
+ if(equal_points(tmp_point, target_point))
+ return true;
+ tmp_item = tmp_item->next;
+ }
+ return false;
+}
+
+void queue_clear(struct queue *q)
+{
+ struct item *tmp;
+ while(q->first != NULL) {
+ tmp = q->first;
+ q->first = q->first->next;
+ free(tmp);
+ }
+ q->first = NULL;
+ q->last = NULL;
+}