-1

我正在为 Minesweeper 创建代码并尝试实现 GUI。但问题是,当我运行代码并玩游戏时,我在棋盘上单击的位置会显示答案板上该位置的 y、x 坐标,而不是 x、y 坐标。我一直在尝试解决这个问题,但我无法弄清楚。我想这可能是我创建董事会的方式,但我尝试了我能想到的一切。

class Board{

public MsGUI gui;

private static char[][] userBoard;
private static char[][] solutionBoard;
private static int boundSize = 5;

public Board(){
 userBoard = new char[][]   {{'-','-','-','-','-'},
                              {'-','-','-','-','-'},
                              {'-','-','-','-','-'},
                              {'-','-','-','-','-'},
                              {'-','-','-','-','-'}};  

 solutionBoard = new char[][] {{'0','2','B','2','0'},
                {'0','3','B','3','0'},
                {'1','3','B','3','1'},
                {'B','1','3','B','2'},
                {'1','1','2','B','2'}};

 return;
    }    

private static void printBoard(char[][] board){      
     for (int x = 0; x < boundSize; x++){
        for(int y = 0; y < boundSize; y++){
            System.out.print(" " + Character.toString(board[x][y]));
        }
        System.out.println("");
    }        

    System.out.println("");
}

public void flagCell(int xCoordinate, int yCoordinate){
    userBoard[xCoordinate][yCoordinate] = 'F';


}

public boolean isFlagged(int xCoordinate,int yCoordinate){

if(userBoard[xCoordinate][yCoordinate] == 'F'){

return true;

}

else{

return false;

}
}

public int getHeight() {

return userBoard.length;

}

public int getWidth(){

return userBoard[0].length;
}

public char getValue(int xCoordinate, int yCoordinate) {

return userBoard[xCoordinate][yCoordinate];

}
private static boolean checkIfAlreadyMarked(int xCoordinate, int yCoordinate)
{
    boolean marked = false;

    if (Character.toString(userBoard[xCoordinate][yCoordinate]).equals("-") == false)
    {
        marked = true;
    }

    return marked;
}


public void revealCell(int xCoordinate, int yCoordinate){

    int count = 0;

for(int i = 0;i < userBoard.length;i++){

    for(int J = 0;J < userBoard[i].length;J++){

        if(userBoard[i][J] != '-'){
        count = count + 1;
    }
}

if(count == 19){

gui.win("you won");

return;

}

}

    if(solutionBoard[xCoordinate][yCoordinate] == 'B'){
        userBoard[xCoordinate][yCoordinate] = solutionBoard[xCoordinate][yCoordinate];
        gui.lose("You lost. Better luck next time!");
        return;
    }   
    if(solutionBoard[xCoordinate][yCoordinate] != '0'){
        userBoard[xCoordinate][yCoordinate] = solutionBoard[xCoordinate][yCoordinate];
    }else{

        userBoard[xCoordinate][yCoordinate] = solutionBoard[xCoordinate][yCoordinate];

        for(int i = 1; i > -2; i--){

            if(xCoordinate-i >= solutionBoard.length || xCoordinate-i < 0)
                continue;

            for(int z = 1; z > -2; z--){

                if(yCoordinate-z >= solutionBoard[xCoordinate].length || yCoordinate-z < 0)
                    continue;


                else if(userBoard[xCoordinate-i][yCoordinate-z] == 'F' || userBoard[xCoordinate-i][yCoordinate-z] != '-')
                    continue;

                else{
                    revealCell(xCoordinate-i, yCoordinate-z);
                }
            }
        }   
    }





}



  public void unflagCell(int xCoordinate, int yCoordinate){

userBoard[xCoordinate][yCoordinate]='-';

}
public static void main(String[] args){
Board b = new Board();
b.gui = new MsGUI(b);
b.gui.setVisible(true);


}
}
4

2 回答 2

2

您初始化 solutionBoard 的方式不是您所期望的。如果你得到solutionBoard[0],你不是在访问第一列(这与我认为你的理解一致),而是第一行(二维数组的第一项):{'0', '2','B','2','0'}

因此,如果您希望将 x 作为行索引,将 y 作为列索引,并且仍然保持这种“可读”初始化,那么无论何时访问数组都必须交换索引。但这只会帮助你解决一个问题——一开始是人类可读的数组分配,但我认为你将来会后悔这个决定。

编辑:您可以根据需要初始化数组,并且仍然使用这样的可读格式:

String readableBoard =
            "0 2 B 2 0;" +
            "0 3 B 3 0;" +
            "1 3 B 3 1;" +
            "B 1 B B 2;" +
            "1 1 2 B 2";
char[][] board = initBoard(readableBoard);

....

private char[][] initBoard(String readableBoard){
    char[][] board = new char[5][5];
    String[] rows = readableBoard.split(";");
    String[] fields = null;
    for (int y = 0; y<rows.length;y++){
        fields = rows[y].split(" ");
        for (int x = 0; x<fields.length; x++){
            board[x][y]=fields[x].charAt(0);
        }
    }
    return board;
}

现在当你打电话

board[2][0]

你会得到'B'

于 2013-04-26T13:43:10.790 回答
0

如果您查看嵌套的 for 循环,您正在打印列而不是我假设的所需行。尝试切换你的 for 循环来迭代 y,然后是 x。

   for (int y = 0; y < boundSize; y++){
    for(int x = 0; x < boundSize; x++){
        System.out.print(" " + Character.toString(board[x][y]));
    }
    System.out.println("");
}        

System.out.println("");
于 2013-04-26T13:53:10.483 回答