java连连看游戏

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java连连看游戏

人ͯ⃝间ͯ⃝四ͯ⃝月ͯ⃝ه٥   2022-06-03 我要评论

本文为大家分享了JAVA语言课程设计:连连看小游戏,供大家参考,具体内容如下

1.设计内容

界面中有5*10的界面,图中共有6种不同的图片,每两个相同的图片连接在一起,如果连线中转折的次数<=3次,两张图片可同时削掉,否则不能削去。

2.设计要求

色彩鲜艳,鼠标点击键好用,以固定时间将所有图片消掉为胜利,若时间到了,图片还有,则闯关失败。

3.设计思想

1)    搭建界面,首先搭建简单界面,可以先用按钮代替图片,并且行列可以先少做一些,如下图所示:
2)    每次用户选择两个图形,如果图形满足一定条件(两个图形一样,且这两个图形之间存在转弯少于3的路径),则两个图形都能消掉。给定任意具有相同图形的两个格子,我们需要寻找这两个格子之间在转弯最少的情况下,经过格子数目最少的路径。如果这个最优路径的转弯数目少于3 ,则这两个格子可以消去。
3)    定义消掉的方法,此方法可以实现,当执行消掉功能时,可以消除两个按钮。
4)    在检验两个方块能否消掉的时候,我们要让两个方块同时满足两个条件才行,就是两者配对并且连线成功。

分3种情况:(从下面的这三种情况,我们可以知道,需要三个检测,这三个检测分别检测一条直路经。这样就会有三条路经。若这三条路经上都是空按钮,那么就刚好是三种直线(两个转弯点)把两个按钮连接起来了)

* 1.相邻  
* 2. 若不相邻的先在第一个按钮的同行找一个空按钮。1).找到后看第二个按钮横向到这个空按钮所在的列是否有按钮。2).没有的话再看第一个按钮到与它同行的那个空按钮之间是否有按钮。3).没有的话,再从与第一个按钮同行的那个空按钮竖向到与第二个按钮的同行看是否有按钮。没有的话路经就通了,可以消了. 
* 3.若2失败后,再在第一个按钮的同列找一个空按钮。1).找到后看第二个按钮竖向到这个空按钮所在的行是否有按钮 2).没有的话,再看第一个按钮到与它同列的那个空按钮之间是否有按钮。3).没有的话,再从与第一个按钮同列的那个空按钮横向到与第二个按钮同列看是否有按钮。没有的话路经就通了,可以消了。

* 若以上三步都失败,说明这两个按钮不可以消去。

5)    在死锁的情况下,我们也可以暂时不终止游戏,而是随机打乱局面(即点击“重列”按钮),打破“死锁”局面。
6)    设计时间限制,即指定时间内没有消除全部按钮,则游戏结束
7)    设计过关模式,可以根据所有按钮都消除了,或是得分到达某一个阈值作为过关的依据。
8)    每一关的指定时间应该逐渐减少
9)    尝试将按钮换成图片,可以利用数组定义图片名称,然后把随机选择按钮变成随机选择数组下标,继而实现选择图片的功能

代码如下:

package test1;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class LianLianKan implements ActionListener {
    JFrame mainFrame; // 主面板
    Container thisContainer;
    JPanel centerPanel, southPanel, northPanel; // 子面板
    JButton diamondsButton[][] = new JButton[6][5];// 游戏按钮数组
    JButton exitButton, resetButton, newlyButton; // 退出,重列,重新开始按钮
    JLabel fractionLable = new JLabel("0"); // 分数标签
    JButton firstButton, secondButton; // 分别记录两次被选中的按钮
    int grid[][] = new int[8][7];// 储存游戏按钮位置
    int score=0;
    static boolean pressInformation = false; // 判断是否有按钮被选中
    int x0 = 0, y0 = 0, x = 0, y = 0, firstMsg = 0, secondMsg = 0; // 游戏按钮的位置坐标
    int i, j, k, n;// 消除方法控制
    public void init() {
        mainFrame = new JFrame("JKJ连连看");
        thisContainer = mainFrame.getContentPane();
        thisContainer.setLayout(new BorderLayout());
        centerPanel = new JPanel();
        southPanel = new JPanel();
        northPanel = new JPanel();
        thisContainer.add(centerPanel, "Center");
        thisContainer.add(southPanel, "South");
        thisContainer.add(northPanel, "North");
        centerPanel.setLayout(new GridLayout(6, 5));
        for (int cols = 0; cols < 6; cols++) {
            for (int rows = 0; rows < 5; rows++) {
                diamondsButton[cols][rows] = new JButton(String.valueOf(grid[cols + 1][rows + 1]));
                diamondsButton[cols][rows].addActionListener(this);
                centerPanel.add(diamondsButton[cols][rows]);
            }
        }
        exitButton = new JButton("退出");
        exitButton.addActionListener(this);
        resetButton = new JButton("重列");
        resetButton.addActionListener(this);
        newlyButton = new JButton("再来一局");
        newlyButton.addActionListener(this);
        southPanel.add(exitButton);
        southPanel.add(resetButton);
        southPanel.add(newlyButton);
        fractionLable.setText("分数:"+score);
        northPanel.add(fractionLable);
        int width=Toolkit.getDefaultToolkit().getScreenSize().width;
        int height=Toolkit.getDefaultToolkit().getScreenSize().height;
        mainFrame.setBounds((width-500)/2,(height-500)/2, 500, 500);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
    }
    public void randomBuild() {
        int randoms, cols, rows;
        for (int twins = 1; twins <= 15; twins++) {  //生成15个随机数
            randoms = (int) (Math.random() * 15 + 1);
            for (int alike = 1; alike <= 2; alike++) {   //每次将这个数放在两个位置
                cols = (int) (Math.random() * 6 + 1);
                rows = (int) (Math.random() * 5 + 1);  //随机生成数组坐标[cols,rows]
                while (grid[cols][rows] != 0) {         //如果这个位置已经赋值了,重新生成。
                    cols = (int) (Math.random() * 6 + 1);
                    rows = (int) (Math.random() * 5 + 1);
                }
                this.grid[cols][rows] = randoms;    //将这个随机数赋到数组中。
            }
        }
    }
    //显示分数
    public void fraction() {
        score+=100;
        fractionLable.setText(score+"");
    }
    //重列方法的实现
    public void reload() {
        int save[] = new int[50];
        int n = 0, cols, rows;
        int grid[][] = new int[8][7];
        for (int i = 0; i <= 6; i++) {
            for (int j = 0; j <= 5; j++) {
                if (this.grid[i][j] != 0) {
                    save[n] = this.grid[i][j];
                    n++;
                }
            }
        }
        n--;
        this.grid = grid;
        while (n >= 0) {
            cols = (int) (Math.random() * 6 + 1);
            rows = (int) (Math.random() * 5 + 1);
            while (grid[cols][rows] != 0) {  //如果已经赋值了,重新生成
                cols = (int) (Math.random() * 6 + 1);
                rows = (int) (Math.random() * 5 + 1);
            }
            this.grid[cols][rows] = save[n];
            n--;
        }
        mainFrame.setVisible(false);
        pressInformation = false; // 这里一定要将按钮点击信息归为初始
        init();
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 5; j++) {
                if (grid[i + 1][j + 1] == 0)
                    diamondsButton[i][j].setVisible(false);
            }
        }
    }
    public void estimateEven(int placeX, int placeY, JButton bz) {
        if (pressInformation == false) {//如果以前没点击过
            x = placeX;
            y = placeY;      //记录这个按钮坐标[x,y]
            secondMsg = grid[x][y];
            secondButton = bz;//记录这个按钮的信息
            pressInformation = true;
        } else {           //如果以前点击过
            x0 = x;    
            y0 = y;
            firstMsg = secondMsg;
            firstButton = secondButton;    //将上一次的button按钮信息赋给first
            x = placeX;
            y = placeY;
            secondMsg = grid[x][y];        //将这次点击按钮的信息记录下来
            secondButton = bz;
            if (firstMsg == secondMsg && secondButton != firstButton) {
                xiao();
            }
        }
    }
    public void xiao() { // 相同的情况下能不能消去。仔细分析,不一条条注释
        if ((x0 == x && (y0 == y + 1 || y0 == y - 1))|| ((x0 == x + 1 || x0 == x - 1) && (y0 == y))) { // 判断是否相邻
            remove();
        } else {
            for (j = 0; j < 7; j++) {
                if (grid[x0][j] == 0) { // 判断第一个按钮同行哪个按钮为空
                    if (y > j) { // 如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
                        for (i = y - 1; i >= j; i--) { // 判断第二按钮左侧直到第一按钮中间有没有按钮
                            if (grid[x][i] != 0) {
                                k = 0;    
                                break;
                            } else {
                                k = 1;
                            } // K=1说明通过了第一次验证
                        }
                        if (k == 1) {
                            linePassOne();
                        }
                    }
                    if (y < j) { // 如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
                        for (i = y + 1; i <= j; i++) { // 判断第二按钮左侧直到第一按钮中间有没有按钮
                            if (grid[x][i] != 0) {
                                k = 0;
                                break;
                            } else {
                                k = 1;
                            }
                        }
                        if (k == 1) {
                            linePassOne();
                        }
                    }
                    if (y == j) {
                        linePassOne();
                    }
                }
                if (k == 2) {
                    if (x0 == x) {
                        remove();
                    }
                    if (x0 < x) {
                        for (n = x0; n <= x - 1; n++) {
                            if (grid[n][j] != 0) {
                                k = 0;
                            }
                            if (grid[n][j] == 0 && n == x - 1) {
                                remove();
                            }
                        }
                    }
                    if (x0 > x) {
                        for (n = x0; n >= x + 1; n--) {
                            if (grid[n][j] != 0) {
                                k = 0;
                                break;
                            }
                            if (grid[n][j] == 0 && n == x + 1) {
                                remove();
                            }
                        }
                    }
                }
            }
            for (i = 0; i < 8; i++) { // 列
                if (grid[i][y0] == 0) {
                    if (x > i) {
                        for (j = x - 1; j >= i; j--) {
                            if (grid[j][y] != 0) {
                                k = 0;
                                break;
                            } else {
                                k = 1;
                            }
                        }
                        if (k == 1) {
                            rowPassOne();
                        }
                    }
                    if (x < i) {
                        for (j = x + 1; j <= i; j++) {
                            if (grid[j][y] != 0) {
                                k = 0;
                                break;
                            } else {
                                k = 1;
                            }
                        }
                        if (k == 1) {
                            rowPassOne();
                        }
                    }
                    if (x == i) {
                        rowPassOne();
                    }
                }
                if (k == 2) {
                    if (y0 == y) {
                        remove();
                    }
                    if (y0 < y) {
                        for (n = y0; n <= y - 1; n++) {
                            if (grid[i][n] != 0) {
                                k = 0;
                                break;
                            }
                            if (grid[i][n] == 0 && n == y - 1) {
                                remove();
                            }
                        }
                    }
                    if (y0 > y) {
                        for (n = y0; n >= y + 1; n--) {
                            if (grid[i][n] != 0) {
                                k = 0;
                                break;
                            }
                            if (grid[i][n] == 0 && n == y + 1) {
                                remove();
                            }
                        }
                    }
                }
            }
        }
    }
    public void linePassOne() {
        if (y0 > j) { // 第一按钮同行空按钮在左边
            for (i = y0 - 1; i >= j; i--) { // 判断第一按钮同左侧空按钮之间有没按钮    
                if (grid[x0][i] != 0) {
                    k = 0;
                    break;
                } else {
                    k= 2;
                } // K=2说明通过了第二次验证
            }
        }
        if (y0 < j) { // 第一按钮同行空按钮在与第二按钮之间
            for (i = y0 + 1; i <= j; i++) {
                if (grid[x0][i] != 0) {
                    k = 0;
                    break;
                } else {
                    k = 2;
                }
            }
        }
    }
    public void rowPassOne() {
        if (x0 > i) {
            for (j = x0 - 1; j >= i; j--) {
                if (grid[j][y0] != 0) {
                    k = 0;
                    break;
                } else {
                    k = 2;
                }
            }
        }
        if (x0 < i) {
            for (j = x0 + 1; j <= i; j++) {
                if (grid[j][y0] != 0) {
                    k = 0;
                    break;
                } else {
                    k = 2;
                }
            }
        }
    }
    //消去按钮算法
    public void remove() {
        firstButton.setVisible(false);
        secondButton.setVisible(false);   //两个按钮不可见
        fraction();        //改变分数
        pressInformation = false;         //点击按钮键清掉
        k = 0;                            //可消除标志清空
        grid[x0][y0] = 0;
        grid[x][y] = 0;                   //按钮键值清零
    }
    //事件响应
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == newlyButton) {
            int grid[][] = new int[8][7];
            this.grid = grid;
            randomBuild();
            score=0;
            mainFrame.setVisible(false);
            pressInformation = false;
            init();
        }
        if (e.getSource() == exitButton)
            System.exit(0);
        if (e.getSource() == resetButton)
            reload();
        for (int cols = 0; cols < 6; cols++) {
            for (int rows = 0; rows < 5; rows++) {
                if (e.getSource() == diamondsButton[cols][rows])
                    estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);
            }
        }
    }
    public static void main(String[] args) {
        LianLianKan llk = new LianLianKan();
        llk.randomBuild();//随机分配键值
        llk.init();//初始化
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们