/*

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

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

*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. 建站手册-浏览器信息:Netscape 浏览器

    ylbtech-建站手册-浏览器信息:Netscape 浏览器 1.返回顶部 1. http://www.w3school.com.cn/browsers/browsers_netscape.asp ...

  2. HTML5: HTML5 WebSocket

    ylbtech-HTML5: HTML5 WebSocket 1.返回顶部 1. HTML5 WebSocket WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议 ...

  3. 9、继续matlab数值分析

    1.matlab拉格朗日插值 function yi=Lagrange(x,y,xi) %x为向量,全部的插值节点 %y为向量,插值节点处的函数值 %xi为标量或向量,被估计函数的自变量: %yi为x ...

  4. Oracle 用户概念与基本操作

    目录 目录 Oracle的用户 通过系统用户来登陆SQLPlus system和sys的区别 查看登陆的用户 启用和锁定一个用户 启用用户 锁定用户 创建用户 修改用户 删除用户 角色权限 常用的用户 ...

  5. PHP面试 MySQL查询优化

    MySQL查询优化 面试题一 请简述项目中优化SQL语句执行效率的方法,从那些方面,SQL语句性能如何分析? 优化查询过程中的数据访问.优化长难的查询语句.优化特定类型的查询语句 分析SQL语句方法 ...

  6. Mysql create constraint foreign key faild.trouble shooting method share

    mysql> create table tb_test (id int(10) not null auto_increment primary key,action_id int(10) not ...

  7. java多线程学习笔记(五)

    补充一个synchronized关键字的结论: 线程A先持有object对象的Lock锁,B线程可以以异步的方式调用object对象中的非synchronized类型的方法 A线程现持有object对 ...

  8. Cocos2d-x 发布 Android

    Cocos2d-x 发布 Android 前置需求: Android NDK Android SDK OR Eclipse ADT Bundle Android AVD target installe ...

  9. 大型项目必备IPC之Binder机制原理(一)

    阿里P7Android高级架构进阶视频免费学习请点击:https://space.bilibili.com/474380680 摘要 Binder是Android系统进程间通信(IPC)方式之一.Li ...

  10. PHP正则表达式中的反斜线

    PHP反斜线再正则表达式中的使用 <?php $str = 'hello\world'; $pattern = '/hello\\\\world/'; preg_match($pattern,$ ...