如何修复java.lang.outofmemoryerror异常?
好的,所以我正在尝试制作一个国际象棋游戏,主要是基于终端的游戏。我首先尝试编码可能的动作,例如Rook,Queen,您知道国际象棋是如何工作的。
但是,每次我尝试以主教,菜单和女王的可能动作获取列表时,这个Java.lang.outofmemoryerror都会出现。我使用了很多语句来创建此可能的动作列表,但是它似乎并不是一个非常复杂的代码,以至于应用程序缺乏内存,因此我只会在此处删除代码,看看是否有人可以帮助我优化它或做某事以防止对角线
移动
possibleCol = col + 1;
for (int i = row + 1; i < this.getBoard().getRows(); i++){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol++;
}
possibleCol = col + 1;
for (int i = row - 1; i < this.getBoard().getRows(); i--){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol++;
}
possibleCol = col - 1;
for (int i = row + 1; i < this.getBoard().getRows(); i++){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol--;
}
possibleCol = col - 1;
for (int i = row - 1; i < this.getBoard().getRows(); i--){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol--;
}
rok移动
for (int i = possibleRow; i < this.getBoard().getRows(); i++){
Piece pieceAt = this.getBoard().getPieceAt(i, col);
Location location = new Location(i, col);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
possibleRow = row - 1;
for (int i = possibleRow; i >= 0; i--){
Piece pieceAt = this.getBoard().getPieceAt(i, col);
Location location = new Location(i, col);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
int possibleCol = col + 1;
for (int i = possibleCol; i < this.getBoard().getColumns(); i++){
Piece pieceAt = this.getBoard().getPieceAt(row, i);
Location location = new Location(row, i);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
possibleCol = col - 1;
for (int i = possibleCol; i < this.getBoard().getColumns(); i--){
Piece pieceAt = this.getBoard().getPieceAt(row, i);
Location location = new Location(row, i);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
Ok, so I´m trying to make a chess game, primarily a terminal based one. I´m first trying to code possible moves a piece can make according to it´s type, like rook, queen, u know how chess works.
But, this java.lang.OutOfMemoryError appears everytime I try to get the list with the possible movements of a Bishop, a Rook and a Queen. I used a lot of for statements to create this list of possible movements, but it doesn´t seem at all a really complex code to the point where the application lacks memory, so I´ll just drop the codes here and see if anyone can help me optimize it or do something to prevent this error
Diagonal moves
possibleCol = col + 1;
for (int i = row + 1; i < this.getBoard().getRows(); i++){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol++;
}
possibleCol = col + 1;
for (int i = row - 1; i < this.getBoard().getRows(); i--){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol++;
}
possibleCol = col - 1;
for (int i = row + 1; i < this.getBoard().getRows(); i++){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol--;
}
possibleCol = col - 1;
for (int i = row - 1; i < this.getBoard().getRows(); i--){
if (this.getBoard().checkCoordinate(i, possibleCol)){
pieceAt = this.getBoard().getPieceAt(i, possibleCol);
location = new Location(i, possibleCol);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
}
}
possibleCol--;
}
Rook moves
for (int i = possibleRow; i < this.getBoard().getRows(); i++){
Piece pieceAt = this.getBoard().getPieceAt(i, col);
Location location = new Location(i, col);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
possibleRow = row - 1;
for (int i = possibleRow; i >= 0; i--){
Piece pieceAt = this.getBoard().getPieceAt(i, col);
Location location = new Location(i, col);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
int possibleCol = col + 1;
for (int i = possibleCol; i < this.getBoard().getColumns(); i++){
Piece pieceAt = this.getBoard().getPieceAt(row, i);
Location location = new Location(row, i);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
possibleCol = col - 1;
for (int i = possibleCol; i < this.getBoard().getColumns(); i--){
Piece pieceAt = this.getBoard().getPieceAt(row, i);
Location location = new Location(row, i);
if (pieceAt == null){
possibleMoves.add(location);
}
else if (pieceAt != null){
possibleKills.add(location);
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑何时停止此循环。
将终端条件与前循环中的条件进行比较:
Consider when this loop stops.
Compare the end condition to the one in the prior loop over rows: