user-0 提问: 自己用easyx写的2048，没有加界面的时候WASD控制都是正确的，加了界面之后，WASD控制的方向就混乱了

回复:
user: user-1
content: case 'a':                     case 'A':moveup();break;                     case 'w':                     case 'W':moveleft();break;                     case 'd':                     case 'D':movedown();break;                     case 's':                     case 'S':moveright();break;                     default :printf("\a");

user: user-1
content: 不过你逻辑好像有问题 我玩了一下遇到bug了

user: user-2
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-0
content: 这是代码 #include <graphics.h> #include <easyx.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #pragma comment(lib,"Winmm.lib") int num[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; int score=0;//  void start1() { 	Sleep(1000); 	setfillcolor(RGB(255,255,255)); 	solidrectangle(0,0,700,700); 	settextstyle(50, 20,"华文新魏"); 	settextcolor(MAGENTA); 	setbkmode(TRANSPARENT); 	outtextxy(80,10,"欢迎试玩由金依编写的游戏"); 	outtextxy(300,60,"2048"); 	Sleep(1000); 	outtextxy(50,110,"尽情挑战更大的合成数字吧~~"); 	Sleep(1000); 	outtextxy(50,500,"点击任意地方继续"); } //开始界面 void hellowin() { 	setfillcolor(LIGHTBLUE); 	solidrectangle(0,0,700,700); 	settextcolor(WHITE); 	setbkmode(TRANSPARENT); 	settextstyle(50, 20,"华文彩云",0,0,200,0,0,0); 	outtextxy(225,100,"2"); 	outtextxy(275,50,"0"); 	outtextxy(375,50,"4"); 	outtextxy(425,100,"8"); 	setfillcolor(RGB(181,230,29)); 	solidroundrect(225,200,425,300,20,20); 	settextcolor(BLACK); 	outtextxy(275,225,"开始"); 	setfillcolor(RGB(0,255,255)); 	solidroundrect(225,350,315,450,20,20);//打开音乐 	solidroundrect(335,350,425,450,20,20);//关闭音乐 	settextcolor(WHITE); 	settextstyle(30, 20,"楷体",0,0,200,0,0,0); 	outtextxy(100,375,"MUSIC"); 	settextstyle(50, 20,"华文彩云",0,0,200,0,0,0); 	outtextxy(240,375,"ON"); 	outtextxy(340,375,"OFF"); 	setfillcolor(RGB(255,255,0)); 	solidroundrect(225,500,425,600,20,20); 	settextcolor(BLACK); 	outtextxy(275,525,"退出"); } //游戏界面 void clear() { 	int x,y; 	setfillcolor(RGB(153,217,234)); 	solidrectangle(0,0,700,700); 	settextstyle(50, 50,"宋体"); 	settextcolor(GREEN); 	setbkmode(TRANSPARENT); 	outtextxy(200,10,"2048"); 	settextstyle(20, 10,"微软雅黑"); 	settextcolor(BLACK); 	outtextxy(0,550,"游戏规则："); 	outtextxy(0,570,"1.W、A、S、D控制移动，W-左，A-上，S-右，D-下"); 	outtextxy(0,590,"2.当数字相同的色块相邻并且向此方向移动时两色块合并"); 	outtextxy(0,610,"3.当合成2048时，挑战成功"); 	outtextxy(0,630,"4.当没有空位并且没有相同的色块相邻时，游戏结束"); 	outtextxy(550,100,"合成分数:"); 	for(x=100;x<500;x+=100) 	{ 		for(y=100;y<500;y+=100) 		{ 			if((x/100)%2==1) 			{ 				if((y/100)%2==1) 				{ 					setfillcolor(RGB(255,188,121)); 					solidrectangle(x,y,x+100,y+100); 				}else{ 					setfillcolor(RGB(255,142,30)); 					solidrectangle(x,y,x+100,y+100); 				} 			}else{ 				if((y/100)%2==1) 				{ 					setfillcolor(RGB(255,142,30)); 					solidrectangle(x,y,x+100,y+100); 				}else{ 					setfillcolor(RGB(255,188,121)); 					solidrectangle(x,y,x+100,y+100); 				} 			} 		} 	} }  //产生随机数 int randnum(int x) { 	int a; 	a=rand()%x; 	return a; }  //判断游戏是否结束,结束返回0，否则返回1 int gamefail() { 	int i,j,l=0;        	for(i=0;i<4;i++) 	{ 		for(j=0;j<4;j++) 		{ 			if(num[i][j]!=0) 				l++;   //l记录非空白格的个数  		} 	} 	//判断 	if(l!=16) 	{ 		return 1; 	}else{ 		l=0;   //l记录是否有相邻的数字相同  		//从上往下 		for(j=0;j<4;j++) 		{ 			for(i=1;i<3;i++) 			{ 				if((num[i][j]==num[i+1][j])||(num[i][j]==num[i-1][j])) 				{ 					l++; 					break; 				} 			} 			if((num[i][j]==num[i+1][j])||(num[i][j]==num[i-1][j])) 				break; 		} 		if(l!=0) 			return 1; 		else 		{	 			l=0; 			for(i=0;i<4;i++) 			{ 				for(j=1;j<3;j++) 				{ 					if((num[i][j]==num[i][j+1])||(num[i][j]==num[i][j-1])) 					{ 						l++; 						break; 					} 				} 				if((num[i][j]==num[i][j+1])||(num[i][j]==num[i][j-1])) 					break; 			} 			if(l!=0) 				return 1; 			else 				return 0; 		} 	} }  //初始化 void start() { 	int x1,y1;//初始随机坐标 	int r;//随机数 	int i,j; 	x1=randnum(4); 	y1=randnum(4); 	r=randnum(10); 	if(r==9) 	{ 		num[x1][y1]=4; 	}else{ 		num[x1][y1]=2; 	} 	i=randnum(4); 	j=randnum(4); 	while(num[i][j]!=0) 	{ 		i=randnum(4); 		j=randnum(4); 	} 	r=randnum(10); 	if(r==9) 	{ 		num[i][j]=4; 	}else{ 		num[i][j]=2; 	} }  void randr() { 	int i,j,r; 	i=randnum(4); 	j=randnum(4); 	while(num[i][j]!=0) 	{ 		i=randnum(4); 		j=randnum(4); 	} 	r=randnum(10); 	if(r==9) 		num[i][j]=4; 	else 		num[i][j]=2; }  void moveup() { 	int i,j,k,l; 	int temp[4]={0,0,0,0};       //间接转换 	//将原来的数组中的非零数相邻放置 	for(j=0;j<4;j++) 	{ 		l=0; 		for(i=0;i<4;i++) 		{ 			for(k=0;k<3;k++) 			{ 				if(num[k][j]==0) 				{ 					num[k][j]=num[k+1][j]; 					num[k+1][j]=0; 				}else	 					l++; 				if(l==4) 					break; 			} 		} 	} 	//将数组的列转入中间变量并移动、相加 	for(j=0;j<4;j++) 	{ 		k=0; 		for(i=0;i<4;i++) 		{ 			if(i==3) 			{ 				temp[k++]=num[i][j]; 				num[i][j]=0; 			}else{ 				if(num[i][j]!=num[i+1][j]&&num[i][j]!=0) 				{ 					temp[k++]=num[i][j]; 					num[i][j]=0; 				}else if(num[i][j]==num[i+1][j]) 				{ 					temp[k++]=num[i][j]+num[i+1][j]; 					score+=temp[k-1];//计算得分 					num[i][j]=num[i+1][j]=0; 				} 			} 		} 		for(i=0;i<4;i++) 			num[i][j]=temp[i]; 	} }  void moveleft() { 	int i,j,k,l; 	int temp[4]={0,0,0,0}; 	for(i=0;i<4;i++) 	{ 		l=0; 		for(j=0;j<4;j++) 		{ 			for(k=0;k<3;k++) 			{ 				if(num[i][k]==0) 				{ 					num[i][k]=num[i][k+1]; 					num[i][k+1]=0; 				}else	 					l++; 				if(l==4) 					break; 			} 		} 	} 	for(i=0;i<4;i++) 	{ 		k=0; 		for(j=0;j<4;j++) 		{ 			if(j==3) 			{ 				if(num[i][j]!=0) 				{ 					temp[k++]=num[i][j]; 					num[i][j]=0; 				} 			}else{ 				if(num[i][j]!=num[i][j+1]&&num[i][j]!=0) 				{ 					temp[k++]=num[i][j]; 					num[i][j]=0; 				}else if(num[i][j]==num[i][j+1]) 				{ 					temp[k++]=num[i][j]+num[i][j+1]; 					score+=temp[k-1]; 					num[i][j]=num[i][j+1]=0; 				} 			} 		} 		for(j=0;j<4;j++) 		{ 			num[i][j]=temp[j]; 		} 	} }  void movedown() { 	int i,j,k,l; 	int temp[4]={0,0,0,0}; 	for(j=0;j<4;j++) 	{ 		l=0; 		for(i=3;i>=0;i--) 		{ 			for(k=3;k>0;k--) 			{ 				if(num[k][j]==0) 				{ 					num[k][j]=num[k-1][j]; 					num[k-1][j]=0; 				}else 					l++; 				if(l==4) 					break; 			} 		} 	} 	for(j=0;j<4;j++) 	{ 		k=3; 		for(i=3;i>=0;i--) 		{ 			if(i==0) 			{ 				if(num[i][j]!=0) 				{ 					temp[k--]=num[i][j]; 					num[i][j]=0; 				} 			}else{ 				if(num[i][j]!=num[i-1][j]&&num[i][j]!=0) 				{ 					temp[k--]=num[i][j]; 					num[i][j]=0; 				}else if(num[i][j]==num[i-1][j]) 				{ 					temp[k--]=num[i][j]+num[i-1][j]; 					score+=temp[k+1]; 					num[i][j]=num[i-1][j]=0; 				} 			} 		} 		for(i=3;i>=0;i--) 			num[i][j]=temp[i]; 	} }  void moveright() { 	int i,j,k,l; 	int temp[4]={0,0,0,0}; 	for(i=0;i<4;i++) 	{ 		l=0; 		for(j=3;j>=0;j--) 		{ 			for(k=3;k>0;k--) 			{ 				if(num[i][k]==0) 				{ 					num[i][k]=num[i][k-1]; 					num[i][k-1]=0; 				}else	 					l++; 				if(l==4) 					break; 			} 		} 	} 	for(i=0;i<4;i++) 	{ 		k=3; 		for(j=3;j>=0;j--) 		{ 			if(j==0) 			{ 				if(num[i][j]!=0) 				{ 					temp[k--]=num[i][j]; 					num[i][j]=0; 				} 			}else{ 				if(num[i][j]!=num[i][j-1]&&num[i][j]!=0) 				{ 					temp[k--]=num[i][j]; 					num[i][j]=0; 				}else if(num[i][j]==num[i][j-1]) 				{ 					temp[k--]=num[i][j]+num[i][j-1]; 					score+=temp[k+1]; 					num[i][j]=num[i][j-1]=0; 				} 			} 		} 		for(j=0;j<4;j++) 			num[i][j]=temp[j]; 	} }  int max1() { 	int i,j,m=num[0][0]; 	for(i=0;i<4;i++) 	{ 		for(j=0;j<4;j++) 		{ 			if(num[i][j]>m) 				m=num[i][j]; 		} 	} 	return m; }  void success() { 	system("cls"); 	setfillcolor(WHITE); 	solidrectangle(0,0,700,700); 	settextcolor(RED); 	settextstyle(50, 20,"华文新魏"); 	outtextxy(300,300,"你赢了！！！"); } void fail() { 	system("cls"); 	setfillcolor(WHITE); 	solidrectangle(0,0,700,700); 	settextcolor(RED); 	settextstyle(50, 20,"华文新魏"); 	outtextxy(300,300,"GAME OVER"); }  void output() { 	int i,j,k=0; 	char a[16][5]; 	char score1[8]; 	for(i=0;i<4;i++) 		for(j=0;j<4;j++) 		{ 			settextstyle(30, 10,"楷体"); 			settextcolor(BLUE); 			sprintf(a[k],"%d",num[i][j]); 			if(num[i][j]!=0) 			outtextxy(100+i*100+50,100+j*100+50,a[k]); 			k++; 		} 	settextstyle(20, 10,"微软雅黑"); 	sprintf(score1,"%d",score); 	settextcolor(RED); 	outtextxy(550,120,score1); }  void main() { 	initgraph(700,700); 	MOUSEMSG m; 	int i,j,k=0,l=0; 	int move; 	int num1[4][4]; 	char c; 	srand((unsigned)time(NULL)); 	mciSendString("open music.mp3 alias mymusic",NULL,0,NULL); 	mciSendString("open 合成音效.wav alias mymusic",NULL,0,NULL); 	mciSendString("open 未移动.wav alias mymusic",NULL,0,NULL); 	mciSendString("play mymusic repeat",NULL,0,NULL); 	start1(); 	FlushMouseMsgBuffer();//清除鼠标信息 	while(1) 	{ 		m=GetMouseMsg(); 		if(m.uMsg==WM_LBUTTONDOWN) 		{	 			break; 		} 	} 	setfillcolor(RGB(255,255,255)); 	solidrectangle(0,0,700,700); 	settextcolor(LIGHTRED); 	settextstyle(50, 20,"华文新魏"); 	outtextxy(25,200,"本游戏是作者做的第一个游戏"); 	Sleep(1000); 	outtextxy(25,300,"有瑕疵，请谅解O(∩_∩)O"); 	Sleep(1000); 	outtextxy(25,400,"本游戏最终解释权归作者所有"); 	Sleep(1500); 	FlushMouseMsgBuffer(); 	hellowin(); 	while(1) 	{ 		m=GetMouseMsg(); 		if(m.uMsg==WM_LBUTTONDOWN) 		{	 			if((m.x>=225&&m.x<=425)&&(m.y>=200&&m.y<=300)) 			{//点击“开始” 				clear(); 				break; 			}if((m.x>=225&&m.x<=425)&&(m.y>=500&&m.y<=600)) 			{//点击“退出” 				return ; 			} 			if(m.y>=350&&m.y<=450) 			{ 				if(m.x>=225&&m.x<=315) 				{ 					mciSendString("play mymusic repeat",NULL,0,NULL);	 				} 				else if(m.x>=335&&m.x<=425) 				{ 					mciSendString("stop mymusic",NULL,0,NULL); 				} 			} 		} 	} 		//初始化 			do 			{ 				for(i=0;i<4;i++) 				{ 					for(j=0;j<4;j++) 					{ 						num[i][j]=0; 					} 				} 				start(); 				for(i=0;i<4;i++) 				{ 					for(j=0;j<4;j++) 					{ 						if(num[i][j]!=0) 							l++; 					} 				} 			}while(l!=2); 			output(); 			while(gamefail()) 			{ 				move=0; 				//判断如何移动 				for(i=0;i<4;i++) 				{ 					for(j=0;j<4;j++) 					{ 						num1[i][j]=num[i][j]; 					} 				} 				c=getch(); 				switch(c) 				{ 					case 'w': 					case 'W':moveup();break; 					case 'a': 					case 'A':moveleft();break; 					case 's': 					case 'S':movedown();break; 					case 'd': 					case 'D':moveright();break; 					default :printf("\a"); 				} 				for(i=0;i<4;i++) 				{ 					for(j=0;j<4;j++) 					{ 						if(num1[i][j]==num[i][j]) 							move++; 						else 							break; 					} 					if(num1[i][j]!=num[i][j]) 						break; 				} 				if(move!=16) 				{ 					//在空白地方产生一个随机数 					randr(); 					clear(); 					mciSendString("stop 合成音效.wav",NULL,0,NULL); 					mciSendString("play 合成音效.wav",NULL,0,NULL); 					output();//输出 				}else{ 					mciSendString("stop 未移动.wav",NULL,0,NULL); 					mciSendString("play 未移动.wav",NULL,0,NULL); 				} 				if(max1()==2048) 				{ 					success(); 					break; 				} 	} 	if(gamefail()&&max1()!=2048) 		fail(); 	getch();	 }