java连连看项目汇报_java连连看小项目 - osc_x4wlt5vu的个人空间 - OSCHINA - 中文开源技术交流社区...

/*

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

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

*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]);

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值