连连看java代码解析,连连看java代码(2) | 学步园

接上篇,还剩下主功能模块的代码没贴。连连看

连连看算法

e8bf9ee8bf9ee79c8be7ae97e6b395-300x179.jpg

如图, 为了找出A, B两点之间的连接路径, 首先过这两点作4条线段, 线段的两端便是地图边缘, 两条与横坐标轴平行, 另两条与纵坐标轴平行. 先考虑与横坐标轴平行的两条.

在两条线段上各取一点C和D, 此两点处在一条与纵坐标轴平行的直线上. 那么, ACDB这条路径便是一条可能的A, B两点的连通路径.

C, D两点在两条线段上移动, 直到找出一条有效的连通路径, 或者最终得出结论不存在这样的路径.

按同样的方式在与纵坐标轴平行的两条线段上查找.

算法优化

两点的连通路径应该是最短的, 所以, 查找从A, B所处的矩形的中线开始, 同时从上下左右4个方面查找, 可以找到看起来最短的连通路径.

e8bf9ee8bf9ee79c8be7ae97e6b3952-300x179.jpg

这个是我看到的最简洁的表述,借此引用过来,相信大家一看也就了然了。只要check AC,CD,DB为连通的,就说明A,B可以互联。用一个数组grid[ ][ ]映射有没有空位,然后一个Button组储存图片信息

以下是代码:

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JPanel;

public class CenterPanel extends JPanel implements ActionListener {

private static final long serialVersionUID = 1L;

MyButton[][] allButtons;

int row;

int col;

int grid[][];//储存游戏按钮位置

int pictureCount = 10;

String path;

String folder;

String[] filenames;

static boolean selectedFlag =false;

MyButton lastSelectButton,currentSelectButton;

int middleX=-2;

int middleY=-2;

public CenterPanel(int row,int col){

this.row=row;

this.col=col;

init();

}

private void createButtons() {

System.out.println(path+folder);

for(int i=0;i

for(int j=0;j

allButtons[i][j]=new MyButton(i,j,grid[i+1][j+1]);

MyButton b = allButtons[i][j];

Icon icon = new ImageIcon(path+folder+filenames[b.getPictrueId()-1]);

b.setIcon(icon);

b.setSize(icon.getIconWidth(),icon.getIconHeight());

allButtons[i][j].addActionListener(this);

}

}

}

private void createPictrueID(){

allButtons = new MyButton[row][col];

grid = new int[row+2][col+2];

path = System.getProperty("user.dir");

folder = "\\src\\Icon\\";

File file = new File(path, folder);

filenames = file.list();

pictureCount =filenames.length;

int randoms,r,c;

for(int twins=1;twins<= row*col/2;twins++) {

randoms=(int)(Math.random()*pictureCount + 1);

for(int i = 1;i <= 2; i++) {

r=(int)(Math.random()*row+1);

c=(int)(Math.random()*col+1);

while(grid [r][c]!=0){

r=(int)(Math.random()*row+1);

c=(int)(Math.random()*col+1);

}

grid[r][c]=randoms;

}

}

}

private void init(){

createPictrueID();

createButtons();

this.setBounds(100,100,row*allButtons[0][0].getWidth(),col*allButtons[0][0].getHeight());

this.setLayout(new GridLayout(row,col));

for(int i=0;i

for(int j=0;j

this.add(allButtons[i][j]);

}

}

}

public void actionPerformed(ActionEvent e) {

MyButton b =(MyButton) e.getSource();

if(selectedFlag == false){

lastSelectButton =b;

b.setBackground(Color.red);

selectedFlag = true;

}

else if(lastSelectButton != b){

lastSelectButton.setBackground(null);

currentSelectButton = b;

if(lastSelectButton.getPictrueId() == currentSelectButton.getPictrueId()){

int x1 = lastSelectButton.getRow();

int y1 = lastSelectButton.getCol();

int x2 = currentSelectButton.getRow();

int y2 = currentSelectButton.getCol();

if(checkOutisMatch(lastSelectButton,currentSelectButton)){

allButtons[x1][y1].setVisible(false);

this.grid[x1+1][y1+1] = 0;

allButtons[x2][y2].setVisible(false);

this.grid[x2+1][y2+1] = 0;

int point=Integer.parseInt(MainView.fractionLable.getText().toString())+ 10;

MainView.fractionLable.setText(point+"");

MainView.count++;

this.revalidate();

}

}

selectedFlag =false;

}

}

private boolean checkOutisMatch(MyButton b1, MyButton b2) {

if(checkIsNear(b1.getRow(),b1.getCol(),b2.getRow(),b2.getCol())){

return true;

}

else{

double moveX = (b1.getRow()+b2.getRow())/2.0;

double moveY = (b1.getCol()+b2.getCol())/2.0;

for(int n =0;n

if(checkX(b1,b2,(int)(moveX+0.5)+n)>= -1){

middleX =(int)(moveX+0.5)+n;

return true;

}

if(checkX(b1,b2,(int)moveX-n)>= -1){

middleX =(int)moveX-n;

return true;

}

if(checkY(b1,b2,(int)(moveY+0.5)+n) >= -1){

middleY =(int)(moveY+0.5)+n;

return true;

}

if(checkY(b1,b2,(int)moveY-n) >= -1){

middleY =(int)moveY-n;

return true;

}

}

return false;

}

}

private int checkY(MyButton b1, MyButton b2, int moveY) {

if(moveY <= col && moveY>=-1){

MyButton b3 =new MyButton();

MyButton b4 =new MyButton();

b3.setRow(b1.getRow());b3.setCol(moveY);

b4.setRow(b2.getRow());b4.setCol(moveY);

if(checkRow(b1.getRow(),b1,b3)&&checkRow(b2.getRow(),b2,b4)&&checkCol(moveY,b1,b2)){

return moveY;

}

}

return -2;

}

private int checkX(MyButton b1, MyButton b2,int moveX) {

if(moveX<=row && moveX>=-1){

MyButton b3 =new MyButton();

MyButton b4 =new MyButton();

b3.setRow(moveX);b3.setCol(b1.getCol());

b4.setRow(moveX);b4.setCol(b2.getCol());

if(checkCol(b1.getCol(),b1,b3)&&checkCol(b2.getCol(),b2,b4)&&checkRow(moveX,b1,b2)){

return moveX;

}

}

return -2;

}

private boolean checkRow(int x,MyButton b1,MyButton b2){

MyButton right = (b1.getCol() >= b2.getCol())?b1:b2;

MyButton left= (right == b1?b2:b1);

if(x==right.getRow()&&x!=left.getRow()){

for(int i = left.getCol();i

if(grid[x+1][i+1] != 0){

return false;

}

}

}

else if(x==left.getRow()&&x!=right.getRow()){

for(int i = left.getCol()+1;i <= right.getCol();i++){

if(grid[x+1][i+1] != 0){

return false;

}

}

}

else if(x==left.getRow()&&x == right.getRow()){

for(int i = left.getCol()+1;i

if(grid[x+1][i+1] != 0){

return false;

}

}

}

else{

for(int i = left.getCol();i <= right.getCol();i++){

if(grid[x+1][i+1] != 0){

return false;

}

}

}

return true;

}

private boolean checkCol(int y,MyButton b1,MyButton b2){

MyButton up = (b1.getRow() >= b2.getRow())?b1:b2;

MyButton down= (up == b1?b2:b1);

if(y ==up.getCol()&& y != down.getCol()){

for(int i = down.getRow();i

if(grid[i+1][y+1] != 0){

return false;

}

}

}

else if(y !=up.getCol()&& y == down.getCol()){

for(int i = down.getRow()+1;i <=up.getRow();i++){

if(grid[i+1][y+1] != 0){

return false;

}

}

}

else if(y ==up.getCol()&& y == down.getCol()){

for(int i = down.getRow()+ 1;i

if(grid[i+1][y+1] != 0){

return false;

}

}

}

else{

for(int i = down.getRow();i <= up.getRow();i++){

if(grid[i+1][y+1] != 0){

return false;

}

}

}

return true;

}

private boolean checkIsNear(int x1, int y1, int x2, int y2) {

if((x1==x2&&Math.abs(y2-y1)==1)||(y2==y1&&Math.abs(x1-x2)==1)){

return true;

}

else

return false;

}

}

补充:在SRC目录下创建一个Icon文件夹,然后把想要的图片放入即可,代码会自动获取图片信息。不然就是一片空白,切记切记。。。以上4个类放入一个包下就可以了。

java连连看代码 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.*; public class Game extends JFrame{ private int num[][]; //实现数组 private JButton gamebutton[][]; //游戏Button组 private Container cont; //内容面板 private JLabel timelabel; //时间标签 private JButton play; //开始游戏按钮 private JButton exit; //退出按钮 private JButton replay; //重新开始按钮 private JButton back; //返回主界面按钮 private JComboBox choice;//游戏等级 private JButton chongle; //游戏按钮重列 private JPanel gamepanel; //游戏按钮面板 private JPanel panel; //开始游戏面板 private int time=0; //时间记录 private Time T; //记时线程 private Thread t=new Thread(T); //记时线程 private int shu=1; //记录获取游戏按钮事件的个数 private int x=0,y=0,a=0,b=0;//按钮的坐标 private JButton button1=null,button2=null;//点击了的按钮 private boolean can;//能否消除 private int lvx,lvy; //游戏等级数组标列数 private int framex,framey; //框架大小 private int again=1;//记录重列次数 public Game(){ itincomponent(); } private void itincomponent(){ //设置面板属性 if(cont!=null) cont.removeAll(); this.setSize(500, 500); this.setLocationRelativeTo(null); //获取内容面板 cont=this.getContentPane(); //开始和退出按钮 panel=new JPanel(); play=new JButton("开始游戏"); choice=new JComboBox(); choice.addItem("低级"); choice.addItem("中级"); choice.addItem("高级"); choice.setSelectedItem("低级"); exit=new JButton("退出游戏"); panel.add(choice); panel.add(play); panel.add(exit); cont.add(panel,"South"); exit.addActionListener(new Exit());//结束事件监听 play.addActionListener(new Play());//开始事件监听 this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } ////获得新的面板 private void p(){ //设置面板属性 this.setSize(framex, framey); this.setLocationRelativeTo(null); //获取内容面板 cont=this.getContentPane(); //开始和退出按钮 panel=new JPanel(); exit=new JButton("退出游戏"); replay=new JButton("重新开始"); back=new JButton("返回主界面"); chongle=new JButton("重列"); panel.add(chongle); panel.add(replay); panel.add(exit); panel.add(back); cont.add(panel,"South"); exit.addActionListener(new Exit());//结束事件监听 replay.addActionListener(new Playbutton());//重新开始事件监听 back.addActionListener(new Back());//返回主界面事件监听 chongle.addActionListener(new Chongle());//重列事件监听 //时间面板 timelabel=new JLabel("游戏时间:"+time+"s",JLabel.CENTER); cont.add(timelabel,"North"); //生成游戏操作面板 gamepanel=new JPanel(); gamepanel.setLayout(new GridLayout(lvx,lvy,3,3)); //设置为表格布局 for (int i = 0; i < lvx; i++) { for (int j = 0; j <lvy; j++) { if(gamebutton[i][j].isVisible()){ gamebutton[i][j].setIcon(new ImageIcon(num[i][j]+".jpg")); } gamepanel.add(gamebutton[i][j]); gamebutton[i][j].addActionListener(new Gamebutton()); } } cont.add(gamepanel, "Center"); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值