Skip to main content
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Post Reopened by RubberDuck, Simon Forsberg, syb0rg, Marc-Andre, Jamal
edited body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Chess generate Generating possible Chess moves

Here is the solution to generating possible moves and keeping the king safe. If someone is willing to look it over and come with some suggestion to perhaps improve it., I would appreciate it.

Full source code is at githubGitHub

Chess generate possible moves

Here is the solution to generating possible moves and keeping the king safe. If someone is willing to look it over and come with some suggestion to perhaps improve it. I would appreciate it.

Full source code is at github

Generating possible Chess moves

Here is the solution to generating possible moves and keeping the king safe. If someone is willing to look it over and come with some suggestion to perhaps improve it, I would appreciate it.

Full source code is at GitHub

Found a bug with bishop in the for loops instead of > -1 it was > -0 oooops.
Source Link
jetersen
  • 101
  • 1
  • 1
  • 5
@Override
public Collection<Square> generatePossibleMoves() {
    int row = super.getSquare().ROW;
    int column = super.getSquare().COLUMN;
    possibleMoves.clear();
    //all possible moves in the down positive diagonal
    for (int j = column + 1, i = row + 1; j < Board.SIZE && i < Board.SIZE; j++, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up positive diagonal
    for (int j = column - 1, i = row + 1; j > -01 && i < Board.SIZE; j--, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up negative diagonal
    for (int j = column - 1, i = row - 1; j >=> 0-1 && i > -0;1; j--, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the down negative diagonal
    for (int j = column + 1, i = row - 1; j < Board.SIZE && i > -0;1; j++, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    return possibleMoves;
}
@Override
public Collection<Square> generatePossibleMoves() {
    int row = super.getSquare().ROW;
    int column = super.getSquare().COLUMN;
    possibleMoves.clear();
    //all possible moves in the down positive diagonal
    for (int j = column + 1, i = row + 1; j < Board.SIZE && i < Board.SIZE; j++, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up positive diagonal
    for (int j = column - 1, i = row + 1; j > -0 && i < Board.SIZE; j--, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up negative diagonal
    for (int j = column - 1, i = row - 1; j >= 0 && i > -0; j--, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the down negative diagonal
    for (int j = column + 1, i = row - 1; j < Board.SIZE && i > -0; j++, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    return possibleMoves;
}
@Override
public Collection<Square> generatePossibleMoves() {
    int row = super.getSquare().ROW;
    int column = super.getSquare().COLUMN;
    possibleMoves.clear();
    //all possible moves in the down positive diagonal
    for (int j = column + 1, i = row + 1; j < Board.SIZE && i < Board.SIZE; j++, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up positive diagonal
    for (int j = column - 1, i = row + 1; j > -1 && i < Board.SIZE; j--, i++) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the up negative diagonal
    for (int j = column - 1, i = row - 1; j > -1 && i > -1; j--, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    //all possible moves in the down negative diagonal
    for (int j = column + 1, i = row - 1; j < Board.SIZE && i > -1; j++, i--) {
        Square square = super.getSquare().getBoardSquare(i, j);
        if (square.getPiece() == null) {
            possibleMoves.add(square);
        } else if (isOpponent(square.getPiece())) {
            possibleMoves.add(square);
            break;
        } else {
            break;
        }
    }
    return possibleMoves;
}
added 3979 characters in body; edited title
Source Link
jetersen
  • 101
  • 1
  • 1
  • 5
Loading
Post Closed as "Not suitable for this site" by Emily L., Marc-Andre, Adam, Malachi, palacsint
Source Link
jetersen
  • 101
  • 1
  • 1
  • 5
Loading