1 packageGame;2
3 import javax.swing.*;4 import java.awt.*;5 import java.awt.event.*;6 importjava.io.IOException;7 importjava.net.URL;8 importjava.util.Random;9
10 @SuppressWarnings("BooleanMethodIsAlwaysInverted")11 public class Myframe extends JFrame implementsKeyListener ,ActionListener, MouseListener {12
13 int[][] photos = new int[4][4];14
15 int fail = 0;16 long score = 0;17 String theme = "A-";18
19 JMenuItem xin = new JMenuItem("新游戏");20 JMenuItem item1 = new JMenuItem("经典");21 JMenuItem item2 = new JMenuItem("炫光");22 JMenuItem item3 = new JMenuItem("糖果");23 JMenuItem item4 = new JMenuItem("手写");24 JMenuItem jifen = new JMenuItem("修改积分");25 JMenuItem editphoto = new JMenuItem("修改数据");26 JMenuItem person = new JMenuItem("制作人员");27
28 JButton browse = new JButton("Don't touch");29 publicMyframe()30 {31 //初始化窗体
32 start_frame();33 //初始化菜单栏
34 initMenu();35 //初始化数据
36 initPhoto();37 //绘制界面
38 paint_view();39 //为窗体添加键盘监听
40 addKeyListener(this);41 setVisible(true);42
43 }44 //设置窗体初始化
45 public voidstart_frame()46 {47 //设置窗体大小
48 setSize(514,538);49 //设置窗体居中
50 setLocationRelativeTo(null);51 //设置窗体置顶52 //setAlwaysOnTop(true);53 //设置关闭模式
54 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);55 //设置窗体标题
56 setTitle("2048Game (欢迎您的使用)");57 //取消默认布局
58 setLayout(null);59 }60 //绘制窗口
61 public voidpaint_view()62 {63 URL imgURL;//根据不同地址取程序运行的根目录
64 getContentPane().removeAll();65 if(fail==1)66 {67 imgURL = Myframe.class.getResource("image/"+theme+"lose.png");68 JLabel lose = new JLabel(newImageIcon(imgURL));69 lose.setBounds(85,100,334,228);70 getContentPane().add(lose);71 }72 for(int i=0; i<4; i++)73 {74 for(int j=0; j<4; j++)75 {76 imgURL = Myframe.class.getResource("image/"+theme+photos[i][j]+".png");77 JLabel image = new JLabel(newImageIcon(imgURL));78 image.setBounds(50+100*j,50+100*i,100,100);79 getContentPane().add(image);80 }81 }82 imgURL = Myframe.class.getResource("image/"+theme+"Background.jpg");83 JLabel background = new JLabel(newImageIcon(imgURL));84 background.setBounds(40,40,420,420);85 getContentPane().add(background);86
87
88 JLabel scorelabel = new JLabel("积分:");89 scorelabel.setBounds(50,15,70,20);90 scorelabel.setFont(new Font("Dialog", Font.BOLD,20));91 scorelabel.setForeground(Color.blue);92 getContentPane().add(scorelabel);93
94 scorelabel = new JLabel(" "+score);95 scorelabel.setBounds(100,16,160,20);96 scorelabel.setFont(new Font("Dialog", Font.BOLD,20));97 scorelabel.setForeground(Color.red);98 getContentPane().add(scorelabel);99
100 browse.setBounds(350,16,150,16);101 browse.setFont(new Font("行书",Font.BOLD,5));102 browse.addMouseListener(this);103 getContentPane().add(browse);104
105 imgURL = Myframe.class.getResource("image/Back.jpg");106 JLabel back = new JLabel(newImageIcon(imgURL));107 back.setBounds(0,0,1080,1920);108 getContentPane().add(back);109
110 getContentPane().repaint();111 }112
113 @Override114 public voidkeyTyped(KeyEvent e) {115
116 }117
118 //事件监听器
119 @Override120 public voidkeyPressed(KeyEvent e)121 {122 int keycode =e.getKeyCode();123 switch(keycode)124 {125 case 37 ->
126 {127 leftmove();128 randomnum();129 }130 case 38 ->
131 {132 upmove();133 randomnum();134 }135 case 39 ->
136 {137 rightmove();138 randomnum();139 }140 case 40 ->
141 {142 downmove();143 randomnum();144 }145 }146 if(isFailure())147 {148 fail = 1;149 }150 if(issuccess())151 {152 JOptionPane.showMessageDialog(null,"成功达到2048!太厉害了!","Congratulations!",JOptionPane.INFORMATION_MESSAGE);153 newMadePerson();154 }155 paint_view();156
157 }158
159
160 @Override161 public voidkeyReleased(KeyEvent e) {162
163 }164
165 public voidleftmove()166 {167 for(int x=0; x<4; x++)168 {169 int[] temp = new int[4];170 int k = 0;171 //后置0元素
172 for(inti:photos[x])173 {174 if(i!=0)175 {176 temp[k++]=i;177 }178 }179 photos[x]=temp;180
181 for(int i = 0; i<3; i++)182 {183 if(photos[x][i]==photos[x][i+1])184 {185 photos[x][i]*=2;186 score+=photos[x][i];187
188 System.arraycopy(photos[x], i + 1 + 1, photos[x], i + 1, 3 - (i + 1));189 photos[x][3]=0;190 }191 }192
193 }194 }195
196 public voidrightmove()197 {198 for(int i=0; i<4 ;i++)199 {200 reverse(photos[i]);201 }202 leftmove();203 for(int i=0; i<4 ;i++)204 {205 reverse(photos[i]);206 }207 }208
209 public voidupmove()210 {211 anticlockwise();212 leftmove();213 clockwise();214 }215
216 public voiddownmove()217 {218 clockwise();219 leftmove();220 anticlockwise();221 }222
223 public void reverse(int[]a)224 {225 for(int start=0,end=a.length-1; start
233 public voidclockwise()234 {235 int[][] a = new int[4][4];236 for(int i=0; i<4; i++)237 {238 for(int j=0; j<4; j++)239 {240 a[i][j] = photos[3-j][i];241 }242 }243 System.arraycopy(a, 0, photos, 0, 4);244 }245
246 public voidanticlockwise()247 {248 int[][] a = new int[4][4];249 for(int i=0; i<4; i++)250 {251 for(int j=0; j<4; j++)252 {253 a[i][j] = photos[j][3-i];254 }255 }256 System.arraycopy(a, 0, photos, 0, 4);257 }258
259 public booleanisFailure()260 {261 long v_score =score;262 int[][] temp = new int[4][4];263 copyArray(temp, photos);264 //left judge
265 leftmove();266 if(!equalArray(temp,photos))267 {268 copyArray(photos,temp);269 score =v_score;270 return false;271 }272 //right judge
273 rightmove();274 if(!equalArray(temp,photos))275 {276 copyArray(photos,temp);277 score =v_score;278 return false;279 }280 //up judge
281 upmove();282 if(!equalArray(temp,photos))283 {284 copyArray(photos,temp);285 score =v_score;286 return false;287 }288 //down judge
289 downmove();290 if(!equalArray(temp,photos))291 {292 copyArray(photos,temp);293 score =v_score;294 return false;295 }296 score =v_score;297 return true;298 }299
300 public void copyArray(int[][] dest, int[][] src)301 {302 int line =src.length;303 int column =src.length;304 for(int i=0; i
310 public boolean equalArray(int[][] a, int[][] b)311 {312 int line =a.length;313 int column = a[0].length;314 for(int i=0; i
327 public voidrandomnum()328 {329 int[] a = new int[16];330 int[] b = new int[16];331 int k = 0;332 for(int i =0; i<16; i++)333 {334 a[i]=-1;335 b[i]=-1;336 }337 for (int i = 0; i < 4; i++)338 {339 for(int j=0; j < 4; j++)340 {341 if(photos[i][j]==0)342 {343 a[k] =i;344 b[k] =j;345 k++;346 }347 }348 }349 if(k!=0)350 {351 Random r = newRandom();352 int index =r.nextInt(k);353 int x =a[index];354 int y =b[index];355 int value = Math.random()<0.9? 2 : 4;356 photos[x][y] =value;357 }358 }359
360 public voidinitPhoto()361 {362 randomnum();363 randomnum();364 }365
366 public voidinitMenu()367 {368 JMenuBar menuBar = newJMenuBar();369
370 JMenu menu1 = new JMenu("开始");371 JMenu menu2 = new JMenu("皮肤");372 JMenu menu3 = new JMenu("修改器");373 JMenu menu4 = new JMenu("制作人员");374
375 menuBar.add(menu1);376 menuBar.add(menu2);377 menuBar.add(menu3);378 menuBar.add(menu4);379
380 menu1.add(xin);381 menu2.add(item1);382 menu2.add(item2);383 menu2.add(item3);384 menu2.add(item4);385 menu3.add(jifen);386 menu3.add(editphoto);387 menu4.add(person);388
389 item1.addActionListener(this);390 item2.addActionListener(this);391 item3.addActionListener(this);392 item4.addActionListener(this);393 xin.addActionListener(this);394 jifen.addActionListener(this);395 editphoto.addActionListener(this);396 person.addActionListener(this);397
398
399 setJMenuBar(menuBar);400 }401
402 public booleanissuccess()403 {404 for (int i = 0; i < 4; i++)405 {406 for (int j = 0; j < 4; j++)407 {408 if(photos[i][j]==2048)409 {410 return true;411 }412 }413 }414 return false;415 }416 //用于子菜单的事件监听
417 @Override418 public voidactionPerformed(ActionEvent e) {419 if(e.getSource() ==item1)420 {421 theme = "A-";422 paint_view();423 }else if(e.getSource() ==item2)424 {425 theme = "B-";426 paint_view();427 }else if (e.getSource() ==item3)428 {429 theme = "C-";430 paint_view();431 }else if(e.getSource()==item4)432 {433 theme = "D-";434 paint_view();435 }436 else if(e.getSource()==xin)437 {438 score = 0;439 fail = 0;440 for(int i=0; i<4; i++)441 {442 for(int j=0; j<4;j++)443 {444 photos[i][j] = 0;445 }446 }447 initPhoto();448 paint_view();449 }else if(e.getSource()==jifen)450 {451 String s;452 s = JOptionPane.showInputDialog(null,"请输入要修改的积分:\n");453 if(s==null||s.equals(""))454 {455 JOptionPane.showMessageDialog(null,"???","what are you doing?",JOptionPane.WARNING_MESSAGE);456 }else
457 {458 score =Long.parseLong(s);459 JOptionPane.showMessageDialog(null,"修改成功!");460 paint_view();461 }462 }else if(e.getSource()==editphoto)463 {464 String s;465 inti,j;466 Object[] num = {"1","2","3","4"};467 Object[] data = {"2","4","8","16","32","64","128","256","512","1024","2048"};468 i = JOptionPane.showOptionDialog(null,"请输入要修改的行数:","修改器",JOptionPane.YES_NO_OPTION,469 JOptionPane.QUESTION_MESSAGE,null,num,num[0]);470 j = JOptionPane.showOptionDialog(null,"请输入要修改的列数:","修改器",JOptionPane.YES_NO_OPTION,471 JOptionPane.QUESTION_MESSAGE,null,num,num[0]);472
473 s = (String) JOptionPane.showInputDialog(null,"请修改数据为","标题",JOptionPane.QUESTION_MESSAGE,474 null,data,data[0]);475
476 photos[i][j] =Integer.parseInt(s);477 paint_view();478
479 }480 else if(e.getSource()==person)481 {482 newMadePerson();483 }484 }485
486 @Override487 public voidmouseClicked(MouseEvent e) {488
489 try
490 {491 Runtime.getRuntime().exec(492 "cmd /c start http://www.baidu.com");493 } catch(IOException x)494 {495 x.printStackTrace();496 }497
498
499 }500
501 @Override502 public voidmousePressed(MouseEvent e) {503
504 }505
506 @Override507 public voidmouseReleased(MouseEvent e) {508
509 }510
511 @Override512 public voidmouseEntered(MouseEvent e) {513
514 }515
516 @Override517 public voidmouseExited(MouseEvent e) {518
519 }520 }