2048 Java implementation of the game

Source: Internet
Author: User
Tags abs

Write a more hurried, more simple function, the interface is ugly, the operation of the number of steps should also be optimized.

Ultimately, it is to write AI, but it seems not so simple, write a few about the pattern evaluation function, you have to continue to learn ...


Left and RIGHT ARROW key operation.


Source:

Import Java.awt.borderlayout;import java.awt.color;import Java.awt.font;import Java.awt.gridlayout;import Java.awt.event.*;import Java.util.random;import Javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.joptionpane;import Javax.swing.jpanel;import Javax.swing.jtextfield;import Javax.swing.border.titledborder;public class My2048 extends JFrame {JPanel Numpanel = new JPanel (); JPanel Scorepanel = new JPanel (); JLabel numlabellist[][] = new jlabel[4][4];//number lattice int numlist[][] = new int[4][4];//array lattice corresponding value int blanks = 16;//blank lattice int s core=0;//Total score JLabel scorelabel=new JLabel (); int numlist2[][] = new int[4][4];//array int smoothweight=1;int for landscape evaluation Monoweight=10;int emptyweight=27;int maxweight=10;public static void Main (string[] args) {new My2048 (). Launchframe (); public void Addnumarea () {for (int i = 0; i < 4; i++) for (int j = 0; J < 4; J + +) {Numlabellist[i][j] = new JLabel (); NUMLIST[I][J] = 0;numlabellist[i][j].setbackground (Color.light_gray); Numlabellist[i][j].setborder (newTitledborder ("")); Numlabellist[i][j].setfont (New Font ("Romantic", Font.Bold, +)); Numlabellist[i][j]. Sethorizontalalignment (Jtextfield.center); Numpanel.add (Numlabellist[i][j]);}} public void Addscorelabel () {scorelabel.setbackground (Color.magenta); Scorelabel.setfont (New Font ("Romantic", Font.Bold) scorelabel.sethorizontalalignment (jtextfield.center); Scorelabel.settext ("Score:" + Integer.tostring (score)); Scorepanel.add (Scorelabel);} Randomly select a blank lattice public int randomindex (int blanks) {Random random = new Random (System.currenttimemillis ()); int result = Random . Nextint (Blanks) +1;return result;} 2,4 randomly select a public int randomvalue () {Random random = new random (); int result = Random.nextint (2); return (result+1) * *;} A new value is generated in the white space, with the refresh function public void NewValue () {int newvalue = Randomvalue ();//value int index = RANDOMINDEX (blanks);//New worthwhile location B lanks--;//Blank lattice number -1for (int i = 0; i < 4; i++) for (int j = 0; J < 4; J + +) {if (numlist[i][j] = = 0) index--;if (index = = 0) {Numlist[i][j] = newvalue;//Displays the new value on the corresponding label nUmlabellist[i][j].settext (Integer.tostring (newvalue); return;}}} public Boolean Leftreduce () {Boolean changed=false;//whether there is a change (move and add), as follows whether to produce a new merit-based Boolean has0before;int p;//row pointer int value;//Current value for (int i = 0; i < 4; i++) {p=0;value=0;has0before=false;for (int j = 0; J < 4; J + +) {if (numlist[i][j]&gt 0&&has0before)//before moving in the direction of a space, movable, changed for Truechanged=true;if (numlist[i][j]==0) has0before=true;if ( NUMLIST[I][J] > 0) {if (Numlist[i][j]==value)//Add put p position {int sum=value*2;numlist[i][j]=0;numlist[i][p]=sum;value=0; p++;score+=sum;changed=true;//Add, changed is True}else{if (value>0)//value put p, the current value is put in value {numlist[i][p]=value;p++ ;} value=numlist[i][j];numlist[i][j]=0;}}} if (value>0)//to the last value may have values numlist[i][p]=value;} return changed;} public Boolean Rightreduce () {Boolean changed=false;//whether there is a change (move and add), as follows whether to produce a new merit-based Boolean has0before;int p;//row pointer int value;//Current value for (int i = 0; I <4; i++) {p=3;value=0;has0before=false;for (int j = 3; J >= 0; j--) {if (numlist[i][j]&gt ; 0&&has0before)//Mobile sideThere are spaces before upward, movable, changed for Truechanged=true;if (numlist[i][j]==0) has0before=true;if (Numlist[i][j] > 0) {if (numlist[i][ J]==value)//Add put p position {int sum=value*2;numlist[i][j]=0;numlist[i][p]=sum;value=0;p--;score+=sum;changed=true;//Add, Changed for True}else{if (value>0)//value put p, the current value is put in value {numlist[i][p]=value;p--;} value=numlist[i][j];numlist[i][j]=0;}}} if (value>0)//to the last value may have values numlist[i][p]=value;} return changed;} public Boolean Upreduce () {Boolean changed=false;//whether there is a change (move and add), as follows whether to produce a new merit based Boolean has0before;int p;//row pointer int value ;//Current value for (int j = 0; J <4; J + +) {p=0;value=0;has0before=false;for (int i = 0; I <4; i++) {if (numlist[i][j]>0& &has0before)//before moving in the direction of a space, movable, changed for Truechanged=true;if (numlist[i][j]==0) has0before=true;if (Numlist[i][j] > 0) {if (Numlist[i][j]==value)//Add put p position {int sum=value*2;numlist[i][j]=0;numlist[p][j]=sum;value=0;p++;score+= sum;changed=true;//add, changed for True}else{if (value>0)//value put p, the current value is put in value {numlist[p][j]=value;p++;} value=numlist[i][j];numlist[i][j]=0;}}} if (value>0)//to the last value may have values numlist[p][j]=value;} return changed;} public Boolean Downreduce () {Boolean changed=false;//whether there is a change (move and add), as follows whether to produce a new merit-based Boolean has0before;int p;//row pointer int value;//Current value for (int j = 0; J <4; J + +) {p=3;value=0;has0before=false;for (int i = 3; I >=0; i--) {if (numlist[i][j]> 0&&has0before)//before moving in the direction of a space, movable, changed for Truechanged=true;if (numlist[i][j]==0) has0before=true;if (numlist I [j] > 0) {if (Numlist[i][j]==value)//Add put p position {int sum=value*2;numlist[i][j]=0;numlist[p][j]=sum;value=0;p--;score+ =sum;changed=true;//add, changed for True}else{if (value>0)//value put p, the current value is put in value {numlist[p][j]=value;p--;} value=numlist[i][j];numlist[i][j]=0;}}} if (value>0)//to the last value may have values numlist[p][j]=value;} return changed;} public void Refresh () {Blanks=0;scorelabel.settext ("Score:" +integer.tostring (Score)), for (Int. i=0;i<4;i++) for ( int j=0;j<4;j++) {if (numlist[i][j]!=0) Numlabellist[i][j].settext (integer.tostring (Numlist[i][j])); else{ Numlabellist[i][j].settext (null); blanks++;}}} public Boolean CheckOut () {if (blanks>0) return false;for (Int. i=0;i<4;i++) for (int j=0;j<3;j++) {if (numlist[i][ J]==numlist[i][j+1]) return false;} for (int j=0;j<4;j++) for (int i=0;i<3;i++) {if (Numlist[i][j]==numlist[i+1][j]) return false;} return true;} public void Out () {Joptionpane.showmessagedialog (null, "Game Over!") "," Version 2048PC ", 2); System.exit (0);} public void Printnumlist () {for (int i = 0, i < 4; i++) {for (int j = 0; J < 4; J + +) {System.out.print (Numlist[i][j]) ; System.out.print (');} System.out.println ();} System.out.println ("***********************");} Monotonic rate, late multiplied by monoweight (simply method inside) public int mononess () {int result1=0;//increment measure int result2=0;//from right to left, increment metric int lastvalue=0 from top to bottom; for (int i=0;i<4;i++) {lastvalue=0;for (int j=0;j<4;j++) {if (numlist[i][j]>0) {if (lastvalue>0) if (numlist[ I][j]<lastvalue) result1++;if (Numlist[i][j]>lastvalue) result1--;lastvalue=numlist[i][j];}}} lastvalue=0;for (int j=0;j<4;j++) {lastvalue=0;for (int i=0;i<4;i++) {if (numlist[i][j]>0) {if (lastvalue>0) if (numlist[i][j]>lastvalue) result2++;if (Numlist[i][j]<lastvalue) result2--;lastvalue=numlist[i][j];}} if (RESULT2&GT;RESULT1) return Result2*monoweight;return Result1*monoweight;} The smoothing rate, which is a negative number, is the inverse of the difference and the latter is multiplied by smoothweight (simply the method is multiplied) public int smoothness () {int result1=0;//horizontal int result2=0;//vertical direction int lastvalue=0;for (int i=0;i<4;i++) {lastvalue=0;for (int j=0;j<4;j++) {if (numlist[i][j]>0) {if (lastvalue>0 ) Result1+=math.abs (Numlist[i][j]-lastvalue); lastvalue=numlist[i][j];}} lastvalue=0;for (int j=0;j<4;j++) {lastvalue=0;for (int i=0;i<4;i++) {if (numlist[i][j]>0) {if (lastvalue>0 ) Result2+=math.abs (Numlist[i][j]-lastvalue); lastvalue=numlist[i][j];}} if (RESULT2&GT;RESULT1) return (0-RESULT1) *smoothweight;return (0-RESULT2) *smoothweight;} The maximum number, late multiplied by maxweight (simply the method is multiplied) public int maxness () {int max=0;for (int. i=0;i<4;i++) for (int j=0;j<4;j++) if (numlist I [J]>max] Max=numlist[i][j];return max*maxweight;} public int emptyness () {return blanks*emptyweight;} public void Launchframe () {sEttitle ("2048PC version"); Set the form title SetBounds (475), setlayout (New BorderLayout ()); setresizable (false); Disable resizing of form numpanel.setlayout (new GridLayout (4, 4)); Set the empty layout numpanel.setlocation (0,200); Addscorelabel ();//Add a scoring device Addnumarea ();//Add a digital lattice Getcontentpane (). Add (Scorepanel,borderlayout.north); Getcontentpane (). Add (Numpanel,borderlayout.center); NewValue (); NewValue (); This.addwindowlistener (new Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}}); This.addkeylistener (New Keyadapter () {public void keypressed (KeyEvent e) {if (e.getkeycode () = = 37)//left {if (Leftreduce () {refresh ();//update Blank,numlabellistnewvalue ();}} if (e.getkeycode () = = 39)//Right-click {if (Rightreduce ()) {Refresh ();//update Blank,numlabellistnewvalue ();}} if (e.getkeycode () = = 38)//on key {if (Upreduce ()) {Refresh ();//update Blank,numlabellistnewvalue ();}} if (e.getkeycode () = = 40)//on key {if (Downreduce ()) {Refresh ();//update Blanks,numlabellist,scorelabelnewvalue ();}} if (e.getkeycode () = = 32)//Space key {if (Leftreduce ()) {Refresh ();//Update blanks, Numlabellist,scorelabelnewvalue ();} else if (Rightreduce ()) {Refresh ();//update Blanks,numlabellist,scorelabelnewvalue ();} else if (Upreduce ()) {Refresh ();//update Blanks,numlabellist,scorelabelnewvalue ();} else if (Downreduce ()) {Refresh ();//update Blanks,numlabellist,scorelabelnewvalue ();}} System.out.println (E.getkeycode ()); if (CheckOut ())//Check whether the game ends out (); Printnumlist (); System.out.println ("Mononess:" +integer.tostring (Mononess ())); System.out.println ("Smoothness:" +integer.tostring (Smoothness ())); System.out.println ("Maxnum:" +integer.tostring (Maxness ())); System.out.println ("Blanks:" +integer.tostring (Emptyness ()));}); SetVisible (True);}}






2048 Java implementation of the game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.