二维数组游戏地图行编号
2d array gamemap line numbering
我正在尝试创建一个带有行编号的 2d 游戏场,但我无法理解如何在 java 中正确编码编号。
到目前为止,我所做的只是将行和列输入到 9。
对于任何两位数或更高的数字,它无法正确显示地图,因为我没有足够的 space。我可以添加到块让我们说两个空白 spaces 这样行编号可以达到数百个,但我正在尝试获得不受限制的地图。
非常感谢任何帮助。
这是我现在拥有的:
public String toString() {
String output;
output = "gamemap:\n ";
for (int j = 0; j < this.y; j++) { // loop over columns
output += j;
}
output += "\n *";
for (int j = 0; j < this.y; j++) { // loop over columns
output += "*";
}
output += "*\n";
for (int i = 0; i < this.x; i++) { // loop over rows
output += i + "*";
for (int j = 0; j < this.y; j++) { // loop over columns
output += this.map[i][j];
}
output += "*\n";
}
output += " *";
for (int j = 0; j < this.y; j++) { // loop over columns
output += "*";
}
output += "*\n";
output += "The player has "+ this.player.lives + " lives left.";
return output;
}
private String getPaddedNumber(int number, int maxNumber) {
if(maxNumber <= number) return Integer.toString(number);
int buffer = number;
while((buffer /= 10) != 0 & (maxNumber /= 10) != 0) {
//nothing
}
StringBuilder sb = new StringBuilder();
while(maxNumber != 0) {
sb.append('0'); //alternatively sb.append(' ');
maxNumber /= 10;
}
return sb.append(number).toString();
}
number 是您要显示的行号,max Number 是您拥有的行数(或者更准确地说,是您拥有的最高行索引)
我正在尝试创建一个带有行编号的 2d 游戏场,但我无法理解如何在 java 中正确编码编号。
到目前为止,我所做的只是将行和列输入到 9。
对于任何两位数或更高的数字,它无法正确显示地图,因为我没有足够的 space。我可以添加到块让我们说两个空白 spaces 这样行编号可以达到数百个,但我正在尝试获得不受限制的地图。
非常感谢任何帮助。 这是我现在拥有的:
public String toString() {
String output;
output = "gamemap:\n ";
for (int j = 0; j < this.y; j++) { // loop over columns
output += j;
}
output += "\n *";
for (int j = 0; j < this.y; j++) { // loop over columns
output += "*";
}
output += "*\n";
for (int i = 0; i < this.x; i++) { // loop over rows
output += i + "*";
for (int j = 0; j < this.y; j++) { // loop over columns
output += this.map[i][j];
}
output += "*\n";
}
output += " *";
for (int j = 0; j < this.y; j++) { // loop over columns
output += "*";
}
output += "*\n";
output += "The player has "+ this.player.lives + " lives left.";
return output;
}
private String getPaddedNumber(int number, int maxNumber) {
if(maxNumber <= number) return Integer.toString(number);
int buffer = number;
while((buffer /= 10) != 0 & (maxNumber /= 10) != 0) {
//nothing
}
StringBuilder sb = new StringBuilder();
while(maxNumber != 0) {
sb.append('0'); //alternatively sb.append(' ');
maxNumber /= 10;
}
return sb.append(number).toString();
}
number 是您要显示的行号,max Number 是您拥有的行数(或者更准确地说,是您拥有的最高行索引)