[英]how to print the row numbers in a 2d array game board in java
I am working on a java Othello game and am using a 2D array with padding to build the board. 我正在研究Java Othello游戏,并且正在使用带有填充的2D数组来构建棋盘。 I have the board printing just fine, the columns are labeled "a -h" but i need the rows to be numberd "1-8" and cannot figure out how to do this.
我的电路板印刷得很好,列标记为“ a -h”,但我需要将行编号为“ 1-8”,并且无法弄清楚该怎么做。 my code is as follows:
我的代码如下:
void printBoard() {
String results = "";
OthelloOut.printComment(" a b c d e f g h");
int row = board.board.length;
int col = board.board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
}
the othelloOut class sort of extends the System.out print statements othelloOut类扩展了System.out打印语句
public class OthelloOut {
static public void printMove(PieceColor color, Move amove){
System.out.printf("%s %s\n", color, amove);
}//printMove
static public void printComment(String str){
System.out.printf("C %s\n", str);
}//printComment
static public void printReady(PieceColor color){
System.out.printf("R %s\n", color);
}//printReady
}//OthelloOut
any help will be much appreciated. 任何帮助都感激不尽。 If this needs to be clarified more just let me know!
如果需要进一步澄清,请告诉我! thanks.
谢谢。
UPDATE: Numbers print but i prints 0 - 9 and i want it to skip the digits 0 and 9 to where its blank in the positions of those two numbers. 更新:数字打印,但我打印0-9,我希望它跳过数字0和9到这两个数字的位置空白。 Any suggestions?
有什么建议么? Thanks for the help guys!
感谢您的帮助!
Your best bet is doing it here: 您最好的选择是在这里进行操作:
for (int i = 0; i < row; i++) {
OthelloOut.printComment(i); // Obviously not exactly like this.
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
Remember that you're not using println
, you're using print
. 请记住,您不是在使用
println
,而是在使用print
。 You want all other text to be printed onto the same line as i. 您希望将所有其他文本打印到与i相同的行上。
And while I'm here..
而当我在这里..
I would be using a StringBuilder
, instead of concatenating a String
. 我将使用
StringBuilder
,而不是连接String
。
for (int i = 0; i < row; i++) {
StringBuilder results = new StringBuilder();
OthelloOut.printComment(i); // Obviously not exactly like this.
for (int j = 0; j < col; j++) {
results.append(pieces[board.board[i][j] + 2]);
}
OthelloOut.printComment(results.toString());
}
You can add the row number at each row iteration like this: 您可以在每次行迭代中添加行号,如下所示:
for (int i = 0; i < row; i++) {
results += i + 1; // add the row number
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.