英文:
Creating a toString Method for a game representing a 1D grid in 2D
问题
这是我的代码:
public String toString() {
StringBuilder b = new StringBuilder();
int maxRowsIndex = game.numRows - 1;
int maxColumnsIndex = game.numColumns - 1;
String lineSeparator = Utils.repeat("---", game.numColumns) + Utils.repeat("-", game.numColumns - 1);
b.append("POSITION: " + currentPosition + "(ODDS: " + totalOdds + ")\n");
for (int i = 0; i < game.numRows; i++) {
for (int j = 0; j < game.numColumns; j++) {
int index = i * game.numColumns + j;
b.append(" ");
b.append(toString(index));
b.append(" ");
if (j < maxColumnsIndex) {
b.append("|");
}
}
// Line separator after each row, except the last
if (i < maxRowsIndex) {
b.append("\n");
b.append(lineSeparator);
b.append("\n");
}
}
return b.toString();
}
private String toString(int index) {
// int lookupIndex = game.boardIndexes[index];
for (int i = 0; i < game.board.length; i++) {
if (game.board[i] == CellValue.EMPTY) {
return boardOdds[i] + "";
} else {
return game.board[i] + "";
}
}
return "";
}
CellValue 有3个选项,X、O 或 EMPTY
game 表示正在进行的井字棋游戏
board 是游戏棋盘的数组,其中包含了 CellValues
boardOdds 是我想要在我的 toString 方法中显示的数组,但只在棋盘上空位(EMPTY)的位置显示,如果它们已经被填满,则应显示 X 或 O。目前为止,如果该位置为空(boardOdds[0]),我得到的是 boardOdds 的第一个值的重复。如果该位置不为空,它会给我 board[0](即 X 或 O)。
如果它是 EMPTY(例如 board[0] == CellValue.EMPTY),我希望看到 boardOdds 的值。如果它不是 EMPTY,我希望看到 board 的值(X 或 O)。
目前的问题是我的第二个 toString 方法 (private String toString(int index)) 中的 i++ 永远不会被执行。
输出结果看起来像这样:
POSITION: 4(ODDS: 31)
8 | 8 | 8
-----------
8 | 8 | 8
-----------
8 | 8 | 8
应该是这样的:
POSITION: 4(ODDS: 31)
8 | 8 | 4
-----------
4 | 2 | 2
-----------
1 | 1 | 1
英文:
I am attempting to view my 1D array as a 2D array in a game of tic tac toe. However in my grid i only view the first element (element [0])in all 9 spaces.
This is my code:
public String toString() {
StringBuilder b = new StringBuilder();
int maxRowsIndex = game.numRows - 1;
int maxColumnsIndex = game.numColumns - 1;
String lineSeparator = Utils.repeat("---", game.numColumns) + Utils.repeat("-", game.numColumns - 1);
b.append("POSITION: " + currentPosition + "(ODDS: "+ totalOdds +")\n");
for (int i = 0; i < game.numRows; i++) {
for (int j = 0; j < game.numColumns; j++) {
int index = i*game.numColumns + j;
b.append(" ");
b.append(toString(index));
b.append(" ");
if (j < maxColumnsIndex) {
b.append("|");
}
}
// Line separator after each row, except the last
if (i < maxRowsIndex) {
b.append("\n");
b.append(lineSeparator);
b.append("\n");
}
}
return b.toString();
}
private String toString(int index) {
//int lookupIndex = game.boardIndexes[index];
for(int i =0; i<game.board.length; i++){
if (game.board[i] == CellValue.EMPTY){
return boardOdds[i] + "";
}
else {
return game.board[i] + "";
}
}
return "";
}
CellValue has 3 options, X O or EMPTY
game represents an ongoing game of Tic Tac Toe
board is the array for the game board that has the CellValues
boardOdds is the array I'm trying to show in my toString method, but only in the board spots that are EMPTY, if they are full they should show X or O, as of right now, i get the first value of boardOdds repeated if the slot is EMPTY(boardOdds[0]). If the slot is not empty it gives me the board[0] (either X or O).
if it is EMPTY (i.e. board[0] == CellValue.EMPTY) i would like to see the boardOdds Value. If it is not EMPTY, I would like to see the board value (X or O).
the current issue is the i++ in my 2nd toString method (private String toString(int index)) is never reached.
The Output looks like:
POSITION: 4[(ODDS: 31)
8 | 8 | 8
-----------
8 | 8 | 8
-----------
8 | 8 | 8]
It should be :
POSITION: 4[(ODDS: 31)
8 | 8 | 4
-----------
4 | 2 | 2
-----------
1 | 1 | 1]
答案1
得分: 0
目前的问题是我的第二个 toString 方法(private String toString(int index))中的 i++ 永远不会被执行。
这是因为你在 for 循环内部使用了 return
,所以只会执行一次迭代。你可以使用 StringBuilder
来生成你的字符串:
private String toString(int index) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < game.board.length; i++) {
if (game.board[i] == CellValue.EMPTY) {
sb.append(boardOdds[i] + "");
} else {
sb.append(game.board[i] + "");
}
}
return sb.toString();
}
英文:
> the current issue is the i++ in my 2nd toString method (private String
> toString(int index)) is never reached.
That is because you use return
inside the for-loop, so only one iteration will be done. You can use a StringBuilder
to generate your string:
private String toString(int index) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < game.board.length; i++) {
if (game.board[i] == CellValue.EMPTY) {
sb.append(boardOdds[i] + "");
} else {
sb.append(game.board[i] + "");
}
}
return sb.toString();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论