back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/pacman.c
blob: c3225b25d082622bc99521b95ee3b4ddb5542b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "field.h"
#include "ghosts.h"
#include "pac.h"
#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>

enum { 
    timeout_duration    = 0,
    sleep_duration      = 190000,
    key_escape          = 27,
    count_get_out_moves = 5
};

static void pathfinder_stage(game_space field, struct ghost_type *red_ghost, 
                             struct ghost_type *pink_ghost, 
                             struct ghost_type *blue_ghost,
                             struct ghost_type *orange_ghost)
{
    enum intersection_type intersection;
    intersection = get_intersection(field, red_ghost);
    if(intersection != direct_path)
        redirect(field, intersection, red_ghost, breadth_first_search);
}

int main()
{
    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();
    cbreak();
    noecho();
    curs_set(0);
    keypad(stdscr, 1);
    timeout(timeout_duration);
    start_color();
    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_pac(&pac);
    display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost, 
                            &orange_ghost);
    display_character(pac.position.y, pac.position.x, 'C');
    usleep(sleep_duration);
    while((key = getch()) != key_escape) {
        if(get_out_stage)
            pull_out_ghosts(&get_out_stage, &red_ghost, &pink_ghost, 
                            &blue_ghost, &orange_ghost);
        else
            pathfinder_stage(field, &red_ghost, &pink_ghost, &blue_ghost, 
                             &orange_ghost);
        if(key != ERR) 
            change_pac_direction(field, &pac, key, &stored_direction);
        else {
            if(stored_direction != none)
                check_remaining_direction(field, &pac, &stored_direction);
            else
                check_remaining_direction(field, &pac, NULL);
        }
        make_ghost_moves(field, &red_ghost, &pink_ghost, &blue_ghost, 
                         &orange_ghost);
        make_pac_move(field, &pac);
        display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost, 
                                &orange_ghost);
        display_character(pac.position.y, pac.position.x, 'C');
        usleep(sleep_duration);
    }
    endwin();
    return 0;
}