在2d阵列上的扫雷程序java x和y被切换

时间:2013-04-26 13:35:46

标签: java user-interface multidimensional-array

我为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);


}
}

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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("");