哈哈,讓你別點了(2048原始碼在這)
阿新 • • 發佈:2021-01-20
package Game; public class Main { public static void main(String[] args) { new Welcome(); } }
1 package Game; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.MouseEvent; 6 import java.awt.event.MouseListener; 7 import java.net.URL; 8 importWelcomejava.util.Random; 9 10 public class Welcome extends JFrame { 11 12 public Welcome() 13 { 14 hello(); 15 } 16 17 public void hello() 18 { 19 try { 20 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 21 } catch(ClassNotFoundException | InstantiationException | IllegalAccessException 22 | UnsupportedLookAndFeelException e) { 23 e.printStackTrace(); 24 } 25 setSize(500,500); 26 setLocationRelativeTo(null); 27 //setAlwaysOnTop(true); 28 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);29 setTitle("歡迎使用"); 30 setLayout(null); 31 32 URL img = Welcome.class.getResource("image/hello.png"); 33 JLabel helloimag = new JLabel(new ImageIcon(img)); 34 helloimag.setBounds(0,0,500,500); 35 36 JLabel hellotext = new JLabel("歡迎使用"); 37 hellotext.setBounds(150,50,200,200); 38 hellotext.setFont(new Font("行書",Font.BOLD,40)); 39 hellotext.setForeground(Color.green); 40 41 JButton button1 = new JButton(); 42 button1.setText("正常進入"); 43 button1.setFont(new Font("Dialog", Font.BOLD,30)); 44 button1.setForeground(Color.red); 45 button1.setBounds(300,300,200,100); 46 47 JButton button2 = new JButton(); 48 button2.setText("簡單進入"); 49 button2.setFont(new Font("Dialog", Font.BOLD,10)); 50 button2.setForeground(Color.black); 51 button2.setBounds(0,300,200,100); 52 53 getContentPane().add(button1); 54 getContentPane().add(button2); 55 getContentPane().add(hellotext); 56 getContentPane().add(helloimag); 57 58 button1.addMouseListener(new Mouselisten1()); 59 button2.addMouseListener(new Mouselisten2()); 60 61 setVisible(true); 62 } 63 64 class Mouselisten1 implements MouseListener 65 { 66 @Override 67 public void mouseClicked(MouseEvent e) { 68 Random r = new Random(); 69 double num1 = Math.random()*(100 +1); 70 double num2 = Math.random()*(100 +1); 71 double result = num1 + num2; 72 String input; 73 input = JOptionPane.showInputDialog(null,"請輸入正確答案:\n"+num1+"+"+num2+"=?\n","人機驗證",JOptionPane.PLAIN_MESSAGE); 74 if(input==null||input.equals("")) 75 { 76 JOptionPane.showMessageDialog(null,"are you kidding me?","驗證失敗!",JOptionPane.ERROR_MESSAGE); 77 }else 78 { 79 double in_result = Double.parseDouble(input); 80 if(Math.abs(result-in_result)<1e-6) 81 { 82 JOptionPane.showMessageDialog(null,"Bingo!","驗證成功",JOptionPane.PLAIN_MESSAGE); 83 new Myframe(); 84 setVisible(false); 85 }else 86 { 87 JOptionPane.showMessageDialog(null,"are you kidding me?","驗證失敗!",JOptionPane.ERROR_MESSAGE); 88 } 89 } 90 91 } 92 93 @Override 94 public void mousePressed(MouseEvent e) { 95 96 } 97 98 @Override 99 public void mouseReleased(MouseEvent e) { 100 101 } 102 103 @Override 104 public void mouseEntered(MouseEvent e) { 105 106 } 107 108 @Override 109 public void mouseExited(MouseEvent e) { 110 111 } 112 } 113 114 class Mouselisten2 implements MouseListener 115 { 116 117 @Override 118 public void mouseClicked(MouseEvent e) { 119 Random r = new Random(); 120 int num1 = r.nextInt(101); 121 int num2 = r.nextInt(101); 122 int result = num1 + num2; 123 String input; 124 input = JOptionPane.showInputDialog(null,"請輸入正確答案:\n"+num1+"+"+num2+"=?\n","人機驗證",JOptionPane.PLAIN_MESSAGE); 125 if(input==null||input.equals("")) 126 { 127 JOptionPane.showMessageDialog(null,"are you kidding me?","驗證失敗!",JOptionPane.ERROR_MESSAGE); 128 }else 129 { 130 int in_result = Integer.parseInt(input); 131 if(result==in_result) 132 { 133 JOptionPane.showMessageDialog(null,"Bingo!","驗證成功",JOptionPane.PLAIN_MESSAGE); 134 new Myframe(); 135 setVisible(false); 136 }else 137 { 138 JOptionPane.showMessageDialog(null,"are you kidding me?","驗證失敗!",JOptionPane.ERROR_MESSAGE); 139 } 140 } 141 142 143 } 144 145 @Override 146 public void mousePressed(MouseEvent e) { 147 148 } 149 150 @Override 151 public void mouseReleased(MouseEvent e) { 152 153 } 154 155 @Override 156 public void mouseEntered(MouseEvent e) { 157 158 } 159 160 @Override 161 public void mouseExited(MouseEvent e) { 162 163 } 164 } 165 }
1 package Game; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.io.IOException; 7 import java.net.URL; 8 import java.util.Random; 9 10 @SuppressWarnings("BooleanMethodIsAlwaysInverted") 11 public class Myframe extends JFrame implements KeyListener ,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 public Myframe() 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 void start_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 void paint_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(new ImageIcon(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(new ImageIcon(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(new ImageIcon(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(new ImageIcon(imgURL)); 107 back.setBounds(0,0,1080,1920); 108 getContentPane().add(back); 109 110 getContentPane().repaint(); 111 } 112 113 @Override 114 public void keyTyped(KeyEvent e) { 115 116 } 117 118 //事件監聽器 119 @Override 120 public void keyPressed(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 new MadePerson(); 154 } 155 paint_view(); 156 157 } 158 159 160 @Override 161 public void keyReleased(KeyEvent e) { 162 163 } 164 165 public void leftmove() 166 { 167 for(int x=0; x<4; x++) 168 { 169 int[] temp = new int[4]; 170 int k = 0; 171 //後置0元素 172 for(int i: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 void rightmove() 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 void upmove() 210 { 211 anticlockwise(); 212 leftmove(); 213 clockwise(); 214 } 215 216 public void downmove() 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<end; start++,end--) 226 { 227 int temp = a[start]; 228 a[start] = a[end]; 229 a[end] = temp; 230 } 231 } 232 233 public void clockwise() 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 void anticlockwise() 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 boolean isFailure() 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<line; i++) 305 { 306 System.arraycopy(src[i],0,dest[i],0,column); 307 } 308 } 309 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<line; i++) 315 { 316 for(int j=0; j<column; j++) 317 { 318 if(a[i][j] !=b [i][j]) 319 { 320 return false; 321 } 322 } 323 } 324 return true; 325 } 326 327 public void randomnum() 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 = new Random(); 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 void initPhoto() 361 { 362 randomnum(); 363 randomnum(); 364 } 365 366 public void initMenu() 367 { 368 JMenuBar menuBar = new JMenuBar(); 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 boolean issuccess() 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 @Override 418 public void actionPerformed(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 int i,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 new MadePerson(); 483 } 484 } 485 486 @Override 487 public void mouseClicked(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 @Override 502 public void mousePressed(MouseEvent e) { 503 504 } 505 506 @Override 507 public void mouseReleased(MouseEvent e) { 508 509 } 510 511 @Override 512 public void mouseEntered(MouseEvent e) { 513 514 } 515 516 @Override 517 public void mouseExited(MouseEvent e) { 518 519 } 520 }Myframe
package Game; import javax.swing.*; import java.awt.*; public class MadePerson extends JFrame { MadePerson() { person(); } public void person() { //setSize(1920,1080); setExtendedState(JFrame.MAXIMIZED_BOTH); setAlwaysOnTop(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setTitle("製作人員"); setLayout(null); //JLabel w = new JLabel("Mr.Wang\nLanguage: Java\nVersion : unknown"); JLabel w = new JLabel("<html><body><br />感謝使用!<br />Mr.Wang<br /><br />" + "Langguage:Java<br /><br />" + "Version:unknown</body></html>"); w.setBounds(500,100,500,500); w.setFont(new Font("行楷", Font.BOLD,50)); getContentPane().add(w); setVisible(true); } }