added rook, created bishop file

This commit is contained in:
CT
2026-04-23 23:07:22 -05:00
parent ce61482b9e
commit 984c37a833
9 changed files with 148 additions and 8 deletions

72
Rook.java Normal file
View File

@@ -0,0 +1,72 @@
import java.util.*;
import javax.swing.ImageIcon;
public class Rook extends Piece {
boolean hasMoved;
public Rook(int x, int y, String color) {
super(x, y, color, new ImageIcon("sprites/" + color + "/rook.png").getImage());
hasMoved = false;
}
public ArrayList<Position> getLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>();
// check left
for (int i = pos.x - 1; i >= 0; i--) {
Position test = new Position(i, pos.y);
if (board.isOpen(test)) {
positions.add(test);
continue;
} else if (board.getPiece(test).colorMatches(this)) {
break;
} else {
positions.add(test);
break;
}
}
// check right
for (int i = pos.x + 1; i <= 7; i++) {
Position test = new Position(i, pos.y);
if (board.isOpen(test)) {
positions.add(test);
continue;
} else if (board.getPiece(test).colorMatches(this)) {
break;
} else {
positions.add(test);
break;
}
}
// check up
for (int i = pos.y + 1; i <= 7; i++) {
Position test = new Position(pos.x, i);
if (board.isOpen(test)) {
positions.add(test);
continue;
} else if (board.getPiece(test).colorMatches(this)) {
break;
} else {
positions.add(test);
break;
}
}
// check down
for (int i = pos.y - 1; i >= 0; i--) {
Position test = new Position(pos.x, i);
if (board.isOpen(test)) {
positions.add(test);
continue;
} else if (board.getPiece(test).colorMatches(this)) {
break;
} else {
positions.add(test);
break;
}
}
return positions;
}
}