如何修复java.lang.outofmemoryerror异常?

发布于 2025-02-04 20:13:54 字数 4298 浏览 2 评论 0原文

好的,所以我正在尝试制作一个国际象棋游戏,主要是基于终端的游戏。我首先尝试编码可能的动作,例如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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪之触 2025-02-11 20:13:54

考虑何时停止此循环。

for (int i = possibleCol; i < this.getBoard().getColumns(); i--){

将终端条件与前循环中的条件进行比较:

for (int i = possibleRow; i >= 0; i--){ 

Consider when this loop stops.

for (int i = possibleCol; i < this.getBoard().getColumns(); i--){

Compare the end condition to the one in the prior loop over rows:

for (int i = possibleRow; i >= 0; i--){ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文