连连看java实训代码_java连连看课程设计报告以及源程序
如何用JAVA 编写一个连连看游戏全程设计
刚试了..测试通过..
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclass LianLianKan implements ActionListener {
JFrame mainFrame; // 主面板
Container thisContainer;
JPanel centerPanel, southPanel, northPanel; //子面板
JButton exitButton, resetButton, newlyButton;// 退出,重列,重新开始按钮
JLabel fractionLable = newJLabel("0"); // 分数标签
JButton firstButton, secondButton; // 分别记录两次被选中的按钮
//了一层是0的按钮,这样就可以实现靠近面板边缘的两个按钮可以消去)
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() {
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");
diamondsButton[cols][rows] = newJButton(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(String.valueOf(Integer.parseInt(fractionLable
.getText())));
northPanel.add(fractionLable);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public void randomBuild() {
int randoms, cols, rows;
while (grid[cols][rows] != 0) {//等于0说明这个空格有了button
this.grid[cols][rows] = randoms;
public void fraction() {
.getText()) + 100));
public void reload() {
int n = 0, cols, rows;
if (this.grid[i][j] != 0) {
save[n] = this.grid[i][j];//记下每个button的数字
n++;//有几个没有消去的button
n = n - 1;
this.grid = grid;
while (n = 0) {//把没有消去的button重新放一次
while (grid[cols][rows] != 0) {
this.grid[cols][rows] = save[n];
n--;
mainFrame.setVisible(false);
init();
if (grid[i + 1][j + 1] == 0)
diamondsButton[i][j].setVisible(false);
public void estimateEven(int placeX, intplaceY, JButton bz) {
if (pressInformation == false) {
x = placeX;
y = placeY;
secondMsg = grid[x][y];
secondButton = bz;
pressInformation = true;
} else {
x0 = x;
y0 = y;
fristMsg = secondMsg;
firstButton = secondButton;
if (fristMsg == 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();
if (grid[x0][j] == 0) { // 判断和第一个按钮同行的哪个按钮为空
//如果找到一个为空的,就按列值的三种情况比较第二个按钮与空按钮的位置
if (y j) {//第二个按钮在空按钮右边
for (i = y - 1; i = j; i--) { //检测从第二个按钮横向左边到空格所在列为止是否全是空格
if (grid[x][i] != 0) {
k = 0;
k = 1;
} // K=1说明全是空格通过了第一次验证,也就是从第二个按钮横向左边到空格所在列为止全是空格
if (k == 1) {
linePassOne();//进入第二次验证,也就是从第一个按钮到它同行的空格之间的空格判断
if (y j) { // 第二个按钮在空按钮左边
for (i = y + 1; i = j; i++) {//检测从第二个按钮横向右边到空格所在列为止是否全是空格
break;
linePassOne();
if (y == j) {//第二个按钮和空按钮同列
//第三次检测,检测确定为空的第j列的那个按钮竖向到第二个按钮,看是不是有按钮
if (x0 == x) {//第一,二按钮在同行
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) {
if (x0 x) {//第一按钮在第二按钮上边
for (n = x0; n = x + 1; n--) {
if (grid[n][j] == 0 n == x +1) {
}//-------------------------------------for
//当上面的检测与第一个按钮同行的空格按钮失败后(不能找到与第二个按钮的相连路经),下面就执行
//检测与第一个按钮同列的空格按钮
if (grid[i][y0] == 0) {// 判断和第一个按钮同列的哪个按钮为空
if (x i) {//第二个按钮在这个空按钮的下面
for (j = x - 1; j = i; j--) {
if (grid[j][y] != 0) {
rowPassOne();
if (x i) {//第二个按钮在这个空按钮的上面
for (j = x + 1; j = i; j++) {
if (x == i) {//第二个按钮与这个空按钮同行
if (y0 == y) {//第二个按钮与第一个按钮同列
if (y0 y) {//第二个按钮在第一个按钮右边
for (n = y0; n = y - 1; n++) {
if (grid[i][n] != 0) {
if (grid[i][n] == 0 n == y -1) {
if (y0 y) {//第二个按钮在第一个按钮左边
for (n = y0; n = y + 1; n--) {
if (grid[i][n] == 0 n == y +1) {
}//--------------------------------for
}//-------------else
}//------------xiao
public void linePassOne() {
if (y0 j) { // 第一按钮同行空按钮在左边
for (i = y0 - 1; i = j; i--) { // 判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i] != 0) {
if (y0 j) { // 第一按钮同行空按钮在右边
for (i = y0 + 1; i = j; i++) {
public void rowPassOne() {
if (x0 i) {//第一个按钮在与它同列的那个空格按钮下面
for (j = x0 - 1; j = i; j--) {
if (grid[j][y0] != 0) {
if (x0 i) {//第一个按钮在与它同列的那个空格按钮上面
for (j = x0 + 1; j = i; j++) {
public void remove() {
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation = false;
grid[x0][y0] = 0;
grid[x][y] = 0;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newlyButton) {
randomBuild();
if (e.getSource() == exitButton)
System.exit(0);
if (e.getSource() == resetButton)
reload();
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();
用java语言编写连连看游戏
我以前自己写一个玩的.没有把你要求的功能全部实现,不过你看了后可以改一下就好了.参考一下吧,我给了注解:
package mybase.programe;
/*
* lianliankan总体算法思路:由两个确定的按钮.若这两个按钮的数字相等,就开始找它们相连的路经.这个找路经
* 三条路经.若这三条路经上都是空按钮,那么就刚好是三种直线(两个转弯点)把两个按钮连接起来了)
* 1.相邻
*
* 与第一个按钮同行的那个空按钮竖向到与第二个按钮的同行看是否有按钮.没有的话路经就通了,可以消了.
* 那个空按钮横向到与第二个按钮同列看是否有按钮.没有的话路经就通了,可以消了.
* 若以上三步都失败,说明这两个按钮不可以消去.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LianLianKan implements ActionListener {
JPanel centerPanel, southPanel, northPanel; // 子面板
JButton exitButton, resetButton, newlyButton; // 退出,重列,重新开始按钮
JLabel fractionLable = new JLabel("0"); // 分数标签
int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0, validateLV; // 游戏按钮的位置坐标
thisContainer.add(centerPanel, "Center");
thisContainer.add(southPanel, "South");
thisContainer.add(northPanel, "North");
diamondsButton[cols][rows] = new JButton(String
public void estimateEven(int placeX, int placeY, JButton bz) {
if (fristMsg == secondMsg secondButton != firstButton) {
if ((x0 == x (y0 == y + 1 || y0 == y - 1))
|| ((x0 == x + 1 || x0 == x - 1) (y0 == y))) { // 判断是否相邻
if (grid[n][j] == 0 n == x - 1) {
if (grid[n][j] == 0 n == x + 1) {
if (grid[i][n] == 0 n == y - 1) {
if (grid[i][n] == 0 n == y + 1) {
if (e.getSource() == diamondsButton[cols][rows])
estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);
求连连看源代码
连连看的代码(基本算法)加了部分注释
public class lianliankan implements ActionListener
{
Frame f,f1;
TextField t; //用来显示一些随机信息,方法是下面的guli().
Label l;
int d[][]={ //用来和界面的按钮建立映射关系
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
public static void main(String[] args)
lianliankan t=new lianliankan();
t.suiji();
t.go();
public void actionPerformed(ActionEvent e) //再来一次按钮的响应事件.
int d[][]={
this.d=d;
suiji();
f.setVisible(false);
f1.setVisible(false);
s="no";
go();
public void go()//初始化界面
l=new Label("亲爱的粉丝,");
f=new Frame("连连看");
t=new TextField();
p1=new Panel();
bc=new Button("退出");
br=new Button("重列");
b=new Button();
b1=new Button(String.valueOf(d[1][1]));
p1.setBackground(Color.pink);
p1.setLocation(100,100);
br.setLocation(0,100);
f.add(p1);
p1.add(b1);
p1.add(b10);
p1.add(b11);
f.pack();
f.setResizable(false);
f.setVisible(true);
bc.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
ex();
});
br.addMouseListener(new MouseAdapter(){
chonglie();
b1.addMouseListener(new MouseAdapter(){
wei(1,1,b1);
b10.addMouseListener(new MouseAdapter(){
b11.addMouseListener(new MouseAdapter(){
public void ex() //退出界面,可用diolog来实现有模式的类型,更加符合
f1=new Frame("游戏作业");
f1.setLayout(new GridLayout(1,1));
bt1=new Button("确定退出");
f1.add(bt1);
f1.pack();
f1.setResizable(false);
f1.show();
f1.setVisible(true);
bt1.addMouseListener(new MouseAdapter(){
public void suiji() //产生随机数,来填充游戏界面对应的数组的各个位置
k++;
public void guli() //随机信息
int l=0;
t.setText("");
l=(int)(Math.random()*10);
System.out.println(l);
switch(l)
case 1:
t.setText("好!加油!");
t.setText("你真棒!");
t.setText("加快速度!");
t.setText("不错啊!");
t.setText("加油吧!");
t.setText("够聪明!");
default:
public void chonglie() //重列方法
save[n]=0; //定义一个数组来保存当前的每个按钮位置上的信息
n=0;
if(this.d[i][j]!=0)
save[n]=this.d[i][j];
n++;
n=n-1;
while(n=0) //产生随机位置,放置按钮
ling();
public void ling() //将数组中为零的成员对应的按钮消去
{ //用按钮类型的数组实现会简化得多,
if(d[1][1]==0)
b1.setVisible(false);
b10.setVisible(false);
b11.setVisible(false);
{ //当两次的按钮相同才能消去
if((s.trim()).equals("no"))
s=b1.getLabel();
x0=w1;
n1=d[x0][y0];
b=bz;
x=w1;
ba=bz;
else
x0=x;
y0=y;
b=ba;
public void xiao() //这里是整个游戏最重要的部分,就是判断两个按钮在信息
{ //相同的情况下能不能消去.仔细分析,不一条条注释
int i=0, j=0,n=0,k=0;
if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))) //相邻的情况
ba.setVisible(false);
b.setVisible(false);
guli();
d[x0][y0]=0;
d[x][y]=0;
if (d[x0][j]==0)
if (yj)
for (i=y-1;i=j;i-- )
if (d[x][i]!=0)
k=0;
k=1;
if (k==1)
if (y0j)
for (i=y0-1;i=j ;i-- )
if (d[x0][i]!=0)
for (i=y0+1;i=j ;i++)
for (i=y+1;i=j ;i++ )
if (y==j )
{ if (x0==x)
if (x0x)
for (n=x0;n=x-1;n++ )
if (d[n][j]!=0)
if(d[n][j]==0 n==x-1)
for (n=x0;n=x+1 ;n-- )
if(d[n][j]==0 n==x+1)
if (d[i][y0]==0)
if (xi)
for (j=x-1;j=i ;j-- )
if (d[j][y]!=0)
if (x0i)
for (j=x0-1;j=i ;j-- )
if (d[j][y0]!=0)
for (j=x0+1;j=i;j++ )
for (j=x+1;j=i;j++ )
for (j=x0+1;j=i ;j++ )
if (x==i)
if (y0==y)
if (y0y)
for (n=y0;n=y-1 ;n++ )
if (d[i][n]!=0)
if(d[i][n]==0 n==y-1)
for (n=y0;n=y+1 ;n--)
if(d[i][n]==0 n==y+1)