我正在从事游戏类项目并陷入困境。
我一直在寻找如何检查二维Array
中的任何列是否已满的情况,如果是,则完全清除该列。
我对Java
真的很陌生,所以如果你能帮我解决这个问题!
这是我到目前为止的代码。
//removes filled columns - added method
private boolean removeFullCol()
{
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[i].length; j++){
if(board[i][j] != occupied){
return false;
}
}
}
//empty square was never found - column is full
return true;
}
答案 0 :(得分:1)
如果您只想删除完整的cols,请按cols检查,但如果要检查所有列,则无法返回布尔值。您有两个选择:
boolean
)int
)第一个选项:
private boolean removeFullCol(int col)
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] == null){
return false;
}
}
return true;
}
第二个选项(返回第一个col full或data,否则返回-1):
private int removeFullCol()
for (int row=0; row<board[col].length; row++) {
int filled = 0;
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] != null){
filled ++;
}
}
// when finished check how many rows are filled
if (board[col].length == filled)
return row;
}
return -1;
}
我在飞行中编写代码 ...如果有任何错误或疑问,请告诉我......