king, copy board logic, getPseudoMoves

This commit is contained in:
CT
2026-04-24 13:34:00 -05:00
parent 984c37a833
commit c3318f834c
16 changed files with 185 additions and 120 deletions

BIN
Bishop.class Normal file

Binary file not shown.

View File

@@ -2,71 +2,28 @@ import java.util.*;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
public class Bishop extends Piece { public class Bishop extends Piece {
boolean hasMoved;
public Bishop(int x, int y, String color) { public Bishop(int x, int y, String color) {
super(x, y, color, new ImageIcon("sprites/" + color + "/bishop.png").getImage()); super(x, y, color, new ImageIcon("sprites/" + color + "/bishop.png").getImage());
hasMoved = false; }
public Piece copy() {
Piece newP = new Bishop(this.pos.x, this.pos.y, this.color);
return newP;
}
public ArrayList<Position> getPseudoLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>();
positions.addAll(slide(board, -1, -1));
positions.addAll(slide(board, 1, -1));
positions.addAll(slide(board, -1, 1));
positions.addAll(slide(board, 1, 1));
return positions;
} }
public ArrayList<Position> getLegalMoves(Board board) { public ArrayList<Position> getLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>(); return null;
// 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

@@ -13,6 +13,10 @@ public class Board {
} }
} }
public Board(boolean isCopy) {
board = new Piece[8][8];
}
public void draw(Graphics g) { public void draw(Graphics g) {
for (Piece[] row : board) { for (Piece[] row : board) {
for (Piece p : row) { for (Piece p : row) {
@@ -32,4 +36,22 @@ public class Board {
public Piece getPiece(Position pos) { public Piece getPiece(Position pos) {
return this.board[pos.x][pos.y]; return this.board[pos.x][pos.y];
} }
public Piece getPiece(int x, int y) {
return this.board[x][y];
}
public static Board copy(Board b) {
Board newBoard = new Board();
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 7; j++) {
Piece p = b.getPiece(i, j);
if (p == null) continue;
newBoard.board[i][j] = p.copy();
}
}
return newBoard;
}
} }

BIN
King.class Normal file

Binary file not shown.

55
King.java Normal file
View File

@@ -0,0 +1,55 @@
import java.util.*;
import javax.swing.ImageIcon;
public class King extends Piece {
boolean hasMoved;
static int[] xDir = {-1, 0, 1, -1, 1, -1, 0, 1};
static int[] yDir = {-1, -1, -1, 0, 0, 1, 1, 1};
public King(int x, int y, String color) {
super(x, y, color, new ImageIcon("sprites/" + color + "/king.png").getImage());
hasMoved = false;
}
public Piece copy() {
Piece newP = new King(this.pos.x, this.pos.y, this.color);
return newP;
}
public ArrayList<Position> getPseudoLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>();
for (int i = 0; i < xDir.length; i++) {
Position test = new Position(pos.x + xDir[i], pos.y + yDir[i]);
if (!Board.inBounds(test)) continue;
if (board.isOpen(test)) {
positions.add(test);
continue;
}
if (!board.getPiece(test).colorMatches(this)) {
positions.add(test);
}
}
return positions;
}
public ArrayList<Position> getLegalMoves(Board board) {
return null;
}
public boolean inCheck(Board board) {
for (Piece[] pieces : board.board) {
for (Piece p : pieces) {
if (p == null) continue;
if (!p.colorMatches(this) && !(p instanceof King)) {
ArrayList<Position> ar = p.getLegalMoves(board);
for (Position posi : ar) {
if (this.pos.equals(posi)) return true;
}
}
}
}
return false;
}
}

Binary file not shown.

View File

@@ -9,13 +9,16 @@ public class Knight extends Piece {
super(x, y, color, new ImageIcon("sprites/" + color + "/knight.png").getImage()); super(x, y, color, new ImageIcon("sprites/" + color + "/knight.png").getImage());
} }
public ArrayList<Position> getLegalMoves(Board board) { public Piece copy() {
Piece newP = new Knight(this.pos.x, this.pos.y, this.color);
return newP;
}
public ArrayList<Position> getPseudoLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>(); ArrayList<Position> positions = new ArrayList<Position>();
for (int i = 0; i < xDir.length; i++) { for (int i = 0; i < xDir.length; i++) {
int xPlus = xDir[i]; Position test = new Position(pos.x + xDir[i], pos.y + yDir[i]);
int yPlus = yDir[i];
Position test = new Position(pos.x + xPlus, pos.y + yPlus);
if (!Board.inBounds(test)) continue; if (!Board.inBounds(test)) continue;
if (board.isOpen(test)) { if (board.isOpen(test)) {
positions.add(test); positions.add(test);
@@ -28,4 +31,8 @@ public class Knight extends Piece {
return positions; return positions;
} }
public ArrayList<Position> getLegalMoves(Board board) {
return null;
}
} }

Binary file not shown.

View File

@@ -11,7 +11,12 @@ public class Pawn extends Piece {
colorDir = color.equals("White") ? -1 : 1; colorDir = color.equals("White") ? -1 : 1;
} }
public ArrayList<Position> getLegalMoves(Board board) { public Piece copy() {
Piece newP = new Pawn(this.pos.x, this.pos.y, this.color);
return newP;
}
public ArrayList<Position> getPseudoLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>(); ArrayList<Position> positions = new ArrayList<Position>();
// diagonal moves (captures) // diagonal moves (captures)
@@ -38,4 +43,8 @@ public class Pawn extends Piece {
} }
return positions; return positions;
} }
public ArrayList<Position> getLegalMoves(Board board) {
return null;
}
} }

Binary file not shown.

View File

@@ -18,7 +18,30 @@ public abstract class Piece {
public abstract ArrayList<Position> getLegalMoves(Board board); public abstract ArrayList<Position> getLegalMoves(Board board);
public abstract ArrayList<Position> getPseudoLegalMoves(Board board);
public abstract Piece copy();
public boolean colorMatches(Piece p) { public boolean colorMatches(Piece p) {
return this.color.equals(p.color); return this.color.equals(p.color);
} }
public ArrayList<Position> slide(Board board, int dx, int dy) {
ArrayList<Position> positions = new ArrayList<>();
int step = 1;
while (true) {
Position test = new Position(pos.x + step * dx, pos.y + step * dy);
if (!Board.inBounds(test)) break;
if (board.isOpen(test)) {
positions.add(test);
} else if (board.getPiece(test).colorMatches(this)) {
break;
} else {
positions.add(test);
break;
}
step++;
}
return positions;
}
} }

BIN
Queen.class Normal file

Binary file not shown.

33
Queen.java Normal file
View File

@@ -0,0 +1,33 @@
import java.util.*;
import javax.swing.ImageIcon;
public class Queen extends Piece {
public Queen(int x, int y, String color) {
super(x, y, color, new ImageIcon("sprites/" + color + "/queen.png").getImage());
}
public Piece copy() {
Piece newP = new Queen(this.pos.x, this.pos.y, this.color);
return newP;
}
public ArrayList<Position> getPseudoLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>();
positions.addAll(slide(board, -1, -1));
positions.addAll(slide(board, 1, -1));
positions.addAll(slide(board, -1, 1));
positions.addAll(slide(board, 1, 1));
positions.addAll(slide(board, 1, 0));
positions.addAll(slide(board, -1, 0));
positions.addAll(slide(board, 0, 1));
positions.addAll(slide(board, 0, -1));
return positions;
}
public ArrayList<Position> getLegalMoves(Board board) {
return null;
}
}

Binary file not shown.

View File

@@ -9,64 +9,23 @@ public class Rook extends Piece {
hasMoved = false; hasMoved = false;
} }
public ArrayList<Position> getLegalMoves(Board board) { public Piece copy() {
Piece newP = new Rook(this.pos.x, this.pos.y, this.color);
return newP;
}
public ArrayList<Position> getPseudoLegalMoves(Board board) {
ArrayList<Position> positions = new ArrayList<Position>(); ArrayList<Position> positions = new ArrayList<Position>();
// check left positions.addAll(slide(board, 1, 0));
for (int i = pos.x - 1; i >= 0; i--) { positions.addAll(slide(board, -1, 0));
Position test = new Position(i, pos.y); positions.addAll(slide(board, 0, 1));
if (board.isOpen(test)) { positions.addAll(slide(board, 0, -1));
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; return positions;
} }
public ArrayList<Position> getLegalMoves(Board board) {
return null;
}
} }