java连连看小项目
/*
*本人也是刚入门,希望各位多多指教
*该项目主要代码在于连线
*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连连看小项目的更多相关文章
- java初学小项目-酒店客房管理系统
最近初次接触JAVA,感觉之前学的C语言很有用,跟着视频做了一个小项目-酒店客房管理系统 /* 酒店客房管理系统 */ import java.util.Scanner;//通过键盘来输入命令需要的引 ...
- 迷你图书管理系统 源代码 Java初级小项目
今天博主再给大家分享一个小项目:MiNi图书管理系统.用的是Java语言开发的,代码不多,大概260行左右吧,系统是实现图书的新增图书.删除图书.借阅图书.归还图书.查看图书等简单的功能(后附源代码) ...
- Java数据库小项目02--管家婆项目
目录 项目要求 开发环境搭建 工具类JDBCUtils 创建管家婆数据表 项目分层 MainApp层 MainView层 ZhangWuController层 ZhangWuService层 Zhan ...
- 吃货联盟订餐系统 源代码 Java初级小项目
咳咳,今天博主给大家写一个小的项目:吃货联盟订餐系统.博主不是大神(互联网架构师的路上ing),也是小白一个,不过是刚入门的小白^_^.项目功能也很简单:只是模拟日常的订餐流程呦,所以有错误以及功能不 ...
- 小项目,吃货联盟,java初级小项目,源代码
1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...
- 掷骰子游戏窗体实现--Java初级小项目
掷骰子 **多线程&&观察者模式 题目要求:<掷骰子>窗体小游戏,在该游戏中,玩家初始拥有1000的金钱,每次输入押大还是押小,以及下注金额,随机3个骰子的点数,如果3个骰 ...
- 嗖嗖移动大厅 源代码 Java初级小项目
今天给大家一个比较综合的项目:嗖嗖移动业务大厅.项目功能很多,概括的功能也很全面.吃透了这个项目,你的java基础部分已经非常棒了!!! 一 . 项目概述 技能要求 使用面向对象设计的思想 合 ...
- Java数据库小项目01--实现用户登录注册
先实现数据库和数据表,检测正常后再做其他的 CREATE TABLE users( username ) NOT NULL, PASSWORD ) NOT NULL); INSERT INTO use ...
- Java数据库小项目00---基础知识
目录 JDBC的简单使用 向JDBC注入攻击 防止注入攻击 自建JDBC工具类 自建工具类优化--使用配置文件 使用数据库连接池优化工具类 JDBC的简单使用 package Test; import ...
随机推荐
- 栈Stack --- 数组实现
栈最大的一个特点就是先进后出(FILO—First-In/Last-Out). /** * 栈:后进先出 * Created by fred on 2018/7/31. */ public class ...
- jquery与其他js冲突
var $j=JQuery.noConflict(); $j('#msg').hide();//此处$j就代表JQuery //重命名以下,把$改为$j
- freemark 语法
我们通过后端model. addAttribute() 传递到前端的值来进行界面渲染 它的循环语句 和其他的有点不同: if 循环 <#if 条件语句> </#if> if ...
- Angularjs实现简单的登陆框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- 64位系统sql链接oracle
在SQL Server 2008中连接Oracle,完成查询.插入操作 建立指向Oracle的连接 在32位的系统中sql链接oracle,在链接服务器里点击服务器对象,右键链接服务器,选择micro ...
- python学习笔记:接口开发——PythonWEB框架之Flask
Flask是一个使用 Python 编写的轻量级 Web 应用框架,安装命令如下 pip install flask 一.服务端接口是怎么开发的? 1.启动一个服务 2.接收到客户端传过来的数据3.登 ...
- 【webpack】webpack之postcss-loader的基本使用---【巷子】
一.postcss-loader简介 postcss-loader 用来对.css 文件进行处理,并添加在 style-loader 和 css-loader 之后.通过一个额外的 postcss 方 ...
- log4j日志格式化
Apache log4j 提供了各种布局对象,每一个对象都可以根据各种布局格式记录数据.另外,也可以创建一个布局对象格式化测井数据中的特定应用的方法. 所有的布局对象 - Appender对象收到 L ...
- 注解深入浅出之Retrofit中的注解(三)
更多andorid高级架构进阶视频免费分享学习请点击:https://space.bilibili.com/474380680 Retrofit中的注解 @Query,@QueryMap,@Field ...
- uvloop官网翻译
魔术堆栈 uvloop:快速的Python网络连接 作者Yury Selivanov @ 1st1 2016年5月3日 TL; DR asyncio是Python标准库附带的异步I / O框架.在此博 ...