java实现连连看游戏课程设计
作者:人??间??四??月???? 发布时间:2023-10-30 13:18:37
本文为大家分享了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();//初始化
}
}
来源:https://blog.csdn.net/weixin_61149547/article/details/122789361


猜你喜欢
- Springboot启动不检查JPA的数据源配置1.问题有时我们使用spring boot ,在依赖中配置了spring data jpa的
- 这里设计一个自定义View,继承了ScrollView,实现可以下拉里面的内容,松手后画面弹回,这个自定义的View可以当做ScrollVi
- 代理模式:为其他对象提供一种代理以控制某个对象的访问。用在:在某些情况下,一个客户不想或者不能直接访问另一个对象,而代理对象可以在客户端和目
- 最近因为公司项目需要用到WinForm的DecExpress控件,在这里把一些使用方法总结一下。 DevExpress中有一个专门
- 汽车前后轮倒车轨迹计算附C#源码(Unity),供大家参考,具体内容如下原理很简单, 都是高中的几何数学部分需要的参数有:车前后轴距;车宽(
- -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 这里有几个问
- 详解Android使用@hide的API的方法今天早上想修改MediaPlaybackService.Java(/packages/apps
- Springboot对配置文件的敏感信息加密前言最近公司对软件的安全问题比较在意,要求对配置文件中的敏感信息如数据库密码等进行加密。但是Sp
- 常需要对list进行排序,小到List<String>,大到对自定义的类进行排序。不需要自行归并或堆排序。简单实现一个接口即可。
- 本文实例为大家分享了C#强制转换和尝试转换的方法,供大家参考,具体内容如下将String[]类型的Object类型,转换为String[]类
- string filePath = @"E:\Randy0528\中文目录\JustTest.rar"; &n
- String类基本概念String类属于引用数据类型,不属于基本数据类型。在Java中只要是" "(双引号)中的,都是S
- 这几天琢磨写一个Android的Runtime用来加速HTML5 Canvas,让GameBuilder+CanTK 不但开发速度快,运行速
- 前言简单来说机器学习的核心步骤在于“获取学习数据;选择机器算法;定型模型;评估模型,预测模型结果”,下面本人就以判断日报内容是否合格为例为大
- 在项目开发过程中,有时会遇到不同程序之间相互调用数据,数据中不免会包含时间,比如ASP.NET调用PHP,牵扯到时间就要做一下处理,PHP程
- C#定义多行字符串的方式在定义的前面加上@符号: string aa = @"asdfsdfsd &n
- cookie机制和session机制的区别具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持
- 1.什么是单例模式?所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对
- 支持趋势线的图表类型包括二维面积图、条形图、柱形图、柱形图、股价图、xy (散点图) 和气泡图中;不能向三维、堆积、雷达图、饼图、曲面图或圆
- 本文实例讲述了C#使用iTextSharp从PDF文档获取内容的方法。分享给大家供大家参考。具体实现方法如下:using System;us