⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamepanel.java

📁 “连连看”源程序 “连连看”源程序 “连连看”源程序
💻 JAVA
字号:
/**
 * 
 *@FileName GamePanel.java 05/10/20
 * 
 *@Copyright (c) 2005 Tender, Inc. All Rights Reserved.
 * 
 *@changeTime
 *
 *@version V1.0
 *
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class GamePanel extends JPanel implements ActionListener{
	
	private int row, col;	//game button's row and col's number 
	
	private int blockNum;	//game leave's block 
	
	private int imgIndex[]; //ths game picture's index
	
	private MyButton bt[][], tempBt[]; //game button's number and temp button's number
	
	private int count=0;    //control mouse click state
	
	private int point[][];	// use save empty point;	
	
	private int x1 = 0, y1 = 0, x2 = 0, y2 = 0; // two point's position
	
	private int tempPX1 = 0, tempPY1 = 0, tempPX2 = 0, tempPY2 = 0;
	
	public GamePanel(){
				
		initGameState();		
	}
	
	//init game's state e.g. row, col's number and so on.
	public void initGameState(){
		
		row = 12;
		
		col = 12;
		
		this.setSize( 50*row, 50*col );
		
		GridLayout gridLayout = new GridLayout(); 
		
		gridLayout.setRows( row );
		
		gridLayout.setColumns( col );
		
		gridLayout.setVgap(2);
		
		gridLayout.setHgap(2);
		
		this.setLayout( gridLayout);
		
		blockNum = (row-2)*(col-2);
		
		point = new int[row][col];
		
		imgIndex = new int[ (blockNum/4) ];
		
		count = 0;
		
		initImgIndex();
		
		initButton();
		
		addButton();
	 
	}
	
	//inition game button display image's number
	private void initImgIndex(){
		
		for( int i = 0; i < (blockNum/4); i++ ){
			
			imgIndex[i] = 4;
			
		}
		
	}
		
	//inition game button
	private void initButton() 
	{
		bt = new MyButton[row][col];
		
		for( int i=0; i < row; i++ ){
			
			for( int j=0; j < col; j++ ){
				
				bt[i][j] = new MyButton();
				
				bt[i][j].addActionListener(this);
				
				if( ( i == 0 ) || ( i == (row - 1) ) || ( j == 0 ) || ( j == (col - 1) ) ){
					 
					bt[i][j].setVisible(false);
					
					point[i][j] = 0;
				
				}else{
					
					Random rand = new Random();
					
					while(true){
						
						int temp = ( rand.nextInt( (blockNum/4) ) + 1) ;
					
						if( imgIndex[ temp-1 ] == 0 ){
							
							continue;
						
						}else{
							
							bt[i][j].setIcon( new ImageIcon("images/"+temp+".JPG"));
							
							bt[i][j].setVisible(true);
							
							bt[i][j].flage = temp;
							
							bt[i][j].setBackground( Color.WHITE );
							
							point[i][j] = 1;
							
							bt[i][j].x = i;
							
							bt[i][j].y = j;
							
							imgIndex[ (temp - 1) ]--;					
							
							break;
						
						}//end of if( imgIndex[ temp-1 ] == 0 )
							
					}//end of whils
						
				}//end of if
					
			}//end of for j
				
		}// end of for i
	}
	
	// add button to panel
	private void addButton(){
		
		for( int i = 0; i < row; i++ ){
			
			for( int j =0; j< col; j++ ){
				
				this.add(bt[i][j]);
			
			}
		
		}
	
	}
	
	//check two point wether can link
	private boolean isLink( int px1, int py1, int px2, int py2 ){
		
		boolean link = false;
		
		if( ( px1 == px2 ) && ( py1 == py2 ) ){
			
			return true;
		
		}
		
		//相临点算法
		if( ( px1 == px2 ) && ( Math.abs(py1 - py2) == 1) ){
			
			return true;
		
		}else if( ( py1 == py2 ) && ( Math.abs(px1 - px2)==1) ){
			
			return true;
			
		}
		
		
		//同一直线上算法
		if( px1 == px2 ){
			
			if( py1 < py2 ){
				
				for( int i = py1+1; i<py2; i++){
					
					link = true;
					
					if( point[px1][i] != 0){
						
						link = false;
						
						break;
						
					}// end of if( point[px1][i] != 0)	
									
				}// end of for
							
			}// end of if( py1 < py2 )	
			
			if( py1 > py2){
				
				for( int i = py1-1; i>py2; i--){
					
					link = true;
					
					if( point[px1][i] != 0){
						
						link = false;
						
						break;
						
					}//	end of if( point[px1][i] != 0)				
				
				}//end of for	
				
			}//end of if( py1 > py2)
			
		}else if( py1 == py2){
			
			if( px1 < px2 ){
				
				for( int i = px1+1; i<px2; i++){
					
					link = true;
					
					if( point[i][py1] != 0){
						
						link = false;
						
						break;
						
					}// end of if ( point[i][py1] != 0)					
				
				}//end of for			
			
			}//end of if( px1 < px2 )	
			
			if( px1 > px2){
				
				for( int i = px1-1; i>px2; i--){
					
					link = true;
					
					if( point[i][py1] != 0){
						
						link = false;
						
						break;
						
					}// end of if( point[i][py1] != 0)
										
				}// end of for
					
			}// end of if( px1 > px2)
			
		}//end of if( px1 == px2 )
		
		return link;
			
	}
	
	private boolean disposePoint( int px1, int py1, int px2, int py2 ){
		
		boolean isDispose = false;
		
		int zx1=0, zy1=0, zx2=0, zy2=0;
		
		if(isLink(px1, py1, px2, py2)){
			
			return true;
			
		}
		
		for( int i = py1; i < col; i++ ){
			
			if( point[px1][i]==0 && ( point[px2][i]==0 || i == py2) ){
				
				zx1 = px1;
				zy1 = i;
				zx2 = px2;
				zy2 = zy1;
				
			}else{
				
				continue;
				
			}//end of if
			
			if(isLink( px1, py1, zx1, zy1)&&isLink(zx1, zy1, zx2, zy2)&&isLink(zx2, zy2, px2, py2)){
				
				return true;
				
			}
			else{
				
				isDispose = false;
				
				continue;
				
			}// end of if
			
		}//end of for
		
		for( int i = py1; i>=0; i--){
				
			if( point[px1][i]==0 && ( point[px2][i]==0 || i == py2) ){
				
				zx1 = px1;
				zy1 = i;
				zx2 = px2;
				zy2 = zy1;
				
			}else{
				
				continue;
				
			}// end of if
			
			if(isLink( px1, py1, zx1, zy1)&&isLink(zx1, zy1, zx2, zy2)&&isLink(zx2, zy2, px2, py2)){
				
				return true;
			
			}else{
				
				isDispose = false;
				
				continue;
			
			}// end of if
		
		}// end of for
		
		for( int i = px1; i < row; i++ ){
			
			if( point[i][py1]==0 && ( point[i][py2]==0 || i == px2) ){
				
				zx1 = i;
				zy1 = py1;
				zx2 = zx1;
				zy2 = py2;
				
			}else{
				
				continue;
				
			}//end of if
			
			if(isLink( px1, py1, zx1, zy1)&&isLink(zx1, zy1, zx2, zy2)&&isLink(zx2, zy2, px2, py2)){
				
				return true;
			
			}else{
				
				isDispose = false;
				
				continue;
			
			}// end of if
			
		}// end of for
		
		for( int i = px1; i >= 0; i-- ){
			
			if( point[i][py1]==0 && ( point[i][py2]==0 || i == px2) ){
				
				zx1 = i;
				zy1 = py1;
				zx2 = zx1;
				zy2 = py2;
				
			}else{
				
				continue;
				
			}// end of if
			
			if(isLink( px1, py1, zx1, zy1)&&isLink(zx1, zy1, zx2, zy2)&&isLink(zx2, zy2, px2, py2)){
				
				return true;
				
			}else{
				
				isDispose = false;
				
				continue;
			
			}// end of if
			
		}// end of for
		
		return isDispose;
		
	}//end of isDisposePoint
	
	private void disposeBt(int x1, int y1, int x2, int y2){
		
		bt[x1][y1].setVisible(false);
		bt[x2][y2].setVisible(false);
		//bt[x1][y1].flage = 0;
		//bt[x2][y2].flage = 0;
		point[x1][y1] = 0;
		point[x2][y2] = 0;
	}
	
	public void begin()
	{
		for(int i=1; i < row - 1; i++ ){
			
			for( int j=1; j < col - 1; j++){
				
				if(point[i][j] == 0)
				{
					
					bt[i][j].setVisible(true);
					
					point[i][j]=1;
				
				}
				
			}
		
		}
		
		//reRange();
	}
	public void tiShi(){
		
		//use save temp position
		int tempX = 1, tempY = 1;
		
		bt[x1][y1].setBackground( Color.WHITE );
		
		bt[x2][y2].setBackground( Color.WHITE );
		
		boolean stop = false;
		
		boolean isBreak = false;
		
		while( !stop ){
			
			if(point[tempX][tempY] != 0){
			
				for( int i = 1; i < row-1; i++ ){
				
					for( int j = 1; j <col-1; j++ ){
					
						if(tempX != i || tempY != j ){
						
							if(disposePoint(tempX, tempY, i, j) 
								&& bt[tempX][tempY].flage == bt[i][j].flage 
								&& bt[tempX][tempY].isVisible()
								&& bt[i][j].isVisible()){
							
								bt[i][j].setBackground(Color.blue);
							
								bt[tempX][tempY].setBackground(Color.blue);
								
								tempPX1 = i;
								
								tempPY1 = j;
								
								tempPX2 = tempX;
								
								tempPY2 = tempY;
								
								stop=true;
							
								isBreak = true;
							
								break;
							
							}else{
							
								stop = false;
							
							}// end of if(disposePoint(tempX, tempY, i, j) && bt[tempX][tempY].flage == bt[i][j].flage)
						
						}// end of if(tempX != i || tempY != j )
					
					}//end of for j 
				
					if( isBreak ){
						
						break;
					}
					
				}//end of for i
				
			}//end of if 
			
			tempY++;
			
			if( tempY == col-1 ){
				
				tempX++;
				
				if(tempX == row-1) stop = true;
				
				tempY = 1;
			}
			
		}//end of while
						
	}
	
	public void reRange(){
		
		

		int c=0;
		
		for(int i = 1; i < row; i++ ){
			
			for( int j = 1; j < col; j++){
				
				if(point[i][j] != 0){
					
					c++;
					
				}//end of if
				
			}//end of for j
			
		}//end of for i
		
		tempBt = new MyButton[c];
		
		int z = 0;
		
		for(int i = 1; i < row; i++ ){
			
			for( int j = 1; j < col; j++){
				
				if(point[i][j] != 0){
					
					tempBt[z] = new MyButton();
					
					tempBt[z].flage = bt[i][j].flage;
					
					tempBt[z].setIcon(bt[i][j].getIcon());
					
					bt[i][j].setVisible(false);
					
					bt[i][j].flage = 0;
					
					point[i][j] = 0;
					
					z++;
					
				}//end of if
				
			}//end of for j
			
		}// end of for i
		
		Random rand = new Random();
		
		for( int z1=0; z1<c; z1++){
	
			while( true ){
				
				int tempX=(rand.nextInt(row-2)+1);
				
				int tempY=(rand.nextInt(col-2)+1);
				
				if(point[tempX][tempY] == 0){
					
					bt[tempX][tempY].flage = tempBt[z1].flage;
					
					bt[tempX][tempY].setIcon(tempBt[z1].getIcon());
					
					bt[tempX][tempY].setVisible(true);
					
					point[tempX][tempY]=1;
					
					break;
					
				}// end of if(point[tempX][tempY] == 0)
				
			}// end of while
			
		}//end of for
		
		bt[tempPX1][tempPY1].setBackground( Color.WHITE );
			
		bt[tempPX2][tempPY2].setBackground( Color.WHITE );

	}
	
	public int getPanelWidth(){
		
		return row*40;
		
	}
	
	public int getPanelHeight(){
		
		return col*40;
		
	}
	
	public void actionPerformed( ActionEvent e ){
		
		if(e.getSource() instanceof MyButton){
			
			MyButton mb = (MyButton) e.getSource();
			
			this.count++;
			
			bt[tempPX1][tempPY1].setBackground( Color.WHITE );
			
			bt[tempPX2][tempPY2].setBackground( Color.WHITE );
			
			if( count%2 == 1 ){
				
				x1 = mb.x;
				
				y1 = mb.y;
				
				bt[x1][y1].setBackground( Color.BLUE );
				
				bt[x2][y2].setBackground( Color.WHITE );
								
				if( x2 == x1 && y2 == y1 ){
					
				}else{
					
					if( bt[x1][y1].flage ==bt[x2][y2].flage && disposePoint(x1, y1, x2, y2) ){
						
						disposeBt(x1, y1, x2, y2);
						
						bt[x1][y1].setBackground( Color.WHITE );
						
						this.blockNum = this.blockNum - 2;	
									
					}
				}//end of if(x2 == x1 && y2 == y1)
				
			}else if( count % 2 == 0 ){
				
				x2 = mb.x;
				
				y2 = mb.y;
				
				bt[x2][y2].setBackground( Color.white );
				
				bt[x1][y1].setBackground( Color.WHITE );
				
				if( x2 == x1 && y2 == y1 ){
					
				}else{
					
					if( bt[x1][y1].flage == bt[x2][y2].flage && disposePoint(x1, y1, x2, y2) ){
						
						disposeBt( x1, y1, x2, y2 );
						
						bt[x2][y2].setBackground( Color.WHITE );
									
						this.blockNum = this.blockNum - 2;
											
					}// end of if
					
				}//end of if(x2 == x1 && y2 == y1)
				
				x1 = 0;
				x2 = 0;
				y1 = 0;
				y2 = 0;
						
			}//end of if( count%2 == 1)
			
		}//end of if(e.getSource() instanceof MyButton)
		
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -