summaryrefslogtreecommitdiff
path: root/bsp/simulator/testdll/snake/snake.c
blob: 87e1c8b36ff76d2ead7ea37abf48702d796e5049 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <string.h>
#include <stdlib.h>
#include <rtthread.h>
#include "snake.h"

#define ASSERT_RET(x, ret)  \
    do{                     \
        if (x)              \
            return ret;     \
    }while(0)

rt_list_t snake_head;
SNAKE_DIR prevdir, newdir;

static SNAKE_DIR dir_adjust(SNAKE_DIR dir)
{
    if ((SNAKE_DIR_UP == prevdir && SNAKE_DIR_DOWN != dir)
            || (SNAKE_DIR_DOWN == prevdir && SNAKE_DIR_UP != dir)
            || (SNAKE_DIR_LEFT == prevdir && SNAKE_DIR_RIGHT != dir)
            || (SNAKE_DIR_RIGHT == prevdir && SNAKE_DIR_LEFT != dir)
       )
    {
        newdir = dir;
    }
    else
    {
        rt_kprintf("dirction change error\n\r");
    }

    return newdir;
}

static void across_XY(point_t *node, const map_t *map)
{
    RT_ASSERT(node != RT_NULL && map != RT_NULL);

    // ������ȳ�����ǰ�߿�����Դ�Խǽ������
    node->x = (node->x + map->width) % map->width;
    node->y = (node->y + map->height) % map->height;
}

static SYS_STE node_update(snake_t *tail, const point_t *node, map_t *map)
{
    SYS_STE ret;
    point_t *pos;

    RT_ASSERT(tail != RT_NULL && node != RT_NULL && map != RT_NULL);

    pos = map->snake_flush;
    pos[0].x = pos[0].y = -1;
    pos[1].x = pos[1].y = -1;

    ret = (SYS_STE)map->range[node->y * map->width + node->x];

    if (FOOD == map->range[node->y * map->width + node->x])
    {
        // ��һ��ʳ������һ���ڵ�
        snake_t *new = (snake_t *)rt_malloc(sizeof(snake_t));
        if (!new)
            return NORMAL;

        pos[0] = *node;
        new->body = *node;
        rt_list_insert_after(&snake_head, &new->list);
    }
    else if (NORMAL == map->range[node->y * map->width + node->x])
    {
        // ��β���޸ĺ��õ�ͷ������������
        rt_list_remove(&tail->list);
        map->range[tail->body.y * map->width + tail->body.x] = NORMAL;

        pos[0] = *node;
        pos[1] = tail->body;

        tail->body = *node;
        rt_list_insert_after(&snake_head, &tail->list);
    }

    map->range[node->y * map->width + node->x] = OVER;

    if (ret != OVER)
        prevdir = newdir;

    return ret;
}


map_t *map_init(rt_uint32_t width, rt_uint32_t heigth)
{
    map_t *map = rt_malloc(sizeof(map_t));

    if (map != RT_NULL)
    {
        map->range = rt_malloc(heigth * width);

        if (!map->range)
        {
            rt_free(map);
            map = RT_NULL;
        }
        else
        {
            map->width = width;
            map->height = heigth;
            memset(map->range, NORMAL, heigth * width);
        }
    }

    return map;
}

// ����һ��ָ�����ȵ�����ָ����
rt_bool_t snake_init(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
{
    rt_int32_t i;
    rt_int32_t inc_x, inc_y;
    point_t old = *start;

    ASSERT_RET(!map || !start, RT_FALSE);

    rt_list_init(&snake_head);

    if (dir == SNAKE_DIR_UP || dir == SNAKE_DIR_DOWN)
    {
        if (map->height <= length)
            return RT_FALSE;

        inc_x = 0;
        inc_y = dir == SNAKE_DIR_DOWN ? 1 : -1;  // �����ӳ����ӣ�ͷ����ָ��λ��
        old.y -= inc_y;
    }
    else
    {
        if (map->width <= length)
            return RT_FALSE;

        inc_y = 0;
        inc_x = dir == SNAKE_DIR_RIGHT ? -1 : 1;
        old.x -= inc_x;
    }

    for (i = 0; i < length; i++)
    {
        snake_t *new = (snake_t *)rt_malloc(sizeof(snake_t));
        if (!new)
            return RT_FALSE;

        new->body.y = inc_y + old.y;
        new->body.x = inc_x + old.x;

        // ������ȳ�����ǰ�߿�����Դ�Խǽ������
        across_XY(&new->body, map);

        map->range[new->body.y * map->width + new->body.x] = OVER;

        old = new->body;
        rt_list_insert_before(&snake_head, &new->list);
    }

    prevdir = dir;

    return RT_TRUE;
}

// �����ʳ��
rt_bool_t food_init(map_t *map, rt_uint32_t max_num)
{
    point_t food;

#ifndef FOOD_TIMEOUT
#define FOOD_TIMEOUT    10
#endif

    rt_uint32_t timeout, num;

    ASSERT_RET(!map, RT_FALSE);

    num = 0;
    timeout = rt_tick_get();
    srand(rand());

    map->food_flush[0].x = map->food_flush[0].y = -1;

    do
    {
        food.x = rand() % map->width;
        food.y = rand() % map->height;

        if (map->range[food.y * map->width + food.x] == NORMAL)
        {
            map->food_flush[0] = food;
            map->range[food.y * map->width + food.x] = FOOD;
            num++;
        }
    }
    while (num < max_num && rt_tick_get() - timeout < FOOD_TIMEOUT);

    return num;
}

void map_deinit(map_t *map)
{
    if (map)
    {
        if (map->range)
        {
            rt_free(map->range);
            map->range = RT_NULL;
        }
        rt_free(map);
    }
}

void snake_deinit(void)
{
    snake_t *node;

    while (!rt_list_isempty(&snake_head))
    {
        node = rt_list_entry(snake_head.prev, snake_t, list);
        rt_list_remove(&node->list);
        rt_free(node);
    }
}

void food_deinit(void)
{

}

SYS_STE snake_step(SNAKE_DIR dir, map_t *map)
{
    snake_t *tail, *head;
    point_t node;

    ASSERT_RET(!map, RT_FALSE);

    dir = dir_adjust(dir);

    // ȡ��ͷβ�����ڵ㣬�����ڵ㲻��Ҫ�ı�
    tail = rt_list_entry(snake_head.prev, snake_t, list);
    head = rt_list_entry(snake_head.next, snake_t, list);

    node = head->body;

    // ����һ���µ���ͷ����
    switch (dir)
    {
    case SNAKE_DIR_UP:
    case SNAKE_DIR_DOWN:
        node.y = head->body.y + (dir == SNAKE_DIR_DOWN ? -1 : 1);
        break;
    case SNAKE_DIR_LEFT:
    case SNAKE_DIR_RIGHT:
        node.x = head->body.x + (dir == SNAKE_DIR_RIGHT ? 1 : -1);
        break;
    }
    across_XY(&node, map);

    return node_update(tail, &node, map);
}

rt_bool_t snake_restart(const point_t *start, const int length, const SNAKE_DIR dir, map_t *map)
{
    ASSERT_RET(!map || !start, RT_FALSE);

    snake_deinit();
    memset(map->range, NORMAL, map->width * map->height);

    return snake_init(start, length, dir, map);
}