-3

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;
            }
        }
  • I guess I used a lot of variables too, but it doesn´t seem that not optimize to throw this type of error – VictorOrtins Jun 06 '22 at 18:32
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include your source code as a working [mcve], which can be compiled and tested by others. – Progman Jun 06 '22 at 18:34
  • Add more RAM to your JVM. – ethry Jun 06 '22 at 18:36

1 Answers1

1

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--){ 
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 1
    Lad, thanks a lot, really. Such an easy logical mistake that get´s lost within the lines of code. This was super helpful! – VictorOrtins Jun 06 '22 at 19:11