gpt4 book ai didi

java - 解决扫雷代码问题

转载 作者:行者123 更新时间:2023-11-30 07:52:54 24 4
gpt4 key购买 nike

我正在尝试使用与其相邻的地雷数量相关的值(数字)填充二维数组

我的思考过程是将每个单元格与相邻单元格进行比较,但我收到了越界异常。

我尝试了几种不同的方法,但似乎无法理解其中的逻辑。我觉得数组让我失望。

游戏板(类)

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();
}

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()
{
System.out.println("\n Lines");

for(int i = 0 ; i < Row ; i++)
{
System.out.print(" "+ (i+1) + " ");
}

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";
}
}

GameClient(类)

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]);
}
}

最佳答案

代码中至少存在两个问题。

这些行中 randomMines 中的第一个

tempRow = (int)(Math.random() * Row);
tempColumn = (int)(Math.random() * Column);

Math.random() 可能返回 1.0,因此 tempRow 将等于 Row,这是问题所在,因为最后一个索引是 行 - 1(和列 - 1)。所以让它像

tempRow = (int)(Math.random() * (Row - 1));
tempColumn = (int)(Math.random() * (Column - 1));

另一个问题是在循环中的 startBoard

for(int i=0 ; i<mines.length ; i++) {
for(int j=0 ; j<mines.length ; j++) {
...
}
}

您有 mines[0].length 列,正如您在 startMines 方法中编写的那样。

我看到的最后一个错误是在 toString 方法中。它甚至无法编译。它应该看起来像这样

public String toString()
{
System.out.println("\n Lines");

for(int i = 0 ; i < Row ; i++) {
System.out.print(" " + (i + 1) + " ");
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";
}

但这并没有起到应有的作用,您打印值而不是将它们转换为String。最好使用StringBuilder

public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("\n Lines\n");

for(int i = 0 ; i < Row ; i++) {
builder.append(" " + (i + 1) + " ");
for (int j = 0; j < Column; j++) {
builder.append(" " + mines[i][j]);
}
builder.append("\n");
}
builder.append("\n 1 2 3 4 5 6 7 8\n Columns");
return builder.toString();
}

您也可以使用String#format漂亮地打印你的主板。

关于java - 解决扫雷代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com