自已简单思路的2048,结果运行不下去,求大佬指点思路哪里错了
程序代码:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<graphics.h> #define W 87 #define S 83 #define A 65 #define D 68 #define ESC 27 int a[4][4]={0};/*存储得到的数*/ int i,j;/*行列的位置*/ void menu()/*开始界面*/ { setbkcolor(EGERGB(0xff,0xff,0xff)); setcolor(EGERGB(0,0,0)); outtextxy(240,200,"制作人:萌萌"); outtextxy(220,280,"请按任意键开始游戏"); outtextxy(230,300,"上下左右是WSAD"); } void gezi()/*设置格子*/ { int i=0,j=0; setcolor(EGERGB(0,0,0xff)); for(;i<=480;i+=160)/*设置格子*/ { for(;j<=360;j+=120) { rectangle(i,j,i+160,j+120); } j=0; } } void show()/*用于将颜色和数字展现出来*/ { int color=0,t; setfont(80,0,"宋体"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { t=a[i][j]; if(t!=0) { for(;t=1;) { t=t/2; color++; setfillcolor(EGERGB(0,0x00+color,0xff-color)); outtextxy(i*160+80,j*120+60,"a[i][j]"); floodfill(i*160+1,j*120+1,EGERGB(0,0,0xff)); } } } } } void change(int flags)/*根据按键改变对应数字*/ { switch(flags) { case 1:/*按下W键*/ for(i=0;i<4;i++) { j=0; while(a[i][j]==0) { while(j<3) { a[i][j]=a[i][j+1]; a[i][j+1]=0; j++; } j=0; } for(j=0;j<3;) { int n; if(a[i][j]==a[i][j+1]) { a[i][j]+=a[i][j+1]; a[i][i+1]=0; for(n=j+1;n<3;n++) { a[i][n]=a[i][n+1]; a[i][n+1]=0; } j=0; } else j++; } } case 2:/*按下S键*/ for(i=0;i<4;i++) { j=3; while(a[i][j]==0) { while(j>0) { a[i][j]=a[i][j-1]; a[i][j-1]=0; j--; } j=3; } for(j=3;j>0;) { int n; if(a[i][j]==a[i][j-1]) { a[i][j]+=a[i][j-1]; a[i][j-1]=0; for(n=j-1;n>0;n--) { a[i][n]=a[i][n-1]; a[i][n-1]=0; } j=3; } else j--; } } case 3:/*按下A键*/ for(j=0;j<4;j++) { i=0; while(a[i][j]==0) { while(i<3) { a[i][j]=a[i+1][j]; a[i+1][j]=0; i++; } i=0; } for(i=0;i<3;) { int n; if(a[i][j]==a[i+1][j]) { a[i][j]+=a[i+1][j]; a[i+1][j]=0; for(n=i+1;n<3;n++) { a[n][j]=a[n+1][j]; a[n+1][j]=0; } i=0; } else i++; } } case 4:/*按下D键*/ for(j=0;j<4;j++) { i=3; while(a[i][j]==0) { while(i>0) { a[i][j]=a[i-1][j]; a[i-1][j]=0; } i=3; } for(i=3;i>0;) { int n; if(a[i][j]==a[i-1][j]) { a[i][j]+=a[i-1][j]; a[i-1][j]=0; for(n=i-1;n>0;n--) { a[n][j]=a[n-1][j]; a[n-1][j]=0; } i=3; } else i--; } } } } int WinMain() { int c=0,b=0; int ch; initgraph(640,480); setcaption("2048"); setfont(20,0,"宋体"); menu(); ch=getch(); cleardevice(); while(1) { gezi(); Sleep(100); srand(time(NULL));/*得到重新出现的位置*/ c=rand()%4; b=rand()%4; while(1) { int m=0; if(a[b][c]==0) { a[b][c]=2; m++; } else { b=rand()%4; c=rand()%4; } if(m==2) break; } show(); Sleep(100); for(i=0;i<4;i++) for(j=0;j<4;j++) { int z=0; if(a[i][j]==1024) { setcolor(RED); cleardevice(); outtextxy(280,240,"YOU WIN!"); getch(); closegraph(); return 0; } if(a[i][j]!=0) { z++; } if(z==16) { outtextxy(280,240,"YOU LOSE!"); return 0; } } ch=getch(); if(ch==27) break; else { switch(ch) { case 87: change(1); break; case 83: change(2); break; case 65: change(3); break; case 68: change(4); break; } } Sleep(100); cleardevice(); } cleardevice(); setcolor(EGERGB(0xff,0,0)); outtextxy(240,220,"是否退出?"); getch(); closegraph(); return 0; }