标签:
package game; import java.util.Random; public class game{ int pane[][]=new int[4][4]; //棋盘 void showpane() //显示棋盘 { for(int a=0;a<=40;a++){ System.out.println(); } System.out.printf("%50s\n"," |-----------------------|"); for(int i=0;i<=3;i++) { System.out.printf("%24s",""); for(int j=0;j<=3;j++) { if(pane[i][j]==0) System.out.printf("%2s%4s","|"," "); else System.out.printf("%2s%4d","|",pane[i][j]); if(j==3) { System.out.printf("%2s\n","|"); System.out.printf("%50s\n"," |-----------------------|"); } } } System.out.printf("%50s\n","按w-向上移动;s-向下移动;a-向左移动;d-向右移动"); int gameOver=1;// 判断游戏结束条件 for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(pane[i][j]==0){ gameOver=0; } } } if(gameOver==1){ System.out.println("------游戏结束-----------游戏结束-----"); System.exit(0); } } void newgame() //开始游戏 { for(int i=0;i<=3;i++) //初始化棋盘 for(int j=0;j<=3;j++) pane[i][j]=0; Random random = new Random(); int m=random.nextInt(4);//产生一个0到4(不包括4的随机数) int n=random.nextInt(4); int p=random.nextInt(4); int q=random.nextInt(4); //在随机的位置上产生初值2 pane[m][n]=pane[p][q]=2; showpane(); } void up(){//向上操作 int i,j,three; for(j=0;j<4;j++){ //上移动 three=0; for(i=0;i<4;i++){ if(pane[i][j]>0){ int t=pane[i][j]; pane[i][j]=0; pane[three][j]=t; three++; } } } for(j=0;j<4;j++){ //合并数 for(i=0;i<3;i++){ if(pane[i][j]==pane[i+1][j]&&pane[i][j]!=0){ pane[i][j]=pane[i][j]*2; pane[i+1][j]=0; int k=i+1; for(;k+1<=3&&pane[k+1][j]>0;k++){ pane[k][j]=pane[k+1][j]; pane[k+1][j]=0; } } } } random();//产生随机数 showpane();//刷新棋盘 } void low(){ //向下 int i,j,three; for(j=0;j<4;j++){//下移动 three=3; for(i=3;i>=0;i--){ if(pane[i][j]>0){ int t=pane[i][j]; pane[i][j]=0; pane[i+three-i][j]=t; three--; } } } for(j=0;j<4;j++){//合并数 for(i=3;i>0;i--){ if(pane[i][j]==pane[i-1][j]&&pane[i][j]!=0){ pane[i][j]=pane[i][j]*2; pane[i-1][j]=0; int k=i-1; for(;k-1>=0&&pane[k-1][j]>0;k--){ pane[k][j]=pane[k-1][j]; pane[k-1][j]=0; } } } } random(); showpane(); } void left(){//向左 int i,j,three; for(i=0;i<4;i++){//左移动 three=0; for(j=0;j<4;j++){ if(pane[i][j]>0){ int t=pane[i][j]; pane[i][j]=0; pane[i][three]=t; three++; } } } for(i=0;i<4;i++){//合并数 for(j=0;j<3;j++){ if(pane[i][j]==pane[i][j+1]&&pane[i][j]!=0){ pane[i][j]=pane[i][j]*2; pane[i][j+1]=0; int k=j+1; for(;k+1<=3&&pane[i][k+1]>0;k++){ pane[i][k]=pane[i][k+1]; pane[i][k+1]=0; } } } } random(); showpane(); } void right(){//右移 int i,j,three; for(i=0;i<4;i++){ three=3; for(j=3;j>=0;j--){//右移动 if(pane[i][j]>0){ int t=pane[i][j]; pane[i][j]=0; pane[i][j+three-j]=t; three--; } } } for(i=0;i<4;i++){//合并数 for(j=3;j>0;j--){ if(pane[i][j]==pane[i][j-1]&&pane[i][j]!=0){ pane[i][j]=pane[i][j]*2; pane[i][j-1]=0; int k=j-1; for(;k-1>=0&&pane[i][k-1]>0;k--){ pane[i][k]=pane[i][k-1]; pane[i][k-1]=0; } } } } random(); showpane(); } void random(){//产生随机数 Random random = new Random(); int m=random.nextInt(4);//产生一个0到4(不包括4的随机数) int n=random.nextInt(4); for(;pane[m][n]!=0;){ m=random.nextInt(4);//产生一个0到4(不包括4的随机数) n=random.nextInt(4); } pane[m][n]=2; } }
标签:
原文地址:http://www.cnblogs.com/vhyc/p/5936931.html