import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import javax.swing.border.*;public class Test extends JApplet{ private static final int UP=0,DOWN=1,LEFT=2,RIGHT=3; private static final int WID=150,SP=10; private int scores=0; private static JLabel scoreLabel; private boolean change,CheckMode=false,gameOverFlag=false,successFlag=false; private int[] label={2,4,8,16,32,64,128}; private Color[] clo={ new Color(200,200,200),new Color(228,228,160),new Color(214,163,92), new Color(234,124,72),new Color(240,84,77),new Color(255,68,53),new Color(200,200,64) }; private Map cmap=new HashMap(); public static RectObject[][] rset=new RectObject[4][4]; public RectObject[][] list=new RectObject[4][4]; private My2048Panel myp; private LinkedList saveList=new LinkedList(); private JButton goBackButton; KeyListener kl=new KeyListener(){ public void keyPressed(KeyEvent e){ saveTheStep(); goBackButton.setVisible(true); if(gameOverFlag==true){ return; } if(!aDirAble()){ gameOver(); } int key=e.getKeyCode(); switch(key){ case KeyEvent.VK_UP: change=false; moveUp(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; case KeyEvent.VK_DOWN: change=false; moveDown(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; case KeyEvent.VK_LEFT: change=false; moveLeft(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; case KeyEvent.VK_RIGHT: change=false; moveRight(true); if(change==true){ getARandomRect(); //saveTheStep(); } break; } //saveTheStep(); } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} }; class RectObject{ private int value; public RectObject(){ value=0; } public RectObject(RectObject obj){ value=obj.value; } public boolean equals(Object inobj){ RectObject obj=(RectObject)inobj; if(obj.value==value){ return true; } return false; } } class Point{ int x; int y; public Point(int i,int j){ x=i; y=j; } } class My2048Panel extends JPanel{ private int[] xindex={SP,2*SP+WID,3*SP+2*WID,4*SP+3*WID}; private int[] yindex={SP,2*SP+WID,3*SP+2*WID,4*SP+3*WID}; public void paintComponent(Graphics g){ //background super.paintComponent(g); for(int i=0;i-1;j--){ if(rset[i][j]!=null){ list[i][k--]=new RectObject(rset[i][j]); } } } for(int i=0;i<4 && flag;i++){ for(int j=3;j>0;j--){ if(list[i][j]!=null && list[i][j-1]!=null && list[i][j].value==list[i][j-1].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i][j-1]=null; j--; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveRight(false); } change=true; }else{ repaint(); } } //moveup public void moveUp(boolean flag){ clearList(list); for(int j=0;j<4;j++){ int k=0; for(int i=0;i<4;i++){ if(rset[i][j]!=null){ list[k++][j]=new RectObject(rset[i][j]); } } } for(int j=0;j<4 && flag;j++){ for(int i=0;i<3;i++){ if(list[i][j]!=null && list[i+1][j]!=null && list[i][j].value==list[i+1][j].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i+1][j]=null; i++; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveUp(false); } change=true; }else{ repaint(); } } //movedown public void moveDown(boolean flag){ clearList(list); for(int j=0;j<4;j++){ int k=3; for(int i=3;i>-1;i--){ if(rset[i][j]!=null){ list[k--][j]=new RectObject(rset[i][j]); } } } for(int j=0;j<4 && flag;j++){ for(int i=3;i>0;i--){ if(list[i][j]!=null && list[i-1][j]!=null && list[i][j].value==list[i-1][j].value){ list[i][j].value*=2; if(CheckMode==false){ int sum=Integer.parseInt(scoreLabel.getText()); sum+=list[i][j].value; scoreLabel.setText(""+sum); } list[i-1][j]=null; i--; } } } if(isChange()){ if(CheckMode==false){ copySet(rset,list); repaint(); moveDown(false); } change=true; }else{ repaint(); } } //other functions private void copySet(RectObject[][] dst, RectObject[][] src){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ dst[i][j]=src[i][j]; } } } //detect whether rset is different from list or not private boolean isChange(){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(rset[i][j]!=null && list[i][j]!=null && !rset[i][j].equals(list[i][j])){ return true; } if(rset[i][j]!=null && list[i][j]==null){ return true; } if(rset[i][j]==null && list[i][j]!=null){ return true; } } } return false; } private void clearList(RectObject[][] s){ for(int i=0;i best){ file.writeInt(cur); } file.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); }catch(IOException e2){ e2.printStackTrace(); } } public static void saveRecord(){ try{ RandomAccessFile file=new RandomAccessFile(new File("LASTRECORD"),"rw"); int score=Integer.parseInt(scoreLabel.getText()); file.writeInt(score); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(rset[i][j]!=null){ file.writeInt(rset[i][j].value); }else{ file.writeInt(0); } } } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } public static void run(JApplet applet,int width,int height){ JFrame frame=new JFrame(title(applet)); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ refreshBest(); saveRecord(); //System.out.println("The score is "+scoreLabel.getText()); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(applet); frame.setSize(width,height); applet.init(); applet.start(); frame.setVisible(true); } public static void main(String[] args){ run(new Test(), 900, 800); }}