关于贪吃蛇的游戏,为什么吃完第二只蛇的时候就出错了?大家帮忙看一下。
耐心看完,先谢了~~#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 21
int apple[3];
char tail[3];
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int Block(char head[2]) //判断出界
{
if ((head[0] < 1) || (head[0] > N) || (head[1] < 1) || (head[1] > N))
return 1;
return 0;
}
int Eat(char snake[2])
{
if ((snake[0] == apple[0]) && (snake[1] == apple[1]))
{
apple[0] = apple[1] = apple[2] = 0;
return 1;
}
return 0;
}
void Draw(char **snake, int len) //蛇移动
{
if (apple[2]) {
gotoxy(apple[1] * 2, apple[0]);
printf("○");
}
gotoxy(tail[1] * 2, tail[0]);
if (tail[2])
printf("★");
else
printf("■");
gotoxy(snake[0][1] * 2, snake[0][0]);
printf("★");
putchar('\n');
}
int Move(char **snake, char dirx, int *len) //控制方向
{
int i, full = Eat(snake[0]);
memcpy(tail, snake[(*len)-1], 2);
for (i = (*len) - 1; i > 0; --i)
memcpy(snake[i], snake[i-1], 2);
switch (dirx)
{
case 'w': case 'W': --snake[0][0]; break;
case 's': case 'S': ++snake[0][0]; break;
case 'a': case 'A': --snake[0][1]; break;
case 'd': case 'D': ++snake[0][1]; break;
default: ;
}
if (Block(snake[0])) return 0;
if (full) // 感觉 bugs is here!
{
snake = (char **)realloc(snake, sizeof(char *) * ((*len) + 1));
snake[(*len)] = (char *)malloc(sizeof(char) * 2);
memcpy(snake[(*len)], tail, 2);
++(*len);
tail[2] = 1;
}
else
tail[2] = 0;
return 1;
}
void init(char plate[N+2][N+2], char ***snake_x, int *len) //初始化
{
int i, j;
char **snake = NULL;
*len = 3;
snake = (char **)realloc(snake, sizeof(char *) * (*len));
for (i = 0; i < *len; ++i)
snake[i] = (char *)malloc(sizeof(char) * 2);
for (i = 0; i < 3; ++i)
{
snake[i][0] = N/2 + 1;
snake[i][1] = N/2 + 1 + i;
}
for (i = 1; i <= N; ++i)
for (j = 1; j <= N; ++j)
plate[i][j] = 1;
apple[0] = rand()%N + 1; apple[1] = rand()%N + 1;
apple[2] = 1;
for (i = 0; i < N + 2; ++i)
{
gotoxy(0, i);
for (j = 0; j < N + 2; ++j)
{
switch (plate[i][j])
{
case 0: printf("□"); continue;
case 1: printf("■"); continue;
default: ;
}
}
putchar('\n');
}
for (i = 0; i < (*len); ++i)
{
gotoxy(snake[i][1] * 2, snake[i][0]);
printf("★");
}
putchar('\n');
*snake_x = snake;
}
void Manual()
{
FILE *fp;
//fopen
gotoxy(N+30,1);
printf("按 W S A D 移动方向");
gotoxy(N+30,5);
printf("历史最高分为: 100");
gotoxy(N+30,8);
printf("你现在得分为: 0");
}
void Free(char **snake, int len)
{
int i;
for (i = 0; i < len; ++i)
free(snake[i]);
free(snake);
}
int main(void)
{
int len;
char ch = 'a';
char a[N+2][N+2] = {{0}};
char **snake;
srand((unsigned)time(NULL));
init(a, &snake, &len);
Manual();
while (ch != 0x1B) // 按 ESC 结束
{
Draw(snake, len);
if (!apple[2]) {
apple[0] = rand()%N + 1;
apple[1] = rand()%N + 1;
apple[2] = 1;
}
setbuf(stdin, NULL);
if (kbhit())
{
gotoxy(0, N+2);
ch = getche();
}
if (!Move(snake, ch, &len))
{
gotoxy(N+2, N+2);
puts("你输了");
Free(snake, len);
exit(0);
}
Sleep(300);
}
Free(snake, len);
exit(0);
return 0;
}
为什么蛇吃完第二个苹果了,程序就报错了?