Implement prevention of teleportation for possible moves

main
Elnu 2 years ago
parent 8294306734
commit 023d0b9a4b

@ -5,14 +5,14 @@ public class Bishop extends Piece {
super(black, panel, "black-bishop.png", "white-bishop.png"); super(black, panel, "black-bishop.png", "white-bishop.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>(); ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
for(int i = 1; i <= 7; i++) {
possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); getPossibleMovesInDirection(1, 1, position, board, possibleMoves);
possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i)); getPossibleMovesInDirection(-1, -1, position, board, possibleMoves);
possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i)); getPossibleMovesInDirection(1, -1, position, board, possibleMoves);
possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i)); getPossibleMovesInDirection(-1, 1, position, board, possibleMoves);
}
return possibleMoves; return possibleMoves;
} }
} }

@ -142,9 +142,6 @@ public class Board {
} }
public void draw(ScreenCoordinate mousePosition) { public void draw(ScreenCoordinate mousePosition) {
// Draw board // Draw board
graphics.setColor(BLACK); graphics.setColor(BLACK);
graphics.fillRect(0, 0, DIMENSION, DIMENSION); graphics.fillRect(0, 0, DIMENSION, DIMENSION);

@ -5,7 +5,7 @@ public class King extends Piece {
super(black, panel, "black-king.png", "white-king.png"); super(black, panel, "black-king.png", "white-king.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>(); ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1)); possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 1)); possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 1));

@ -6,7 +6,7 @@ public class Knight extends Piece {
super(black, panel, "black-knight.png", "white-knight.png"); super(black, panel, "black-knight.png", "white-knight.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>(); ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 2, position.y - 1)); possibleMoves.add(new BoardCoordinate(position.x - 2, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 2)); possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 2));

@ -5,7 +5,7 @@ public class Pawn extends Piece {
super(black, panel, "black-pawn.png", "white-pawn.png"); super(black, panel, "black-pawn.png", "white-pawn.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>(); ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
if (this.black) { if (this.black) {
possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); possibleMoves.add(new BoardCoordinate(position.x, position.y + 1));

@ -13,10 +13,10 @@ public abstract class Piece {
image = panel.loadImage(black ? blackImagePath : whiteImagePath); image = panel.loadImage(black ? blackImagePath : whiteImagePath);
} }
public abstract ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position); public abstract ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board);
public ArrayList<BoardCoordinate> getLegalMoves(BoardCoordinate position, Board board) { public ArrayList<BoardCoordinate> getLegalMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> legalMoves = getPossibleMoves(position); ArrayList<BoardCoordinate> legalMoves = getPossibleMoves(position, board);
for (int i = 0; i < legalMoves.size(); i++) { for (int i = 0; i < legalMoves.size(); i++) {
BoardCoordinate possibleMove = legalMoves.get(i); BoardCoordinate possibleMove = legalMoves.get(i);
Piece targetPiece = board.get(possibleMove); Piece targetPiece = board.get(possibleMove);
@ -46,4 +46,15 @@ public abstract class Piece {
public void draw(Graphics graphics, ImageObserver observer, ScreenCoordinate coordinate) { public void draw(Graphics graphics, ImageObserver observer, ScreenCoordinate coordinate) {
draw(graphics, observer, coordinate.x, coordinate.y); draw(graphics, observer, coordinate.x, coordinate.y);
} }
void getPossibleMovesInDirection(int dx, int dy, BoardCoordinate position, Board board, ArrayList<BoardCoordinate> possibleMoves) {
for (
int x = position.x + dx, y = position.y + dy;
!board.outOfBounds(x, y);
x += dx, y += dy) {
BoardCoordinate coordinate = new BoardCoordinate(x, y);
possibleMoves.add(coordinate);
if (board.get(coordinate) != null) break;
}
}
} }

@ -5,7 +5,7 @@ public class Queen extends Piece {
super(black, panel, "black-queen.png", "white-queen.png"); super(black, panel, "black-queen.png", "white-queen.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>(); ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
for(int i = 1; i <= 7; i++) { for(int i = 1; i <= 7; i++) {
possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i));

@ -5,7 +5,7 @@ public class Rook extends Piece {
super(black, panel, "black-rook.png", "white-rook.png"); super(black, panel, "black-rook.png", "white-rook.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>(); ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
for(int i = 1; i <= 7; i++) { for(int i = 1; i <= 7; i++) {
possibleMoves.add(new BoardCoordinate(position.x - i, position.y)); possibleMoves.add(new BoardCoordinate(position.x - i, position.y));