JAVA版2048
阿新 • • 發佈:2020-08-29
Java版2048
用來練習java的小專案
"Talk is cheap, Show me the code."
import javax.swing.*; import java.awt.event.*; import java.security.Key; import java.util.*; import java.awt.*; import javax.swing.*; public class game2048 { private static void createAndShowGUI() { //建立窗體 JFrame f = new JFrame("2048"); JPanel jp = new JPanel(); jp.setLayout(new GridLayout(4,4,5,5)); f.add(jp); f.setSize(600,600); f.setLocation(400,300); MainGame mainGame = new MainGame(); mainGame.iniGame(); //新增鍵盤監聽事件 jp.setFocusable(true); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ JButton but; if(mainGame.mp[i][j] != 0){ but = new JButton(""+mainGame.mp[i][j]); } else{ but = new JButton(""); } Font wordFont = new Font("宋體",Font.BOLD,25); but.setFont(wordFont); jp.add(but); } } jp.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e){ int keyCode = e.getKeyCode(); //System.out.println(keyCode); switch (keyCode){ case KeyEvent.VK_UP: mainGame.upUnion();break; case KeyEvent.VK_DOWN: mainGame.downUnion();break; case KeyEvent.VK_LEFT: mainGame.leftUnion();break; case KeyEvent.VK_RIGHT: mainGame.rightUnion();break; default:break; } if(mainGame.move) { if (mainGame.nextOp()) { jp.removeAll(); jp.repaint(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { JButton but; if (mainGame.mp[i][j] != 0) { but = new JButton("" + mainGame.mp[i][j]); } else { but = new JButton(""); } Font wordFont = new Font("宋體", Font.BOLD, 25); but.setFont(wordFont); jp.add(but); } } jp.updateUI(); } else { jp.removeAll(); jp.repaint(); JButton but = new JButton("遊戲結束!最終得分:" + mainGame.finalScore); Font wordFont = new Font("宋體", Font.BOLD, 25); but.setFont(wordFont); but.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); jp.add(but); jp.updateUI(); } } } }); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(game2048::createAndShowGUI); } } class MainGame { int[][] mp = new int[5][5]; int x,y; int finalScore; boolean move = false; public MainGame() { for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ mp[i][j] = 0; } } x = 0; y = 0; } public boolean getNext() { boolean flag=false; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(mp[i][j] == 0){ flag = true; break; } } if(flag) break; } if(!flag) return false; Random rd = new Random(); int a, b; while (true) { a = rd.nextInt(4); b = rd.nextInt(4); if (this.mp[a][b] == 0) { x = a; y = b; return true; } } } public int getVal() { Random rd = new Random(); if(rd.nextInt(4) <= 2) { return 2; } else { return 4; } } public boolean iniGame() { if(getNext()){ mp[x][y] = 2; } else{ return false; } if(getNext()){ mp[x][y] = 2; } else{ return false; } return true; } public int downUnion() { int score = 0; //向下移動 for(int i=0;i<4;i++){ for(int j=2;j>=0;j--){ if(mp[j][i] != 0) { int l = j; while (l + 1 < 4 && mp[l + 1][i] == 0){ l++; mp[l][i] = mp[l-1][i]; mp[l-1][i] = 0; move = true; } } } } //相鄰同樣元素合併 for(int i=0;i<4;i++){ for(int j=2;j>=0;j--){ if(mp[j+1][i] == mp[j][i] && mp[j+1][i] != 0){ score += mp[j][i]; mp[j+1][i] += mp[j][i]; mp[j][i] = 0; move = true; } } } //向下移動 for(int i=0;i<4;i++){ for(int j=2;j>=0;j--){ if(mp[j][i] != 0) { int l = j; while (l + 1 < 4 && mp[l + 1][i] == 0){ l++; mp[l][i] = mp[l-1][i]; mp[l-1][i] = 0; move = true; } } } } finalScore += score; return score; } public int upUnion() { int score = 0; //移動 for(int i=0;i<4;i++){ for(int j=1;j<4;j++){ if(mp[j][i] != 0){ int l = j; while(l-1>=0&&mp[l-1][i]==0){ l--; mp[l][i] = mp[l+1][i]; mp[l+1][i] = 0; move = true; } } } } //合併 for(int i=0;i<4;i++){ for(int j=1;j<4;j++){ if(mp[j][i] == mp[j-1][i] && mp[j][i] != 0){ score += mp[j][i]; mp[j-1][i] += mp[j][i]; mp[j][i] = 0; move = true; } } } //移動 for(int i=0;i<4;i++){ for(int j=1;j<4;j++){ if(mp[j][i] != 0){ int l = j; while(l-1>=0&&mp[l-1][i]==0){ l--; mp[l][i] = mp[l+1][i]; mp[l+1][i] = 0; move = true; } } } } finalScore += score; return score; } public int leftUnion() { int score = 0; //移動 for(int i=0;i<4;i++){ for(int j=1;j<4;j++){ if(mp[i][j] != 0){ int l = j; while(l-1>=0&&mp[i][l-1]==0){ l--; mp[i][l] = mp[i][l+1]; mp[i][l+1] = 0; move = true; } } } } //合併 for(int i=0;i<4;i++){ for(int j=1;j<4;j++){ if(mp[i][j] == mp[i][j-1] && mp[i][j] != 0){ score += mp[i][j]; mp[i][j-1] += mp[i][j]; mp[i][j] = 0; move = true; } } } //移動 for(int i=0;i<4;i++){ for(int j=1;j<4;j++){ if(mp[i][j] != 0){ int l = j; while(l-1>=0&&mp[i][l-1]==0){ l--; mp[i][l] = mp[i][l+1]; mp[i][l+1] = 0; move = true; } } } } finalScore += score; return score; } public int rightUnion() { int score = 0; for(int i=0;i<4;i++){ for(int j=2;j>=0;j--){ if(mp[i][j] != 0){ int l = j; while(l+1<4&&mp[i][l+1]==0){ l++; mp[i][l] = mp[i][l-1]; mp[i][l-1]=0; move = true; } } } } //合併 for(int i=0;i<4;i++){ for(int j=2;j>=0;j--){ if(mp[i][j] == mp[i][j+1] && mp[i][j] != 0){ score += mp[i][j]; mp[i][j+1] += mp[i][j]; mp[i][j] = 0; move = true; } } } //移動 for(int i=0;i<4;i++){ for(int j=2;j>=0;j--){ if(mp[i][j] != 0){ int l = j; while(l+1<4&&mp[i][l+1]==0){ l++; mp[i][l] = mp[i][l-1]; mp[i][l-1]=0; move = true; } } } } finalScore += score; return score; } public boolean nextOp() { if(!getNext()) return false; mp[x][y] = getVal(); move = false; return true; } }
效果圖: