该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JOptionPane;
public class Demo {
public static int[][] chess=new int[4][4];
public static int[][] chess1=new int[4][4];
static Font font1=new Font("黑体",Font.BOLD,64);
public static void deal(int d) {
account();
for(int i=0;i<=3;i++)
for(int j=0;j<=3;j++)
{
chess1[i][j]=chess[i][j];
}
switch(d) {
case 83:move1();break;
case 87:move2();break;
case 65:move3();break;
case 68:move4();break;
default:break;
}
int m=0;
for(int i=0;i<=3;i++)
for(int j=0;j<=3;j++)
{
if(chess1[i][j]!=chess[i][j]) {
m=m+1;
}
}
if(m>0) {
newChess();
}}
public static void move1() {
for(int m=0;m<=3;m++)
for(int i=3;i>0;i--)
for(int j=0;j<=3;j++)
if(chess[i][j]==0) {
chess[i][j]=chess[i-1][j];
chess[i-1][j]=0;
}
for(int i=3;i>0;i--)
for(int j=0;j<=3;j++)
if(chess[i][j]==chess[i-1][j]) {
chess[i][j]=chess[i][j]*2;
chess[i-1][j]=0;
}
for(int m=0;m<=3;m++)
for(int i=3;i>0;i--)
for(int j=0;j<=3;j++)
if(chess[i][j]==0) {
chess[i][j]=chess[i-1][j];
chess[i-1][j]=0;
}
}
public static void move2() {
for(int m=0;m<=3;m++)
for(int i=0;i<3;i++)
for(int j=0;j<=3;j++)
if(chess[i][j]==0) {
chess[i][j]=chess[i+1][j];
chess[i+1][j]=0;
}
for(int i=0;i<3;i++)
for(int j=0;j<=3;j++)
if(chess[i][j]==chess[i+1][j]) {
chess[i][j]=chess[i][j]*2;
chess[i+1][j]=0;
}
for(int m=0;m<=2;m++)
for(int i=0;i<3;i++)
for(int j=0;j<=3;j++)
if(chess[i][j]==0) {
chess[i][j]=chess[i+1][j];
chess[i+1][j]=0;
}
}
public static void move3() {
for(int m=0;m<=3;m++)
for(int j=0;j<3;j++)
for(int i=0;i<=3;i++)
if(chess[i][j]==0) {
chess[i][j]=chess[i][j+1];
chess[i][j+1]=0;
}
for(int j=0;j<3;j++)
for(int i=0;i<=3;i++)
if(chess[i][j]==chess[i][j+1]) {
chess[i][j]=chess[i][j]*2;
chess[i][j+1]=0;
}
for(int m=0;m<=3;m++)
for(int j=0;j<3;j++)
for(int i=0;i<=3;i++)
if(chess[i][j]==0) {
chess[i][j]=chess[i][j+1];
chess[i][j+1]=0;
}
}
public static void move4() {
for(int m=0;m<=3;m++)
for(int j=3;j>0;j--)
for(int i=0;i<=3;i++)
if(chess[i][j]==0) {
chess[i][j]=chess[i][j-1];
chess[i][j-1]=0;
}
for(int j=3;j>0;j--)
for(int i=0;i<=3;i++)
if(chess[i][j]==chess[i][j-1]) {
chess[i][j]=chess[i][j]*2;
chess[i][j-1]=0;
}
for(int m=0;m<=3;m++)
for(int j=3;j>0;j--)
for(int i=0;i<=3;i++)
if(chess[i][j]==0) {
chess[i][j]=chess[i][j-1];
chess[i][j-1]=0;
}
}
public static void newChess(){
Random r1=new Random();
int m=r1.nextInt(4);
int n=r1.nextInt(4);
if(chess[m][n]==0) {
int p=r1.nextInt(10);
if(p==9) {chess[m][n]=4;}else {chess[m][n]=2;}
}else {
account();
newChess();}
}
public static void account() {
int m=0;
for(int i=0;i<=3;i++)
for(int j=0;j<=3;j++) {
if(chess[i][j]==0)
{m++;}
}
for(int i=0;i<=2;i++)
for(int j=0;j<=3;j++) {
if(chess[i][j]==chess[i+1][j])
{m++;}
}
for(int i=0;i<=3;i++)
for(int j=0;j<=2;j++) {
if(chess[i][j]==chess[i][j+1])
{m++;}
}
if(m==0) {
JOptionPane.showMessageDialog(Day4.frame1, "游戏结束");
}else {
}
}
public static void drawChess(Graphics g) {
g.setFont(font1);
for(int i=0;i<=3;i++)
for(int j=0;j<=3;j++)
{
switch(chess[i][j]) {
case 0:
g.setColor(new Color(205,193,180));
g.fillRect(10+120*j, 35+120*i, 110, 110);
break;
case 2:
g.setColor(new Color(238,228,218));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(2+"", j*120+40, i*120+110);
break;
case 4:
g.setColor(new Color(237,224,200));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(4+"", j*120+40, i*120+110);
break;
case 8:
g.setColor(new Color(242,177,121));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(8+"", j*120+40, i*120+110);
break;
case 16:
g.setColor(new Color(245,149,99));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(16+"", j*120+30, i*120+110);
break;
case 32:
g.setColor(new Color(246,124,95));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(32+"", j*120+30, i*120+110);
break;
case 64:
g.setColor(new Color(246,94,59));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(64+"", j*120+30, i*120+110);
break;
case 128:
g.setColor(new Color(237,207,114));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(128+"", j*120+10, i*120+110);
break;
case 256:
g.setColor(new Color(237,204,97));
g.fillRect(10+120*j,35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(256+"", j*120+10, i*120+110);
break;
case 512:
g.setColor(new Color(237,200,80));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(512+"", j*120+10, i*120+110);
break;
case 1024:
g.setColor(new Color(240,195,68));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(1024+"", j*120-12, i*120+110);
break;
case 2048:
g.setColor(new Color(236,195,0));
g.fillRect(10+120*j, 35+120*i, 110, 110);
g.setColor(Color.black);
g.drawString(2048+"", j*120-12, i*120+110);
break;
}
}
}
}