back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-04-07 22:40:47 +0300
committerscratko <m@scratko.xyz>2024-04-07 22:40:47 +0300
commit155a3c5f91c7a3bd89febfeb9927478961f5ee28 (patch)
tree1953cd610d4a06c51aaedb728edd6131a4fa426a
parent2c2448cc94b8f17ac699814a75110411d57f3bea (diff)
downloadpacman-155a3c5f91c7a3bd89febfeb9927478961f5ee28.tar.gz
pacman-155a3c5f91c7a3bd89febfeb9927478961f5ee28.tar.bz2
pacman-155a3c5f91c7a3bd89febfeb9927478961f5ee28.zip
Tracking coins eaten
The initialization of ncurses parameters and ghosts was put into functions. The order of movements has been changed: now pacman moves first. Accounting for eaten coins. Correct coin erasure.
-rw-r--r--field.c35
-rw-r--r--field.h13
-rw-r--r--ghosts.c20
-rw-r--r--ghosts.h4
-rw-r--r--pac.c36
-rw-r--r--pac.h7
-rw-r--r--pacman.c54
-rw-r--r--queue.c2
-rw-r--r--queue.h2
9 files changed, 117 insertions, 56 deletions
diff --git a/field.c b/field.c
index 5bcb813..a795f8e 100644
--- a/field.c
+++ b/field.c
@@ -1,5 +1,6 @@
#include "field.h"
#include "ghosts.h"
+#include "queue.h"
#include <ncurses.h>
#include <stdlib.h>
@@ -52,11 +53,11 @@ game_space get_new_field()
return field;
}
-static int is_has_point(int i, int j)
+int field_has_coin(int x, int y)
{
- 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));
+ return !((x == 12 && y == 9) || (x == 12 && y == 18) ||
+ (x == 14 && y == 18) || (x == 15 && y == 9) ||
+ (x == 17 && y == 9) || (x == 17 && y == 18));
}
void print_field(game_space field)
@@ -72,7 +73,7 @@ void print_field(game_space field)
case '1':
case '2':
case '3':
- if(is_has_point(i, j))
+ if(field_has_coin(i, j))
addch('.');
else
addch(' ');
@@ -113,18 +114,32 @@ void display_ghosts_on_field(struct ghost_type *red_ghost,
ghost_char);
}
-void eat_or_revert_symbol(game_space field, int y, int x,
- enum select_character character)
+void clear_or_revert_symbol(game_space field, struct coordinates position,
+ enum select_character character,
+ struct queue *eaten_coins)
{
+ int x, y;
+ x = position.x;
+ y = position.y;
int symbol = field[y][x];
move(y, x);
if(character == ghost_char) {
switch(symbol) {
- case '#':
+ case one_path:
+ case two_paths:
+ case three_paths:
+ if(field_has_coin(y, x))
+ queue_consists_point(eaten_coins, position) ?
+ addch(' ') : addch('.');
+ else
+ addch(' ');
+ break;
+ case door:
addch('#');
break;
- case '.':
- addch('.');
+ case coin:
+ queue_consists_point(eaten_coins, position) ?
+ addch(' ') : addch('.');
break;
case ' ':
addch(' ');
diff --git a/field.h b/field.h
index 7ac53e8..4681267 100644
--- a/field.h
+++ b/field.h
@@ -5,7 +5,8 @@ enum {
field_width = 28,
field_height = 29,
door = '#',
- block = '/'
+ block = '/',
+ coin = '.'
};
enum intersection_type {
@@ -40,14 +41,18 @@ void display_ghosts_on_field(struct ghost_type *red_ghost,
struct ghost_type *blue_ghost,
struct ghost_type *orange_ghost);
-void eat_or_revert_symbol(game_space field, int y, int x,
- enum select_character character);
+struct queue;
+struct coordinates;
+void clear_or_revert_symbol(game_space field, struct coordinates position,
+ enum select_character character,
+ struct queue *eaten_coins);
enum intersection_type get_intersection(const game_space field,
- struct ghost_type *ghost);
+ struct ghost_type *ghost);
struct free_directions find_free_directions(game_space field, int y, int x);
int is_obstacle(game_space field, int x, int y);
+int field_has_coin(int x, int y);
#endif
diff --git a/ghosts.c b/ghosts.c
index 7a320ec..0029702 100644
--- a/ghosts.c
+++ b/ghosts.c
@@ -92,16 +92,16 @@ void make_ghost_moves(game_space field,
struct ghost_type *red_ghost,
struct ghost_type *pink_ghost,
struct ghost_type *blue_ghost,
- struct ghost_type *orange_ghost)
+ struct ghost_type *orange_ghost,
+ struct queue *eaten_coins)
{
- eat_or_revert_symbol(field, red_ghost->position.y, red_ghost->position.x,
- ghost_char);
- eat_or_revert_symbol(field, pink_ghost->position.y, pink_ghost->position.x,
- ghost_char);
- eat_or_revert_symbol(field, blue_ghost->position.y, blue_ghost->position.x,
- ghost_char);
- eat_or_revert_symbol(field, orange_ghost->position.y,
- orange_ghost->position.x, ghost_char);
+ clear_or_revert_symbol(field, red_ghost->position, ghost_char, eaten_coins);
+ clear_or_revert_symbol(field, pink_ghost->position, ghost_char,
+ eaten_coins);
+ clear_or_revert_symbol(field, blue_ghost->position, ghost_char,
+ eaten_coins);
+ clear_or_revert_symbol(field, orange_ghost->position, ghost_char,
+ eaten_coins);
change_position(red_ghost, red_ghost->direction);
change_position(pink_ghost, pink_ghost->direction);
change_position(blue_ghost, blue_ghost->direction);
@@ -189,7 +189,7 @@ void breadth_first_search(game_space field, struct ghost_type *ghost,
near_point = get_near_point(consider_point, dx, dy);
if(is_obstacle(field, near_point.x, near_point.y))
continue;
- if(is_consist_point(&next_points, near_point))
+ if(queue_consists_point(&next_points, near_point))
continue;
#if 0
init_pair(1, COLOR_RED, COLOR_BLACK);
diff --git a/ghosts.h b/ghosts.h
index 08c2367..7830a97 100644
--- a/ghosts.h
+++ b/ghosts.h
@@ -47,11 +47,13 @@ void pull_out_ghosts(int *get_out_stage,
struct ghost_type *blue_ghost,
struct ghost_type *orange_ghost);
+struct queue;
void make_ghost_moves(game_space field,
struct ghost_type *red_ghost,
struct ghost_type *pink_ghost,
struct ghost_type *blue_ghost,
- struct ghost_type *orange_ghost);
+ struct ghost_type *orange_ghost,
+ struct queue *eaten_coins);
struct pacman;
void redirect(game_space field, enum intersection_type paths,
diff --git a/pac.c b/pac.c
index 1fd4b73..241609b 100644
--- a/pac.c
+++ b/pac.c
@@ -1,10 +1,11 @@
#include "pac.h"
#include "ncurses.h"
+#include "queue.h"
void initialize_pac(struct pacman *pac)
{
pac->lives = max_live;
- pac->points_eaten = 0;
+ pac->coins_eaten = 0;
pac->position.y = pac_y;
pac->position.x = pac_x;
pac->direction = none;
@@ -120,23 +121,44 @@ void change_pac_direction(game_space field, struct pacman *pac, int key,
check_remaining_direction_after_pressed_key(field, pac);
}
-void make_pac_move(game_space field, struct pacman *pac)
+static int is_coin_symbol(int symbol)
{
- eat_or_revert_symbol(field, pac->position.y, pac->position.x, pac_char);
+ return
+ symbol == coin || symbol == one_path || symbol == two_paths ||
+ symbol == three_paths;
+}
+
+static int check_coin(game_space field, struct coordinates position,
+ struct queue *eaten_coins)
+{
+ int x, y;
+ x = position.x;
+ y = position.y;
+ return
+ is_coin_symbol(field[y][x]) && field_has_coin(x, y) &&
+ !queue_consists_point(eaten_coins, position);
+}
+
+void make_pac_move(game_space field, struct pacman *pac,
+ struct queue *eaten_coins)
+{
+ clear_or_revert_symbol(field, pac->position, pac_char, eaten_coins);
switch(pac->direction) {
case left:
--pac->position.x;
- return;
+ break;
case right:
++pac->position.x;
- return;
+ break;
case up:
--pac->position.y;
- return;
+ break;
case down:
++pac->position.y;
- return;
+ break;
default:
return;
}
+ if(check_coin(field, pac->position, eaten_coins))
+ queue_push(eaten_coins, &pac->position);
}
diff --git a/pac.h b/pac.h
index e3c5ffa..dfa6a86 100644
--- a/pac.h
+++ b/pac.h
@@ -11,7 +11,7 @@ enum {
struct pacman {
char lives;
- unsigned char points_eaten;
+ unsigned char coins_eaten;
struct coordinates position;
enum movement_direction direction;
};
@@ -23,7 +23,8 @@ void change_pac_direction(game_space field, struct pacman *pac, int key,
void check_remaining_direction(game_space field, struct pacman *pac,
enum movement_direction *stored_direction);
-
-void make_pac_move(game_space field, struct pacman *pac);
+struct queue;
+void make_pac_move(game_space field, struct pacman *pac,
+ struct queue *eaten_coins);
#endif
diff --git a/pacman.c b/pacman.c
index 5b5b50a..9346055 100644
--- a/pacman.c
+++ b/pacman.c
@@ -25,15 +25,19 @@ static void pathfinder_stage(game_space field, struct pacman pac,
redirect(field, intersection, red_ghost, pac, breadth_first_search);
}
-int main()
+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()
{
- game_space field = NULL;
- struct ghost_type red_ghost, pink_ghost, blue_ghost, orange_ghost;
- struct pacman pac;
- int key, get_out_stage;
- enum movement_direction stored_direction;
- stored_direction = none;
- get_out_stage = count_get_out_moves;
initscr();
start_color();
cbreak();
@@ -42,36 +46,48 @@ int main()
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_ghost(&red_ghost, red);
- initialize_ghost(&pink_ghost, pink);
- initialize_ghost(&blue_ghost, blue);
- initialize_ghost(&orange_ghost, orange);
+ 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);
- if(key != ERR)
- change_pac_direction(field, &pac, key, &stored_direction);
- else
- check_remaining_direction(field, &pac, &stored_direction);
+ make_pac_move(field, &pac, &eaten_coins);
make_ghost_moves(field, &red_ghost, &pink_ghost, &blue_ghost,
- &orange_ghost);
- make_pac_move(field, &pac);
+ &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);
- display_character(pac.position.y, pac.position.x, pac_char);
usleep(sleep_duration);
}
+ queue_clear(&eaten_coins);
endwin();
return 0;
}
diff --git a/queue.c b/queue.c
index dd3dc1b..9e4d01e 100644
--- a/queue.c
+++ b/queue.c
@@ -43,7 +43,7 @@ int equal_points(struct coordinates first_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)
+int queue_consists_point(const struct queue *q, struct coordinates target_point)
{
enum { false, true };
struct item *tmp_item = q->first;
diff --git a/queue.h b/queue.h
index 413b76f..97a0e77 100644
--- a/queue.h
+++ b/queue.h
@@ -19,7 +19,7 @@ struct coordinates queue_front(const struct queue *q);
int empty(const struct queue *q);
void pop(struct queue *q);
int equal_points(struct coordinates tmp_point, struct coordinates target_point);
-int is_consist_point(const struct queue *q, struct coordinates target_point);
+int queue_consists_point(const struct queue *q, struct coordinates target_point);
void queue_clear(struct queue *q);
#endif