【问题标题】:how do I make changes to a 2d array stick?如何更改二维阵列棒?
【发布时间】:2021-03-29 19:40:26
【问题描述】:

我查了几个与此类似的问题,但没有发现任何有用的东西。

我正在创建一个棋盘游戏,我正在使用二维数组。我正在尝试将“房间”对象添加到“板”对象。

食宿都是二维数组。我将它们保留为字符串数组并在 draw 方法中遍历它们以显示位置(这是棋盘的一小部分)。

我正在尝试将 Board 2d 数组的内容更改为包含 Room 2d 数组。

这是董事会课程:

public class Board {

    private String[][] board;

    public Board(int x, int y) {

        board = new String[x][y];

        for (int i = 0;i < x; i++) {
            for (int j = 0; j < y; j++) {
                board[i][j] = "floor";
            }
        }
    }

    public void addToSquare(int x, int y, String item) {
        String currentItems = this.getFromSquare(x,y);
        currentItems.concat(item);
        board[x][y] = currentItems;
    }

    public void clearSquare(int x, int y) {
        board[x][y] = "";
    }

    public void addRoom(Room room, int centrePointX, int centrePointY) {

        for (int i = 0; i < room.roomX; i++) {
            for(int j = 0; j < room.roomY; j++) {

                String roomContents = room.getContents(i,j);
                int roomOriginX = room.radX;
                int roomOriginY = room.radY;
                this.clearSquare((centrePointX-2 + i), (centrePointY-2 + j));
                this.addToSquare((centrePointX-2 + i), (centrePointY-2 + j), roomContents);
            }
        }
    }

    public String getFromSquare(int x, int y) {
        return board[x][y];
    }

    public String[][] getShownPosition(int playerX, int playerY, int shownWidth, int shownHeight){

        String[][] shownPosition = new String[shownWidth][shownHeight];
        int radX = shownWidth / 2;
        int radY = shownHeight /2;

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

                if (!((playerX-radX)+i <0 || (playerY -radY)+j <0 || (playerX-radX)+i >= board.length || (playerY -radY)+j >= board.length)) {
                    shownPosition[i][j] = board[(playerX - radX) + i][(playerY - radY) + j];
                }else{
                    shownPosition[i][j] = "blackSpace";
                }
            }
        }
        return shownPosition;
    }

}

当我开始绘制位置时,我会运行“ShownPosition”并根据字符串显示图像。

这可能不是正确的方法,但是 - 直到房间部分 - 一切正常。

问题是房间没有出现。如您所见,我用“墙壁”填满了整个房间,只是为了更容易看到它何时加载。

我添加了“clearsquare”和“addtosquare”方法,就像我之前所说的 board[x][y] = roomContents。

在我写的更新方法中

shownPosition = mBoard.getShownPosition(player.boardx,
                                        player.boardy,
                                        shownBoardX,
                                        shownBoardY);

但无论我尝试什么,我都无法获得一个“房间”来展示。

任何帮助表示赞赏!

【问题讨论】:

    标签: java arrays 2d


    【解决方案1】:

    您似乎忽略了Board::addToSquarecurrentItems.concat(item); 的结果,因此它实际上从未向其中添加任何内容。应该是

    public void addToSquare(int x, int y, String item) {
        String currentItems = this.getFromSquare(x,y);
        currentItems = currentItems.concat(item);
        board[x][y] = currentItems;
    }
    

    此外,当涉及到二维数组和坐标时,请确保您实际存储值并使用正确的坐标访问它们。例如查看以下代码生成的内容:

    for(int x=0; x<3; x++){
        System.out.println("\n");
        for(int y=0; y<5; y++){
            System.out.printf("%s,%s\t", x, y);
        }
    }
    

    输出:

    0,0 0,1 0,2 0,3 0,4 
    
    1,0 1,1 1,2 1,3 1,4 
    
    2,0 2,1 2,2 2,3 2,4 
    

    您可能会注意到,数组是以 Y 实际代表列而 X 代表行的方式生成的。为了在您的代码中解决此问题,您需要以相反的顺序生成每个二维数组,即首先遍历行然后遍历列:

    for (int j=0; j<y; j++){        // <- these two
        for (int i=0; i<x; i++){    // <-  lines
            board[i][j] = "floor";
        }
    }
    

    或切换坐标:

    for (int i=0; i<x; i++){
        for (int j=0; j<y; j++){
            board[j][i] = "floor"; // <- here
        }
    }
    

    【讨论】:

    • hmm 我想这将在稍后发挥作用.. 目前两个数组的长度相同,所以它并没有真正的区别。我可以让它显示地板而不是“房间”对象,就像将“地板”更改为“墙”
    • @litlee 我已经更新了答案,请阅读第一段。
    • 啊,我明白了!现在这似乎可行了
    • @litlee 很高兴听到这个消息。如果您可以将答案标记为正确,那将很有帮助,这样问题就不会悬而未决。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2011-01-03
    • 2012-04-01
    相关资源
    最近更新 更多