2048程序
//声明#include#include#include#include#include#define SIZE 4static int score=0;void putn(int n[][SIZE]);void getn(int n[][SIZE]);int isempty(int n[][SIZE]);int isfull(int n[][SIZE]);void math(int n[][SIZE],char c);void tow(int n[][SIZE]);void toa(int n[][SIZE]);void tos(int n[][SIZE]);void tod(int n[][SIZE]);
//主函数int main(){int i,j;int n[SIZE][SIZE];char c=' ';for(i=0;i//函数void putn(int n[][SIZE]){int i,j;for(i=0;i0?0:1;}void math(int n[][SIZE],char c){switch(c){case 'w':tow(n);break;case 'a':toa(n);break;case 's':tos(n);break;case 'd':tod(n);break;default :;}}void tow(int n[][SIZE]){int i,j,a;int m[SIZE];for(a=0;a=0;j--){for(a=SIZE-1;a>=0;a--){for(i=SIZE-1;i>0;i--){if(n[i][j]==0){n[i][j]=n[i-1][j];n[i-1][j]=0;}}}}for(j=SIZE-1;j>=0;j--){for(a=SIZE-1,i=SIZE-1;i>=0;i--){if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048){m[a--]=n[i][j];n[i][j]=0;}else if(n[i][j]==n[i-1][j]){m[a--]=n[i][j]+n[i-1][j];score+=m[a+1];n[i][j]=0,n[i-1][j]=0;}}for(i=SIZE-1;i>=0;i--){n[i][j]=m[i];m[i]=0;}}}void tod(int n[][SIZE]){int i,j,a;int m[SIZE];for(a=0;a=0;i--){for(a=SIZE-1;a>=0;a--){for(j=SIZE-1;j>0;j--){if(n[i][j]==0){n[i][j]=n[i][j-1];n[i][j-1]=0;}}}}for(i=SIZE-1;i>=0;i--){for(a=SIZE-1,j=SIZE-1;j>=0;j--){if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048){m[a--]=n[i][j];n[i][j]=0;}else if(n[i][j]==n[i][j-1]){m[a--]=n[i][j]+n[i][j-1];score+=m[a+1];n[i][j]=0,n[i][j-1]=0;}}for(j=SIZE-1;j>=0;j--){n[i][j]=m[j];m[j]=0;}}}