back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/field.c
blob: df61097787edf97ecf6977b50a41c80ee88b5343 (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
#include "field.h"
#include "ghosts.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;
}

static int is_has_point(int i, int j)
{
    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));
}

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(is_has_point(i, j))
                        addch('.');
                    else
                        addch(' ');
                    break;
                case '/':
                    addch('/');
                    break;
                case '.':
                    addch('.');
                    break;
                case '#':
                    addch('#');
                    break;
            }
            refresh();
        }
    }
}

void display_character(int y, int x, int 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, '&');
    display_character(pink_ghost->position.y, pink_ghost->position.x, '&');
    display_character(blue_ghost->position.y, blue_ghost->position.x, '&');
    display_character(orange_ghost->position.y, orange_ghost->position.x, '&');
}

void clear_symbol(game_space field, int y, int x, 
                  enum select_character character)
{
    int symbol = field[y][x];
    move(y, x);
    if(character == ghost_char) {
        switch(symbol) {
            case '#':
                addch('#');
                break;
            case '.':
                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(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;
}