java - 解决扫雷代码问题

标签 java oop arrays

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

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

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

游戏板(类)

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/

相关文章:

java - Java 中的通用字段多态性

java - 使用 Java 准确确定 HDFS 中的文件以加载到一个 Hbase 表中?

inheritance - 你为什么要对整个类(class)进行密封/最终定级?

c# - 在父构造函数中创建子对象

c - 数组理解:

c - 用 C 中的 fgets 覆盖我的数组

c - 在c中将二维数组从一个函数传递到另一个函数?

Java 程序是否仅为某些类型的 CSV 文件创建扫描仪?

java - @RabbitListener 以 "stopped"模式开始绑定(bind)到扇出交换

oop - 如何向过程程序员教授面向对象编程?