计算扫雷舰代码的问题

时间:2015-10-13 23:03:02

标签: java oop arrays

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

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

我一直在尝试一些不同的方法,但我似乎无法理解逻辑。我觉得阵列让我失望。

游戏板(类)

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

1 个答案:

答案 0 :(得分:1)

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

首先在这些行中的randomMines

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

Math.random()可能会返回1.0,因此tempRow将等于Row这是问题,因为最后一个索引是Row - 1(和Column - 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来打印您的电路板。