• JAVA版2048


    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;
        }
    }
    

    效果图:

  • 相关阅读:
    To the Virgins, to Make Much of Time
    瓦尔登湖
    贪心算法
    R语言实战 —— 常见问题解决方法
    R语言实战(四)—— 基本数据管理
    R语言实战(三)——模拟随机游走数据
    Vim——回顾整理
    Clion下载安装使用教程(Win+MinGW)
    【ACM】孪生素数问题
    【ACM】一种排序
  • 原文地址:https://www.cnblogs.com/LeafLove/p/13582619.html
Copyright © 2020-2023  润新知