/*

*本人也是刚入门,希望各位多多指教

*该项目主要代码在于连线

*1.2个连线没有拐弯

*2.2个连线有一个拐弯

*3.2个连线有2个拐弯

*采用递归算法

*/

package llk;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class llk implements ActionListener {
JFrame mainf;
Container c1;
JPanel centerPanel, southPanel, northPanel;
JButton diamondsButton[][] = new JButton[11][10];// 游戏按钮数组
JButton exitButton, resetButton, newlyButton;// 退出,重列,重新开始按钮
JLabel fractionLable = new JLabel("0"); // 分数标签
JButton firstButton, secondButton;
int grid[][] = new int[13][12];
static boolean pressInformation = false;
int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0, validateLV; // 游戏按钮的位置坐标
int i, j, k, n;// 消除方法控制

public void init() {
mainf = new JFrame("连连看");
c1 = mainf.getContentPane();
c1.setLayout(new BorderLayout());
centerPanel = new JPanel();
southPanel = new JPanel();
northPanel = new JPanel();
c1.add(centerPanel, "Center");
c1.add(southPanel, "South");
c1.add(northPanel, "North");
centerPanel.setLayout(new GridLayout(11, 10));
for (int cols = 0; cols < 11; cols++) {
for (int rows = 0; rows < 10; rows++) {
diamondsButton[cols][rows] = new JButton(
String.valueOf(grid[cols + 1][rows + 1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
for (int i = 1; i <= 10; i++) {
ImageIcon icon = new ImageIcon("src/image/" + i + ".jpg");
if (grid[cols + 1][rows + 1] == i) {
diamondsButton[cols][rows].setIcon(icon);
}
}

}
}

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(String.valueOf(Integer.parseInt(fractionLable
.getText())));
northPanel.add(fractionLable);

mainf.setBounds(280, 100, 600, 600);
mainf.setVisible(true);
mainf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void randombulid() {
int randoms, col, row;
for (int t = 1; t <= 55; t++) {
randoms = (int) (Math.random() * 10 + 1);
for (int a = 1; a <= 2; a++) {
col = (int) (Math.random() * 11 + 1);
row = (int) (Math.random() * 10 + 1);
while (grid[col][row] != 0) {
col = (int) (Math.random() * 11 + 1);
row = (int) (Math.random() * 10 + 1);
}
grid[col][row] = randoms;
}
}
}

public void reload() {
int r = 0, cols, rows;
int save[] = new int[110];
for (int q = 1; q <= 11; q++) {
for (int w = 1; w <= 10; w++) {
if (grid[q][w] != 0) {
save[r] = grid[q][w];
r++;
}
}
}
r = r - 1;
for (int q = 1; q <= 11; q++) {
for (int w = 1; w <= 10; w++) {
grid[q][w] = 0;
}
}
while (r >= 0) {// 把没有消去的button重新放一次
cols = (int) (Math.random() * 11 + 1);
rows = (int) (Math.random() * 10 + 1);
while (grid[cols][rows] != 0) {
cols = (int) (Math.random() * 11 + 1);
rows = (int) (Math.random() * 10 + 1);
}
this.grid[cols][rows] = save[r];
r--;
}
mainf.setVisible(false);
pressInformation = false; // 这里一定要将按钮点击信息归为初始
init();
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 10; j++) {
if (grid[i + 1][j + 1] == 0)
diamondsButton[i][j].setVisible(false);
}
}
}

public void jilu(int placeX, int placeY, JButton bz) {
if (pressInformation == false) {
x = placeX;
y = placeY;
fristMsg = grid[x][y];
firstButton = bz;
pressInformation = true;
} else {
x0 = x;
y0 = y;
secondMsg = fristMsg;
secondButton = firstButton;
x = placeX;
y = placeY;
fristMsg = grid[x][y];
firstButton = bz;
if (fristMsg == secondMsg && secondButton != firstButton) {
if (xiao2(x, y, x0, y0) == true) {
remove();
}
if (xiao3(x, y, x0, y0) == true) {
remove();
}
if (xiao4(x, y, x0, y0) == true) {
remove();
}
}
pressInformation = false;
}
}

public void fraction() {
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText()) + 100));
}

public void remove() {
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
grid[x0][y0] = 0;
grid[x][y] = 0;

}

public boolean xiao2(int a, int b, int a0, int b0) {
boolean state = false;
// 0折,同一行,之间没有控件,则可以消去
if ((a > a0) && (b == b0)) {
if (a == a0 + 1) {
state = true;
} else {
for (int i = a - 1; i > a0; i--) {
if (grid[i][b] != 0) {
state = false;
break;
} else {
state = true;
}
}
}
}
if ((a < a0) && (b == b0)) {
if (a == a0 - 1) {
state = true;
} else {
for (int i = a + 1; i < a0; i++) {
if (grid[i][b] != 0) {
state = false;
break;
} else {
state = true;
}
}
}

}
// 0折,同一列,之间没有控件,则可以消去
if ((a == a0) && (b > b0)) {
if (b == b0 + 1) {
state = true;
} else {
for (int i = b - 1; i > b0; i--) {
if (grid[a][i] != 0) {
state = false;
break;
} else {
state = true;
}
}
}

}

if ((a == a0) && (b < b0)) {
if (b == b0 - 1) {
state = true;
} else {
for (int i = b + 1; i < b0; i++) {
if (grid[a][i] != 0) {
state = false;
break;
} else {
state = true;
}
}
}

}
return state;
}

public boolean xiao3(int a, int b, int a0, int b0) {
boolean state = false;
if (xiao2(a, b, a0, b) == true && xiao2(a0, b0, a0, b) == true
&& grid[a0][b] == 0) {
state = true;
}
if (xiao2(a, b, a, b0) == true && xiao2(a0, b0, a, b0) == true
&& grid[a][b0] == 0) {
state = true;
}

return state;

}

public boolean xiao4(int a, int b, int a0, int b0) {
boolean state = false;
// (a,b)正右边的点
for (int i = a + 1; i <= grid.length - 1; i++) {
if (grid[i][b] == 0) {
if (xiao3(i, b, a0, b0) == true) {
state = true;
}
}
}
// (a,b)正左边的点
for (int i = a - 1; i >= 0; i--) {
if (grid[i][b] == 0) {
if (xiao3(i, b, a0, b0) == true) {
state = true;
}
}
}
// (a,b)正上方的点
for (int i = b + 1; i <= grid[b].length - 1; i++) {
if (grid[a][i] == 0) {
if (xiao3(a, i, a0, b0) == true) {
state = true;
}
}
}
// (a,b)正下方的点
for (int i = b - 1; i >= 0; i--) {
if (grid[a][i] == 0) {
if (xiao3(a, i, a0, b0) == true) {
state = true;
}
}
}
return state;
}

public static void main(String[] args) {
llk l = new llk();
l.randombulid();
l.init();

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == newlyButton) {
int grid[][] = new int[13][12];
this.grid = grid; randombulid(); mainf.setVisible(false);
pressInformation = false;
init();
}
if (e.getSource() == exitButton)
System.exit(0);
if (e.getSource() == resetButton)
reload();
for (int cols = 0; cols < 11; cols++) {
for (int rows = 0; rows < 10; rows++) {
if (e.getSource() == diamondsButton[cols][rows])
jilu(cols + 1, rows + 1, diamondsButton[cols][rows]);
}
}
}

}

java连连看小项目的更多相关文章

  1. java初学小项目-酒店客房管理系统

    最近初次接触JAVA,感觉之前学的C语言很有用,跟着视频做了一个小项目-酒店客房管理系统 /* 酒店客房管理系统 */ import java.util.Scanner;//通过键盘来输入命令需要的引 ...

  2. 迷你图书管理系统 源代码 Java初级小项目

    今天博主再给大家分享一个小项目:MiNi图书管理系统.用的是Java语言开发的,代码不多,大概260行左右吧,系统是实现图书的新增图书.删除图书.借阅图书.归还图书.查看图书等简单的功能(后附源代码) ...

  3. Java数据库小项目02--管家婆项目

    目录 项目要求 开发环境搭建 工具类JDBCUtils 创建管家婆数据表 项目分层 MainApp层 MainView层 ZhangWuController层 ZhangWuService层 Zhan ...

  4. 吃货联盟订餐系统 源代码 Java初级小项目

    咳咳,今天博主给大家写一个小的项目:吃货联盟订餐系统.博主不是大神(互联网架构师的路上ing),也是小白一个,不过是刚入门的小白^_^.项目功能也很简单:只是模拟日常的订餐流程呦,所以有错误以及功能不 ...

  5. 小项目,吃货联盟,java初级小项目,源代码

    1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...

  6. 掷骰子游戏窗体实现--Java初级小项目

    掷骰子 **多线程&&观察者模式 题目要求:<掷骰子>窗体小游戏,在该游戏中,玩家初始拥有1000的金钱,每次输入押大还是押小,以及下注金额,随机3个骰子的点数,如果3个骰 ...

  7. 嗖嗖移动大厅 源代码 Java初级小项目

    今天给大家一个比较综合的项目:嗖嗖移动业务大厅.项目功能很多,概括的功能也很全面.吃透了这个项目,你的java基础部分已经非常棒了!!! 一 . 项目概述 技能要求  使用面向对象设计的思想  合 ...

  8. Java数据库小项目01--实现用户登录注册

    先实现数据库和数据表,检测正常后再做其他的 CREATE TABLE users( username ) NOT NULL, PASSWORD ) NOT NULL); INSERT INTO use ...

  9. Java数据库小项目00---基础知识

    目录 JDBC的简单使用 向JDBC注入攻击 防止注入攻击 自建JDBC工具类 自建工具类优化--使用配置文件 使用数据库连接池优化工具类 JDBC的简单使用 package Test; import ...

随机推荐

  1. 转-C++之string判断字符串是否包含某个子串

    转自:https://blog.csdn.net/zhouxinxin0202/article/details/77862615/ 1.string类函数find C++的string类提供了字符串中 ...

  2. 安卓真机或者模拟器运行安装应用时提示 Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113]解决办法

    有时候为了方便调试APP,会在电脑上开启模拟器来调试我们的代码,有时候会出现 Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract n ...

  3. dubbo超时原理以及解决方案

    dubbo超时原理以及解决方案 本篇主要记录dubbo中关于超时的常见问题,实现原理,解决的问题 超时问题 为了检查对dubbo超时的理解,尝试回答如下几个问题,如果回答不上来或者不确定那么说明此处需 ...

  4. 一张图搞清楚Java异常机制

    下面是Java异常类的组织结构,红色区域的异常类表示是程序需要显示捕捉或者抛出的. Throwable Throwable是Java异常的顶级类,所有的异常都继承于这个类. Error,Excepti ...

  5. Spring Boot实现通用的接口参数校验

    Spring Boot实现通用的接口参数校验 Harries Blog™ 2018-05-10 2418 阅读 http ACE Spring App API https AOP apache IDE ...

  6. iView + vue-quill-editor 实现一个富文本编辑器(包含图片,视频上传)

    1. 引入插件(注意IE10以下不支持) npm install vue-quill-editor --savenpm install quill --save (Vue-Quill-Editor需要 ...

  7. XStream的简单使用

    XStream XStream是一个java对象和xml相互转换的工具 创建XStream对象:XStream stream = new XStream() Java对象转换成xml:stream . ...

  8. 解决HDFS小文件带来的计算问题

    hive优化 一.小文件简述 1.1. HDFS上什么是小文件? HDFS存储文件时的最小单元叫做Block,Hadoop1.x时期Block大小为64MB,Hadoop2.x时期Block大小为12 ...

  9. MySQL中orderby和limit分页数据重复的问题

    背景 读取规则是按照某表中sequence字段排序的,而这个字段是让人手工填写的.那么,可想而知,数据一多,难免会出现填写的值相同的情况. 综上所述,可能就会导致以下两条sql出现数据重叠的情况: s ...

  10. PHP操作XML方法之 XML Expat Parser

    XML Expat Parser 简介 此PHP扩展实现了使用PHP支持JamesClark编写的expat.此工具包可解析(但不能验证)XML文档.它支持PHP所提供的3种字符编码:US-ASCII ...