【发布时间】: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);
但无论我尝试什么,我都无法获得一个“房间”来展示。
任何帮助表示赞赏!
【问题讨论】: