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
Bishop.java Normal file
View File

@@ -0,0 +1,72 @@
import java.util.*;
import javax.swing.ImageIcon;
public class Bishop extends Piece {
boolean hasMoved;
public Bishop(int x, int y, String color) {
super(x, y, color, new ImageIcon("sprites/" + color + "/bishop.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;
}
}

Binary file not shown.

View File

@@ -6,10 +6,10 @@ public class Board {
public Board() { public Board() {
board = new Piece[8][8]; board = new Piece[8][8];
for (int i = 0; i <= 7; i++) { for (int i = 0; i <= 7; i++) {
board[i][2] = new Pawn(i + 1, 2, "Black"); board[i][1] = new Pawn(i, 1, "Black");
} }
for (int i = 0; i <= 7; i++) { for (int i = 0; i <= 7; i++) {
board[i][7] = new Pawn(i + 1, 7, "White"); board[i][6] = new Pawn(i, 6, "White");
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,8 +1,7 @@
import java.awt.*; import java.awt.*;
import java.util.*; import java.util.*;
public class Piece { public abstract class Piece {
ArrayList<Integer> legalMoves;
Position pos; Position pos;
Image sprite; Image sprite;
String color; String color;
@@ -17,10 +16,7 @@ public class Piece {
g.drawImage(sprite, pos.x * 40, pos.y * 40, null); g.drawImage(sprite, pos.x * 40, pos.y * 40, null);
} }
public ArrayList<Position> getLegalMoves(Board board) { public abstract ArrayList<Position> getLegalMoves(Board board);
System.out.println("parent method not overriden");
return null;
}
public boolean colorMatches(Piece p) { public boolean colorMatches(Piece p) {
return this.color.equals(p.color); return this.color.equals(p.color);

BIN
Rook.class Normal file

Binary file not shown.

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