现在的位置: 首页 > 综合 > 正文

连连看java代码(2)

2017年11月24日 ⁄ 综合 ⁄ 共 5617字 ⁄ 字号 评论关闭

接上篇,还剩下主功能模块的代码没贴。连连看算法,怎么求最短路径问题

连连看算法

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

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

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

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

算法优化

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

以上内容来自互联网:http://www.benegg.com/game/archives/34.html

这个是我看到的最简洁的表述,借此引用过来,相信大家一看也就了然了。只要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<row;i++){
			for(int j=0;j<col;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<row;i++){
			for(int j=0;j<col;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<Math.max(row+2,col+2);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 <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 <= 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 <right.getCol();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 <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 <=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 <up.getRow();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个类放入一个包下就可以了。

抱歉!评论已关闭.