试图找出,如何使用数字填充新数组或当前数组,对应于与其相邻的地雷数量。我一直在尝试一些不同的方式,我似乎无法理解逻辑。我觉得数组会让我失望。我的思考过程是将每个单元格与相邻单元格进行比较,但是,我会得到一个超出范围的异常。总的来说,我的问题是,我如何编写一个方法来填充当前地雷的数组,以及每个单元格中的数字,对应于与其相邻的多少个地雷。
import java.util.Random;
import java.util.Scanner;
public class GameBoard
{
private int[][] mines;
private char[][] tileCount;
private int[][] sol;
private int Row, Column, mine;
Random random = new Random();
Scanner input = new Scanner(System.in);
public GameBoard(int Row, int Column, int mine)
{
this.Row = Row;
this.Column = Column;
this.mine = mine;
mines = new int[Row][Column];
tileCount = new char[Row][Column];
startMines();
randomMines();
fillTips();
startBoard();
adCell();
}
public void startMines(){
for(int i=0 ; i<mines.length ; i++)
for(int j=0 ; j<mines[0].length ; j++)
mines[i][j]=0;
}
public void randomMines()
{
double x = (Row * Column) * .25;
int tempRow, tempColumn;
int j=0;
for(int i=0 ; j < this.mine ; i++)
{
tempRow = (int)(Math.random() * Row);
tempColumn = (int)(Math.random() * Column);
if(mines[tempRow][tempColumn] == 0)
{
mines[tempRow][tempColumn] = 9;
++j;
}
}
}
public void showMines(){
for(int i=0 ; i < Row; i++)
for(int j=0 ; j < Column ; j++)
if(mines[i][j] == '*')
System.out.print(tileCount[i][j]='*');
}
public void fillTips(){
for(int line=0 ; line < Row ; line++)
for(int column=0 ; column < Column ; column++){
for(int i=-1 ; i<=0 ; i++)
for(int j=-1 ; j<=0 ; j++)
if(mines[line][column] != -1)
if(mines[line][column] == -1)
mines[line][column]++;
}
}
public void startBoard(){
for(int i=0 ; i<mines.length ; i++)
for(int j=0 ; j<mines.length ; j++)
tileCount[i][j]= '.';
}
public String toString()
{
for(int i = 0 ; i < Row ; i++){
System.out.print(" "+ i + " ");
for(int j = 0 ; j < Column ; j++){
System.out.print( " "+ mines[i][j]);
}
System.out.println();
}
return "\n 1 2 3 4 5 6 7 8\n Columns";
}
}
import java.util.Scanner;
import java.util.Arrays;
public class GameClient {
int grid;
public static void main(String args[]) {
System.out.println("Welcome to the game minesweeper, I hope you enjoy your stay.");
System.out.println("We will begin by asking how large you would like the game.");
System.out.println("---------------------------------------------------------------");
System.out.println("Input two values please: ");
Scanner grid = new Scanner(System.in);
int grid1 = grid.nextInt();
int grid2 = grid.nextInt();
double mineCount = ((grid1*grid2)*.25);
System.out.println("Enter a number of mines, please 25% less than the total tiles which is " + mineCount);
Scanner mineCounts = new Scanner(System.in);
mineCount = mineCounts.nextInt();
GameBoard[][] tileSize = new GameBoard[grid1][grid2];
tileSize[0][0] = new GameBoard(grid1, grid2, (int)mineCount);
System.out.println(tileSize[0][0]);