Search code examples
javaout-of-memorychess

How can I fix a java.lang.OutOfMemoryError exception?


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;
            }
        }

Solution

  • 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--){