Home > other >  2048 game code
2048 game code

Time:02-28

Package text2. Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. The awt. Event. KeyEvent;
Import the Java. The awt. Event. KeyListener;
Import the Java. The awt. Event. MouseEvent;
Import the Java. The awt. Event. MouseListener;
Import the Java. SQL. The Date;
Import the Java. Util. Random;

The import javax.mail. Swing. ImageIcon;
The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
The import javax.mail. Swing. JLabel;
The import javax.mail. Swing. JMenu;
The import javax.mail. Swing. JMenuBar;
The import javax.mail. Swing. JMenuItem; Public class MyJframeSuper extends JFrame implements KeyListener, ActionListener {
Public static void main (String [] args) {new MyJframeSuper () painJframe ();
}//the following three is convenient to move to monitor the skinning judgment JMenuItem jMenuItem1=new JMenuItem (" classic "); JMenuItem jMenuItem2=new JMenuItem (" neon blue "); JMenuItem jMenuItem3=new JMenuItem (" candy ");//here is defined in the skin of the String String STR="A -"; Int score=0; The int data [] []=new int [4] [4].//judging from top to bottom and move around the overall effect, if mobile failure is game
Public void check () {if (checkLeft ()==false & amp; & CheckRight ()==false & amp; & Checkups ()==false & amp; & CheckDown ()==false) {System. Out. Println (" games "); }}/copy/array method
Public void copyArray (int SRC [] [], int dest [] []) {for (int I=0; I & lt; The SRC. Length; {i++) for (int j=0; J & lt; The SRC [I]. Length; J++) {dest [I] [j]=SRC [I] [j]; }}
}//every move will produce new picture
Public void frushPainting () {int frushI []={1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; Int frushj []={1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; Int w=0; For (int I=0; I & lt; Data. The length; {i++) for (int j=0; J & lt; Data [I]. Length; J++) {if (data [I] [j]==0) {frushI [w]=I; FrushI [w]=j; W++; }}} the if (w!=0) {Random r=new Random (); Int index=r.n extInt (w); Int x=frushI [index]; Int y=frushI [index]; Data [x] [y]=2; }}//have two random picture
Public void iniData () {frushPainting (); FrushPainting ();
}//inspect left mobile
Public Boolean checkLeft () {//create an array of type int newArray [] []=new int [4] [4].//define a method used to copy an array copyArray (data, newArray);//call left shift method left ();//set a label a Boolean flag=false;//to judge the lo again: for (int I=0; I & lt; Data. The length; {i++) for (int j=0; J & lt; Data [I]. Length; J++) {if (data [I] [j].=newArray [I] [j]) {flag=true; Break lo. }}}//and then back to switch array, array copy words method copyArray (newArray, data); Return flag; }//check the right public Boolean checkRight () {//create an array of type int newArray [] []=new int [4] [4].//define a method used to copy an array copyArray (data, newArray);//call moves to the right method horizonReserve ();//set a label a Boolean flag=false;//to judge the lo again: for (int I=0; I & lt; Data. The length; {i++) for (int j=0; J & lt; Data [I]. Length; J++) {if (data [I] [j].=newArray [I] [j]) {flag=true; Break lo. }}}//and then back to switch array, array copy words method copyArray (newArray, data); Return flag; }//check on mobile public Boolean checkups () {//create an array of type int newArray [] []=new int [4] [4].//define a method used to copy an array copyArray (data, newArray);////to call up method anti-clockwise rotating anticlockwise ();//then calls the left shift left ();//the last turn clockwise clockwise ();//set a label a Boolean flag=false;//to judge the lo again: for (int I=0; I & lt; Data. The length; {i++) for (int j=0; J & lt; Data [I]. Length; J++) {if (data [I] [j].=newArray [I] [j]) {flag=true; Break lo. }}}//and then back to switch array, array copy words method copyArray (newArray, data); Return flag; }//check mobile public Boolean checkDown () {//create an array of type int newArray [] []=new int [4] [4].//define a method used to copy an array copyArray (data, newArray);////call down method first clockwise turn clockwise ();//then calls the right horizonReserve ();//the last turn counterclockwise anticlockwise ();//set a label a Boolean flag=false;//to judge the lo again: for (int I=0; I & lt; Data. The length; {i++) for (int j=0; J & lt; Data [I]. Length; J++) {if (data [I] [j].=newArray [I] [j]) {flag=true; Break lo. }}}//and then back to switch array, array copy words method copyArray (newArray, data); Return flag; }//draw the avi menu bar public void printinfjmenubar () {JMenuBar jb=new JMenuBar (); JMenu jMenu1=new JMenu (" change skin "); JMenu jMenu2=new JMenu (" about us "); Jb. The add (jMenu1); Jb. The add (jMenu2); JMenu1. Add (jMenuItem1); JMenu1. Add (jMenuItem2); JMenu1. Add (jMenuItem3); Enclosing setJMenuBar (jb); JMenuItem1. AddActionListener (this); JMenuItem2. AddActionListener (this); JMenuItem3. AddActionListener (this); }//drawing form public void painJframe () {//drawn form this. SetTitle (" my drawing "); Printinfjmenubar (); Enclosing setSize (514, 538); Enclosing addKeyListener (this); Enclosing setLocation (150, 100); Enclosing setDefaultCloseOperation (EXIT_ON_CLOSE); Enclosing setVisible (true);//this is to form displayed iniData (); Painting (); }
//draw the form background
Public void painting () {getContentPane () removeAll ();//remove all interface for (int j=0; J & lt; 4. {j++) for (int I=0; I & lt; 4. I++) {JLabel jb=new JLabel (new ImageIcon (" E: \ \ o peng apeng \ \ Jimage \ \ A - "+ data [j] [I] +" PNG "));//from jb imported images folder. SetBounds (50 + 100 * I, 50 + 100 * j, 100, 100);//sets the position of picture enclosing getContentPane (). The add (jb);//add images to form}}//here is to set up a form's background JLabel jb1=new JLabel (new ImageIcon (" E: \ \ o peng apeng \ \ Jimage_background \ \ "" + STR +" 2. PNG ")); Jb1. SetBounds (40, 40, 420, 420); JLabel jb2=new JLabel (" score "+" : "+ score).//it is to define the score of jb2. SetBounds (40, 20, 100, 20); Enclosing getContentPane (). The add (jb2); Enclosing getContentPane (). The add (jb1); GetContentPane () repaint ();//refresh interface
}
//the photo down
Public void clockwise () {//this is clockwise int newArray [] []=new int [4] [4]. For (int I=0; I & lt; 4. {i++) for (int j=0; J & lt; 4. J++) {newArray [j] [3 -] I=data [I] [j]; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related