user-0 提问: 贪吃蛇一吃到食物就不行了，求教大神哪里错了

回复:
user: user-0
content: 呵呵，断点调试后解决问题了，多谢了

user: user-1
content: 引用 2 楼 u012852836 的回复:我写的哪个错在哪了，为什么不行啊？ 错在逻辑复杂，难以看明白。 for()中i一直增加，没有上限，一看就有问题。 你为何不断点调一下呢？

user: user-0
content: 我写的哪个错在哪了，为什么不行啊？

user: user-1
content: #include <windows.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <time.h> #define S 200 #define HEIGHT 20 #define WIDTH 80 int m,n; void gotoxy(int x ,int y) { 	COORD pos={x,y}; 	HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);     SetConsoleCursorPosition(hOut,pos); 	 }  void block() { 	int i=0,j=0; 	gotoxy(i,j); 	while (i<WIDTH) 	{ 		printf("@"); 		i++; 	} 	i=0;j=1; 	while (j<HEIGHT-1) 	{ 		gotoxy(i,j); 		printf("@"); 		gotoxy(i+WIDTH-1,j); 		printf("@"); 		j++; 	} 	while (i<WIDTH) 	{         gotoxy(i,j); 		printf("@"); 		i++; 	} }  food() { 	int x=(int)(rand()%79)+1; 	int y=(int)(rand()%19)+1; 	if (x == 0 || x == 79 || y == 0 || y == 19) 	{ 		food(); 	} 	else 	{ 		m=x;n=y; 		gotoxy(x,y); 		printf("*"); 	} }  void start() { 	int nSleep = S; 	food();  	struct Corner 	{ 		int nX; 		int nY; 		int nDirection; 		struct Corner *pNext; 	};  	Corner Head; 	Corner *pTail; 	pTail = NULL; 	Head.pNext = NULL; #define UP_DIRECTION	0 #define RIGHT_DIRECTION	1 #define DOWN_DIRECTION	2 #define LEFT_DIRECTION	3 	int nTailDirection = RIGHT_DIRECTION; 	int nHeadDirection = RIGHT_DIRECTION;  	int nTailX = 5; 	int nTailY = 5; 	int nHeadX = 7; 	int nHeadY = 5;  	for (int i = nTailX; i <= nHeadX - 1; i++) 	{ 		gotoxy(i, nTailY); 		printf("*"); 	} 	gotoxy(nHeadX, nHeadY); 	printf("#");  	char ch = 0; 	while(TRUE) 	{ 		if (kbhit()) 		{ 			int nOldHeadDirection = nHeadDirection; 			ch=getch(); 			switch(ch) 			{ 			case 'w': 			case 'W': 				if (nHeadDirection != DOWN_DIRECTION) 				{ 					if (nHeadDirection == UP_DIRECTION) 					{ 						nSleep = 1; 					} 					nHeadDirection = UP_DIRECTION; 				} 				break; 			case 'd': 			case 'D': 				if (nHeadDirection != LEFT_DIRECTION) 				{ 					if (nHeadDirection == RIGHT_DIRECTION) 					{ 						nSleep = 1; 					} 					nHeadDirection = RIGHT_DIRECTION; 				} 				break; 			case 's': 			case 'S': 				if (nHeadDirection != UP_DIRECTION) 				{ 					if (nHeadDirection == DOWN_DIRECTION) 					{ 						nSleep = 1; 					} 					nHeadDirection = DOWN_DIRECTION; 				} 				break; 			case 'a': 			case 'A': 				if (nHeadDirection != RIGHT_DIRECTION) 				{ 					if (nHeadDirection == LEFT_DIRECTION) 					{ 						nSleep = 1; 					} 					nHeadDirection = LEFT_DIRECTION; 				} 				break; 			default: 				break; 			} 			if (nOldHeadDirection != nHeadDirection) 			{ 				Corner *pCorner = new Corner(); 				pCorner->nX = nHeadX; 				pCorner->nY = nHeadY; 				pCorner->nDirection = nHeadDirection; 				pCorner->pNext = NULL; 				if (pTail) 				{ 					pTail->pNext = pCorner; 				} 				else 				{ 					Head.pNext = pCorner; 				} 				pTail = pCorner; 			} 		} 	 		switch(nHeadDirection) 		{ 		case UP_DIRECTION: 			gotoxy(nHeadX, nHeadY); 			printf("*"); 			nHeadY -= 1; 			break; 		case RIGHT_DIRECTION: 			gotoxy(nHeadX, nHeadY); 			printf("*"); 			nHeadX += 1; 			break; 		case DOWN_DIRECTION: 			gotoxy(nHeadX, nHeadY); 			printf("*"); 			nHeadY += 1; 			break; 		case LEFT_DIRECTION: 			gotoxy(nHeadX, nHeadY); 			printf("*"); 			nHeadX -= 1; 			break; 		default: 			break; 		}  		gotoxy(nHeadX, nHeadY); 		printf("#");  		if (nHeadX == m && nHeadY == n) 		{  			food(); 		} 		else 		{ 			gotoxy(nTailX, nTailY); 			printf(" ");  			Corner *pHeadNext = Head.pNext; 			if (pHeadNext) 			{ 				if (pHeadNext->nX == nTailX && pHeadNext->nY == nTailY) 				{ 					nTailDirection = pHeadNext->nDirection; 					Head.pNext = pHeadNext->pNext; 					if (!Head.pNext) 					{ 						pTail = NULL; 					} 					delete pHeadNext; 				} 			}  			switch(nTailDirection) 			{ 			case UP_DIRECTION: 				nTailY -= 1; 				break; 			case RIGHT_DIRECTION: 				nTailX += 1; 				break; 			case DOWN_DIRECTION: 				nTailY += 1; 				break; 			case LEFT_DIRECTION: 				nTailX -= 1; 				break; 			default: 				break; 			} 		}  		if (nHeadX == 0 || nHeadX == 79 || nHeadY == 0 || nHeadY == 19) 		{ 			gotoxy(0, 20); 			printf("Dead!"); 			getch(); 			exit(EXIT_FAILURE); 		}  		if (nSleep != 1) 		{ 			Sleep(nSleep); 		} 		nSleep = S; 	} }  int main() { 	srand((unsigned)time(NULL)); 	block(); 	start(); 	 }