minemap.java

来自「eclipse环境下编写的java扫雷程序」· Java 代码 · 共 146 行

JAVA
146
字号
//雷区信息类---just the data information about the mine map!
import java.util.Random;
public class MineMap {
	public int [][]maps;  //-1代表雷,0~8代表周围的雷数---012345678 represent mines,-1 represent there is a mine
	private boolean [][]checked;         //只为openwhichShouldShow服务,标记是否已经执行过递归---just for recursion:mark the executed point
	//	雷区大小---size of the map
	static public int rows=10;           
    static public int colums=10;   
    //  雷数---the number of the mines
    static public int numberofMines=10;  
    //  递归检查哪些按钮应该打开---recursion to show which shound show
    public  void openwhichShouldShow(int r,int c)   
    {
        checked[r][c]=true;	//标记这个点说明已经递归过了---mark this point to show it has been executed
    	int [] round=new int [8]; //存储其周围8个方向的坐标(已经转化成线性坐标)---store the 8 direction linear position of this point
    	int sum=getaround(r,c,round);       //得到此按钮周围的按钮坐标---get the around linear point
    	int tempr,tempc;                  
    	for(int i=0;i<sum;i++)
    	{
    		//线性坐标转化为2维坐标存入临时变量---translate the linear point to x-y point 
    		tempr=round[i]/colums;          
    		tempc=round[i]%colums;
    		//显示出来---show
    		MineFrame.myButtons[tempr][tempc].lockthis();   
    		//如果此处的周围雷数还是0,继续打开其周围的--if there is no mine aroud (tempr,tempc),recursion!
    		if(maps[tempr][tempc]==0 && !checked[tempr][tempc])  
    		     openwhichShouldShow(tempr,tempc);   
    	}
    	
    }
 
    public MineMap() {
    	maps=new int[rows][colums];
    	checked=new boolean [rows][colums];
    }
    //设置雷区信息---set the information about the map
    static public void setData(int r,int c,int n)
    {
    	rows=r;
    	colums=c;
    	numberofMines=n;
    }
    //  重设地图---reset the map
    public void reset()
    {
    	generateMine();
    	generateNoMine();
    }   
    //随即的产生雷---generateMine by random
    private void generateMine()
    {
//    	先把所有初始化为0
    	for(int i=0;i<rows;i++)
    		for(int j=0;j<colums;j++)
    		{	maps[i][j]=0;
    		    checked[i][j]=false;
    		} 		
    	//根据总雷数随即产生雷,标记为-1
    	Random r=new Random();
    	for(int i=0;i<numberofMines;i++)
    	{	
    		while(true)
    		{
    			int row=r.nextInt(rows);
    		    int colum=r.nextInt(colums);
    		    if(maps[row][colum]!=-1)
    		    {
    			    maps[row][colum]=-1;
    		    	break;
    		    }
    		}  		
    	}	
    } //随即产生雷
   //根据一个点 得到周围点的坐标,返回一共有多少个有效的周围点(3~8)---
   //generate the points around one point,and return the numbers of legal "neighbors"(3~8)
    private  int getaround(int r,int c, int [] re )
    {
    	int i=0;
    	//left up
    	if(r-1>=0 && c-1>=0)
    	{
    		re[i]=(r-1)*colums+(c-1);i++;
    	}
    	//right up
    	if(r-1>=0 && c+1<colums)
    	{
    		re[i]=(r-1)*colums+(c+1);i++;
    	}
    	//left down
    	if(r+1<rows && c-1>=0)
    	{
    		re[i]=(r+1)*colums+(c-1);i++;
    	}
    	//right down
    	if(r+1<rows && c+1<colums)
    	{
    		re[i]=(r+1)*colums+(c+1);i++;
    	}
    	//up
    	if(r-1>=0)
    	{
    		re[i]=(r-1)*colums+(c);i++;
    	}
    	//down
    	if(r+1<rows)
    	{
    		re[i]=(r+1)*colums+(c);i++;
    	}
    	//left
    	if(c-1>=0)
    	{
    		re[i]=(r)*colums+(c-1);i++;
    	}
    	//right
    	if(c+1<colums)
    	{
    		re[i]=(r)*colums+(c+1);i++;
    	}
    	//返回周围有效的格子的总数
    	return i;
    } 
    //根据产生好的雷,产生每一个点周围的雷数---generate the numbers of mines around one point
    private void generateNoMine()
    {	
    	int [] round=new int [8];
    	for(int i=0;i<rows;i++)
    		for(int j=0; j<colums ;j++)
    		{
    			if(maps[i][j]==-1)  //每遇到一个雷,把其周围的格子雷数加一
    			{		
    			int ii=getaround(i,j,round);
    		    for(int jj=0;jj<ii;jj++)
    			{
    			     int temp=round[jj];
    			     if(maps[temp/colums][temp%colums]!=-1)
    					maps[temp/colums][temp%colums]++;
    	    	}
    			}
    			
    		}
    }  //根据雷算出每个点周围的雷数
    

    
    
}

⌨️ 快捷键说明

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