如何更改二维阵列棒?
how do I make changes to a 2d array stick?
我查了几个类似的问题,还没找到有用的。
我正在创建一个棋盘游戏,我正在使用二维数组。我正在尝试将“Room”对象添加到“Board”对象。
食宿都是二维数组。我将它们保存为字符串数组,并在绘制方法中 运行 遍历它们以显示位置(这是棋盘的一小部分)。
我正在尝试更改 Board 二维数组的内容以包含 Room 二维数组。
这是董事会Class:
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);
但无论我怎么尝试,我都无法找到一个“房间”来展示。
感谢任何帮助!
您似乎忽略了 Board::addToSquare
中 currentItems.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
}
}
我查了几个类似的问题,还没找到有用的。
我正在创建一个棋盘游戏,我正在使用二维数组。我正在尝试将“Room”对象添加到“Board”对象。
食宿都是二维数组。我将它们保存为字符串数组,并在绘制方法中 运行 遍历它们以显示位置(这是棋盘的一小部分)。
我正在尝试更改 Board 二维数组的内容以包含 Room 二维数组。
这是董事会Class:
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);
但无论我怎么尝试,我都无法找到一个“房间”来展示。
感谢任何帮助!
您似乎忽略了 Board::addToSquare
中 currentItems.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
}
}