2048 Java Implementation of games and 2048 Java Implementation of games

Source: Internet
Author: User

2048 Java Implementation of games and 2048 Java Implementation of games

Writing is in a hurry, the function is relatively simple, the interface is ugly, and the number of operation steps should be optimized.

In the end, I still want to write AI, but it seems that it is not that simple. I wrote a few functions about pattern evaluation and I still have to continue learning...


The up, down, and left direction keys are operated.


Source code:

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 SC Orepanel = new JPanel (); JLabel numlabellist [] [] = new JLabel [4] [4]; // number lattice int numlist [] [] = new int [4] [4]; // The array lattice corresponds to the value int blks = 16; // Number of blank cells int score = 0; // total score JLabel scorelabel = new JLabel (); int numlist2 [] [] = new int [4] [4]; // array int smoothweight = 1; int 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 (new TitledBorder (""); numlabellist [I] [j]. setFont (new Font ("Romantic", Font. BOLD, 35); numlabellist [I] [j]. setHorizontalAlignment (JTextField. CENTER); numpanel. add (numlabellist [I] [j]) ;}} public void AddScoreLabel () {s Corelabel. setBackground (Color. MAGENTA); scorelabel. setFont (new Font ("Romantic", Font. BOLD, 35); scorelabel. setHorizontalAlignment (JTextField. CENTER); scorelabel. setText ("score:" + Integer. toString (score); scorepanel. add (scorelabel);} // randomly select a blank grid public int RandomIndex (int blks) {Random random = new Random (System. currentTimeMillis (); int result = random. nextInt (blanks) + 1; return result;} // select a publ at random for 2, 4. Ic int RandomValue () {Random random = new Random (); int result = random. nextInt (2); return (result + 1) * 2;} // a new value is generated in the blank space. The public void NewValue () {int newvalue = RandomValue (); // new value: int index = RandomIndex (blanks); // new value position: blanks --; // Number of blank cells-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; // The New Value numlabellist [I] [J]. setText (Integer. toString (newvalue); return ;}} public boolean LeftReduce () {boolean changed = false; // whether there is a change (move and add ), whether to generate a new value in the future based on boolean has0before; int p; // The row pointer int value; // The 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]> 0 & has0before) // there is a space before moving the direction, movable, changed to truechanged = true; if (numlist [I] [j] = 0) has0before = true; if (numlist [I] [j]> 0) {if (num List [I] [j] = value) // Add the P position {int sum = value * 2; numlist [I] [j] = 0; numlist [I] [p] = sum; value = 0; p ++; score + = sum; changed = true; // sum, changed to true} else {if (value> 0) // put the value in p, and put the current value in value {numlist [I] [p] = value; p ++ ;} value = numlist [I] [j]; numlist [I] [j] = 0 ;}} if (value> 0) // The value numlist [I] [p] = value;} return changed;} public boolean RightReduce () {boolean changed = false; // whether there is a change (moving and adding), as to whether to generate a new value in the future based on boolean has0before; int p; // The row pointer int value; // The 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]> 0 & has0before) // there is a space before moving the direction, movable, changed to truechanged = true; if (numlist [I] [j] = 0) has0before = true; if (numlist [I] [j]> 0) {if (numlist [I] [j] = value) // Add P position {int sum = value * 2; numlist [I] [j] = 0; numlist [I] [p] = sum; value = 0; p --; score + = sum; changed = true; // sum, changed to true} else {if (value> 0) // value Put p, and put the current value in value {numlist [I] [p] = value; p --;} value = numlist [I] [j]; numlist [I] [j] = 0 ;}} if (value> 0) // The value numlist [I] [p] = value in the final value ;} return changed;} public boolean UpReduce () {boolean changed = false; // whether there is a change (moving and adding), as to whether to generate a new value in the future based on boolean has0before; int p; // The row pointer int value; // The 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) // there is a space before moving the direction, removable, cha Nged is truechanged = true; if (numlist [I] [j] = 0) has0before = true; if (numlist [I] [j]> 0) {if (numlist [I] [j] = value) // Add P position {int sum = value * 2; numlist [I] [j] = 0; numlist [p] [j] = sum; value = 0; p ++; score + = sum; changed = true; // sum, changed to true} else {if (value> 0) // put the value in p, and put the current value in value {numlist [p] [j] = value; p ++ ;} value = numlist [I] [j]; numlist [I] [j] = 0 ;}} if (value> 0) // The final value of numlist [p] [j] = value;} return changed;} public boolean Do WnReduce () {boolean changed = false; // whether there is a change (moving and adding), as to whether to generate a new value in the future based on boolean has0before; int p; // The int value of the row pointer; // 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) // there is a space before moving the direction. It can be moved, changed to truechanged = true; if (numlist [I] [j] = 0) has0before = true; if (numlist [I] [j]> 0) {if (numlist [I] [j] = value) // Add P position {int sum = value * 2; numlist [I] [j] = 0; numlist [p] [J] = sum; value = 0; p --; score + = sum; changed = true; // sum, changed to true} else {if (value> 0) // put the value in p, and the current value in value {numlist [p] [j] = value; p --;} value = numlist [I] [j]; numlist [I] [j] = 0 ;}} if (value> 0) // the final value of 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. showMessageDialo G (null, "the game is over! "," 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, multiply the value of monoweight in the later stage (simply in the method) public int Mononess () {int result1 = 0; // The int result2 = 0 from right to left; // The 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) 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> result1) return result2 * monoweight; return result1 * monoweight;} // smoothing rate, which is a negative number. It is the opposite number of Difference values. It is multiplied by smoothweight (simply in the method). public int Smoothness () {int result1 = 0; // horizontal int result2 = 0; // vertical 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> result1) return (0-result1) * smoothweight; return (0-result2) * smoothweight;} // the maximum number. Multiply the value by maxweight (the value is multiplied by the value in the method) 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"); // set the Form title setBounds (700,100,400,475); setLayout (new BorderLayout (); setResizable (false ); // disable the numpanel function. setLayout (new GridLayout (4, 4); // sets the empty layout numpanel. setLocation (0,200); AddScoreLabel (); // Add the analyzer AddNumArea (); // Add the number grid getContentPane (). add (scorepanel, BorderLayout. NORTH); getContentPane (). add (numpanel, BorderLayout. CENTER); NewValue (); this. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); this. addKeyListener (new KeyAdapter () {public void keyPressed (KeyEvent e) {if (e. getKeyCode () = 37) // left click {if (LeftReduce () {Refresh (); // update blank, numlabellistNewValue () ;}} if (e. getKeyCode () = 39) // right-click {if (RightReduce () {Refresh (); // update blank, numlabellistNewValue () ;}} if (e. getKeyCode () = 38) // press {if (UpReduce () {Refresh (); // update blank, numlabellistNewValue () ;}} if (e. getKeyCode () = 40) // The upper key {if (DownReduce () {Refresh (); // update blks, 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 );}}






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.