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

📄 distributing.java

📁 用JAVA实现的一个挖雷的小游戏 不过只是为了实现这个小游戏 所以没有增加级别等级
💻 JAVA
字号:
package com.xinxi.mine;

import java.util.Random;

public class Distributing {
	///private char thunder1[][];
	private static char thunder2[][];//存放所有块的内容.
	private int thunder[];//存放地雷的位置.
	private int count;
	private int lengthx;
	private int lengthy;
	public Distributing(){		
	}
	
	public Distributing(int count, int lengthx, int lengthy){
		this.count = count;
		this.lengthx = lengthx;
		this.lengthy = lengthy;
		int numAll = (lengthx)*(lengthy);
		//求哪个是雷的算法:
		thunder2 = new char[lengthx+2][lengthy+2];
		thunder = new int[count];
		
		//先把所有方块初使值定为'0'.
		for(int i=0; i<lengthx+2; i++){
			for(int j=0; j<lengthy+2; j++){
				thunder2[i][j]=48;
			}
		}
		//把是雷的改成'*'.
		Random rand = new Random();
		boolean flag=false;
		for(int i=0; i<count; i++){
			flag=true;
			int n=rand.nextInt(numAll)+1;//产生1-->numAll之间的数.
			for(int j=0; j<count; j++){
				if(n==thunder[j]){
					i--;
					flag=false;
					break;
				}
			}
			if(flag)
				thunder[i]=n;
		}
		/*
		for(int i=0; i<count; i++){
			System.out.println(thunder[i]);
		}
		*/
		for(int i=0; i<count; i++){
			int x=(thunder[i]-1)/lengthy+1;//确定其中雷的横坐标.
			int y=thunder[i]%lengthy;//确定其中雷的纵坐标.
			if(y==0){
				y=lengthy;
			}
			thunder2[x][y]='*';
			//System.out.println(x+"---"+y);
		}
		
		
		
		
		
		//求雷所对应数值的算法.
		for(int i=1; i<lengthx+1; i++){
			for(int j=1; j<lengthy+1; j++){
				if(thunder2[i][j]=='*'){
					if(thunder2[i][j-1]!='*')
						thunder2[i][j-1]++;
					if(thunder2[i][j+1]!='*')
						thunder2[i][j+1]++;
					if(thunder2[i-1][j-1]!='*')
						thunder2[i-1][j-1]++;
					if(thunder2[i-1][j+1]!='*')
						thunder2[i-1][j+1]++;
					if(thunder2[i+1][j+1]!='*')
						thunder2[i+1][j+1]++;
					if(thunder2[i+1][j-1]!='*')
						thunder2[i+1][j-1]++;
					if(thunder2[i+1][j]!='*')
						thunder2[i+1][j]++;
					if(thunder2[i-1][j]!='*')
						thunder2[i-1][j]++;					
				}
			}
		}
		
		
	}
	
	
	public void start(){
	}
	
	
	
	
	

	public static char[][] getThunder2() {
		return thunder2;
	}

	public static void setThunder2(char[][] thunder2) {
		Distributing.thunder2 = thunder2;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getLengthx() {
		return lengthx;
	}

	public void setLengthx(int lengthx) {
		this.lengthx = lengthx;
	}

	public int getLengthy() {
		return lengthy;
	}

	public void setLengthy(int lengthy) {
		this.lengthy = lengthy;
	}

	public int[] getThunder() {
		return thunder;
	}

	public void setThunder(int[] thunder) {
		this.thunder = thunder;
	}
	
	
	
	
	
	
	

	
	

	
	
}

⌨️ 快捷键说明

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