簡體   English   中英

找出掃雷代碼的問題

[英]Problems figuring out minesweeper code

我正在嘗試使用與相鄰排雷數量相關的值(數字)填充二維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]);
    }
}

代碼中至少有兩個問題。

這些行的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++) {
        ...
    }
}

您在startMines方法中編寫了mines[0].length列。

我看到的最后一個錯誤是在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漂亮地打印您的電路板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM