示例#1
0
// 判断蛇是否死了。
// 返回0 蛇没有死 else 蛇死了。
int IsSnakeDead()
{
	PGAME_COORD posBody;
	PGAME_COORD posHead;

	int i;
	int size = ListSize(snake_list);


	// 判断是否死亡
	/// 判断是否碰到墙
	posHead = (PGAME_COORD)ListGetAt(snake_list, 0);

	if (posHead->x < 0 || posHead->x > boundary.x ||
		posHead->y < 0 || posHead->y > boundary.y)
	{
		return SNAKE_DEAD;
	}
	/// 判断是否碰到自己
	//// 从第二个节点开始,逐一和头节点比较。
	size = ListSize(snake_list);

	for (i = 1; i < size; i++)
	{
		posBody = (PGAME_COORD)ListGetAt(snake_list, i);
		if (CoordEqual(posHead, posBody))
		{
			return SNAKE_DEAD;
		}
	}
	return SNAKE_MOVED;
}
示例#2
0
// 生成新的食物。
int CreateFood()
{
	PGAME_COORD posbody;
	int i;
	int size = ListSize(snake_list);

new_food:

	// 随机生成食物的坐标。
	food.x = rand() % boundary.x;
	food.y = rand() % boundary.y;

	// 判断是否和蛇重叠了,否则重新生成食物坐标,知道不重叠。

	for (i = 0; i < size; i++)
	{
		posbody = (PGAME_COORD)ListGetAt(snake_list, i);
		if (CoordEqual(posbody, &(food)))
		{
			goto new_food;
		}
	}
	return 1;

}
示例#3
0
// 按照序号获得蛇的节点的坐标,不能超过蛇的长度,否则返回NULL
PGAME_COORD GetSnakeAt(int n)
{
	if (n < ListSize(snake_list))
		return (PGAME_COORD)ListGetAt(snake_list, n);
	else
		return NULL;

}
示例#4
0
// 用来移动一步,
// 移动以后,如果吃到了食物,则生长。
// 如果碰到了墙或者自己,则死亡,并返回是否死亡的状态。
int SnakeMove()
{
	// 头和尾的坐标
	PGAME_COORD posHead;
	PGAME_COORD posTail;

	// 把尾从链表中取出,按照当前方向放置到头的位置。
	posHead = (PGAME_COORD)ListGetAt(snake_list, 0);
	posTail = (PGAME_COORD)ListPopBack(snake_list);

	// 根据当前方向来设定新的头坐标。
	switch (snake_dir)
	{
	case SNAKE_UP:
		posTail->y = posHead->y - 1;
		posTail->x = posHead->x;
		break;
	case SNAKE_DOWN:
		posTail->y = posHead->y + 1;
		posTail->x = posHead->x;
		break;
	case SNAKE_LEFT:
		posTail->x = posHead->x - 1;
		posTail->y = posHead->y;
		break;
	case SNAKE_RIGHT:
		posTail->x = posHead->x + 1;
		posTail->y = posHead->y;
		break;
	}
	ListPushFront(snake_list, posTail);

	// 判断是否吃到了食物
	if (CoordEqual(posHead, &food))
	{
		return SNAKE_EATEN_FOOD;
	}

	// 如果没有吃到食物判断是否撞到障碍,然后返回状态。
	return IsSnakeDead();
}
示例#5
0
// 获得蛇的最后一个节点的坐标
PGAME_COORD GetSnakeTail()
{
	return (PGAME_COORD)ListGetAt(snake_list, GetSnakeSize() - 1);
}
示例#6
0
// 获得蛇的第一个节点的坐标
PGAME_COORD GetSnakeHead()
{
	return (PGAME_COORD)ListGetAt(snake_list, 0);
}
示例#7
0
void ManipulateNumerics()
{
    /* We should initialize the container before any operations. */
    List* list = ListInit();

    /* Push the integer elements. */
    ListPushFront(list, (void*)(intptr_t)20);
    ListPushBack(list, (void*)(intptr_t)40);

    /* Insert the elements with the specified indexes. */
    ListInsert(list, 0, (void*)(intptr_t)10);
    ListInsert(list, 3, (void*)(intptr_t)50);
    ListInsert(list, 2, (void*)(intptr_t)30);

    /*---------------------------------------------------------------*
     * Now the list should be: (10)<-->(20)<-->(30)<-->(40)<-->(50)  *
     *---------------------------------------------------------------*/

    /* Iterate through the list. */
    void* element;
    int num = 10;
    ListFirst(list, false);
    while (ListNext(list, &element)) {
        assert((int)(intptr_t)element == num);
        num += 10;
    }

    /* Iterate through the list in the reversed order. */
    num = 50;
    ListFirst(list, true);
    while (ListReverseNext(list, &element)) {
        assert((int)(intptr_t)element == num);
        num -= 10;
    }

    /* Get the element from the list head. */
    ListGetFront(list, &element);
    assert((int)(intptr_t)element == 10);

    /* Get the element from the list tail. */
    ListGetBack(list, &element);
    assert((int)(intptr_t)element == 50);

    /* Get the elements from the specified indexes. */
    ListGetAt(list, 2, &element);
    assert((int)(intptr_t)element == 30);
    ListGetAt(list, 3, &element);
    assert((int)(intptr_t)element == 40);

    /* Replace the element residing at the list head. */
    ListSetFront(list, (void*)(intptr_t)-1);

    /* Replace the element residing at the list tail. */
    ListSetBack(list, (void*)(intptr_t)-5);

    /* Replace the elements residing at the specified indexes. */
    ListSetAt(list, 1, (void*)(intptr_t)-2);
    ListSetAt(list, 2, (void*)(intptr_t)-3);
    ListSetAt(list, 3, (void*)(intptr_t)-4);

    /* Reverse the list. */
    ListReverse(list);

    /*---------------------------------------------------------------*
     * Now the list should be: (-5)<-->(-4)<-->(-3)<-->(-2)<-->(-1)  *
     *---------------------------------------------------------------*/

    /* Remove the element from the list head. */
    ListPopFront(list);

    /* Remove the element from the list tail. */
    ListPopBack(list);

    /* Remove the elements from the specified indexes. */
    ListRemove(list, 1);
    ListRemove(list, 1);

    /* Get the list size. And the remaining element should be (-4). */
    unsigned size = ListSize(list);
    assert(size == 1);

    ListGetFront(list, &element);
    assert((int)(intptr_t)element == -4);

    ListDeinit(list);
}
示例#8
0
void ManipulateObjects()
{
    /* We should initialize the container before any operations. */
    List* list = ListInit();
    ListSetClean(list, CleanObject);

    /* Push the object elements. */
    Tuple* tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 20;
    tuple->second = -20;
    ListPushFront(list, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 40;
    tuple->second = -40;
    ListPushBack(list, tuple);

    /* Insert the elements with the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 10;
    tuple->second = -10;
    ListInsert(list, 0, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 50;
    tuple->second = -50;
    ListInsert(list, 3, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = 30;
    tuple->second = -30;
    ListInsert(list, 2, tuple);

    /*---------------------------------------------------------------*
     * Now the list should be: (10)<-->(20)<-->(30)<-->(40)<-->(50)  *
     *---------------------------------------------------------------*/

    /* Iterate through the list. */
    void* element;
    int num = 10;
    ListFirst(list, false);
    while (ListNext(list, &element)) {
        assert(((Tuple*)element)->first == num);
        num += 10;
    }

    /* Iterate through the list in the reversed order. */
    num = 50;
    ListFirst(list, true);
    while (ListReverseNext(list, &element)) {
        assert(((Tuple*)element)->first == num);
        num -= 10;
    }

    /* Get the element from the list head. */
    ListGetFront(list, &element);
    assert(((Tuple*)element)->first == 10);

    /* Get the element from the list tail. */
    ListGetBack(list, &element);
    assert(((Tuple*)element)->first == 50);

    /* Get the elements from the specified indexes. */
    ListGetAt(list, 2, &element);
    assert(((Tuple*)element)->first == 30);
    ListGetAt(list, 3, &element);
    assert(((Tuple*)element)->first == 40);

    /* Replace the element residing at the list head. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -1;
    tuple->second = 1;
    ListSetFront(list, tuple);

    /* Replace the element residing at the list tail. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -5;
    tuple->second = 5;
    ListSetBack(list, tuple);

    /* Replace the elements residing at the specified indexes. */
    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -2;
    tuple->second = 2;
    ListSetAt(list, 1, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -3;
    tuple->second = 3;
    ListSetAt(list, 2, tuple);

    tuple = (Tuple*)malloc(sizeof(Tuple));
    tuple->first = -4;
    tuple->second = 4;
    ListSetAt(list, 3, tuple);

    /* Reverse the list. */
    ListReverse(list);

    /*---------------------------------------------------------------*
     * Now the list should be: (-5)<-->(-4)<-->(-3)<-->(-2)<-->(-1)  *
     *---------------------------------------------------------------*/

    /* Remove the element from the list head. */
    ListPopFront(list);

    /* Remove the element from the list tail. */
    ListPopBack(list);

    /* Remove the elements from the specified indexes. */
    ListRemove(list, 1);
    ListRemove(list, 1);

    /* Get the list size. And the remaining element should be (-4). */
    unsigned size = ListSize(list);
    assert(size == 1);

    ListGetFront(list, &element);
    assert(((Tuple*)element)->first == -4);

    ListDeinit(list);
}