back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/field.c
blob: a795f8e5b623b5d4cf14b017437fe777e1907ec8 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "field.h"
#include "ghosts.h"
#include "queue.h"
#include <ncurses.h>
#include <stdlib.h>

static const char field_sample[field_height][field_width] = {
    {"////////////////////////////"},
    {"/1....2.....1//1.....2....1/"},
    {"/.////./////.//./////.////./"},
    {"/./  /./   /.//./   /./  /./"},
    {"/./  /./   /.//./   /./  /./"},
    {"/.////./////.//./////.////./"},
    {"/2....3..2..2..2..2..3....2/"},
    {"/.////.//.////////.//.////./"},
    {"/.////.//.////////.//.////./"},
    {"/1....2//1..1//1..1//2....1/"},
    {"//////.///// // /////.//////"},
    {"     /.///// // /////./     "},
    {"     /.//1        1//./     "},
    {"     /.// ///##/// //./     "},
    {"//////.// /      /2  2//////"},
    {"      3  2/      / //2      "},
    {"//////.// //////// //.//////"},
    {"     /.//2        2//./     "},
    {"//////.// //////// //.//////"},
    {"/1....3..2..1//1..2..3....1/"},
    {"/.////./////.//./////.////./"},
    {"/.////./////.//./////.////./"},
    {"/1.1//2..2..2..2..2..2//1.1/"},
    {"///.//.//.////////.//.//.///"},
    {"///.//.//.////////.//.//.///"},
    {"/1.2..1//1..1//1..1//1..2.1/"},
    {"/.//////////.//.//////////./"},
    {"/1..........2..2..........1/"},
    {"////////////////////////////"},
};

static void copy_field(game_space field)
{
    int i;
    for(i = 0; i < field_height; ++i) {
        int j;
        for(j = 0; j < field_width; ++j)
            field[i][j] = field_sample[i][j];
    }
}

game_space get_new_field()
{
    game_space field = malloc(field_width * field_height);
    copy_field(field);
    return field;
}

int field_has_coin(int x, int y)
{
    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)
{
    int i;
    char symbol;
    for(i = 0; i < field_height; ++i) {
        int j;
        for(j = 0; j < field_width; ++j) {
            symbol = field[i][j];
            move(i, j);
            switch(symbol) {
            case '1':
            case '2':
            case '3':
                if(field_has_coin(i, j))
                    addch('.');
                else
                    addch(' ');
                break;
            case '/':
                addch('/');
                break;
            case '.':
                addch('.');
                break;
            case '#':
                addch('#');
                break;
            }
            refresh();
        }
    }
}

void display_character(int y, int x, enum select_character symbol)
{
    move(y, x);
    addch(symbol);
    refresh();
}

void display_ghosts_on_field(struct ghost_type *red_ghost,
                             struct ghost_type *pink_ghost,
                             struct ghost_type *blue_ghost,
                             struct ghost_type *orange_ghost)
{
    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_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 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 coin:
            queue_consists_point(eaten_coins, position) ?
                addch(' ') : addch('.');
            break;
        case ' ':
            addch(' ');
            break;
        }
    }
    else if(character == pac_char)
        addch(' ');
    refresh();
}

enum intersection_type get_intersection(const game_space field,
                                          struct ghost_type *ghost)
{
    int y, x, symbol;
    y = ghost->position.y;
    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;
    }
}

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;
    found_paths.right = field[y][x+1] != '/' && field[y][x+1] != '#' ? 1 : 0;
    found_paths.up = field[y-1][x] != '/' && field[y-1][x] != '#' ? 1 : 0;
    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;
}