Compare commits
8 Commits
a2b0c23b42
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f6aaa63a5f | |||
| bb85a44235 | |||
| 3e96095c66 | |||
| c3318f834c | |||
| 984c37a833 | |||
| ce61482b9e | |||
| 6e6c222d57 | |||
| d834371ca3 |
29
Bishop.java
Normal file
29
Bishop.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Bishop extends Piece {
|
||||
|
||||
public Bishop(int x, int y, String color) {
|
||||
super(x, y, color, new ImageIcon("sprites/" + color + "/bishop.png").getImage());
|
||||
}
|
||||
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
BIN
Board.class
BIN
Board.class
Binary file not shown.
40
Board.java
40
Board.java
@@ -6,13 +6,17 @@ public class Board {
|
||||
public Board() {
|
||||
board = new Piece[8][8];
|
||||
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++) {
|
||||
board[i][7] = new Pawn(i + 1, 7, "White");
|
||||
board[i][6] = new Pawn(i, 6, "White");
|
||||
}
|
||||
}
|
||||
|
||||
public Board(boolean isCopy) {
|
||||
board = new Piece[8][8];
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
for (Piece[] row : board) {
|
||||
for (Piece p : row) {
|
||||
@@ -26,6 +30,36 @@ public class Board {
|
||||
}
|
||||
|
||||
public boolean isOpen(Position pos) {
|
||||
return this.board[pos.x][pos.y] == null;
|
||||
return this.getPiece(pos) == null;
|
||||
}
|
||||
|
||||
public Piece getPiece(Position pos) {
|
||||
return this.board[pos.x][pos.y];
|
||||
}
|
||||
|
||||
public Piece getPiece(int x, int y) {
|
||||
return this.board[x][y];
|
||||
}
|
||||
|
||||
public void setPiece(int x, int y, Piece p) {
|
||||
this.board[x][y] = p;
|
||||
}
|
||||
|
||||
public void setPiece(Position pos, Piece p) {
|
||||
this.board[pos.x][pos.y] = p;
|
||||
}
|
||||
|
||||
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
Chess.class
BIN
Chess.class
Binary file not shown.
BIN
Display.class
BIN
Display.class
Binary file not shown.
66
King.java
Normal file
66
King.java
Normal file
@@ -0,0 +1,66 @@
|
||||
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) {
|
||||
ArrayList<Position> positions = getPseudoLegalMoves(board);
|
||||
for (Position p : new ArrayList<>(positions)) {
|
||||
Piece tempPiece = board.getPiece(p);
|
||||
board.setPiece(pos, null);
|
||||
board.setPiece(p, this);
|
||||
if (inCheck(board, p)) {
|
||||
positions.remove(p);
|
||||
}
|
||||
board.setPiece(p, tempPiece);
|
||||
board.setPiece(pos, this);
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
public boolean inCheck(Board board, Position pos) {
|
||||
for (Piece[] pieces : board.board) {
|
||||
for (Piece p : pieces) {
|
||||
if (p == null) continue;
|
||||
if (!p.colorMatches(this) && !(p instanceof King)) {
|
||||
ArrayList<Position> ar = p.getPseudoLegalMoves(board);
|
||||
for (Position posi : ar) {
|
||||
if (pos.equals(posi)) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
BIN
Knight.class
BIN
Knight.class
Binary file not shown.
24
Knight.java
24
Knight.java
@@ -9,16 +9,30 @@ public class Knight extends Piece {
|
||||
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>();
|
||||
|
||||
for (int i : xDir) {
|
||||
for (int j : yDir) {
|
||||
Position test = new Position(pos.x + i, pos.y + j);
|
||||
if (!Board.inBounds(test)) continue;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Pawn.class
BIN
Pawn.class
Binary file not shown.
38
Pawn.java
38
Pawn.java
@@ -11,30 +11,40 @@ public class Pawn extends Piece {
|
||||
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>();
|
||||
|
||||
// diagonal moves (captures)
|
||||
if (Board.inBounds(new Position(pos.x + 1, pos.y + colorDir))
|
||||
&& !board.isOpen(new Position(pos.x + 1, pos.y + colorDir))
|
||||
&& !board.board[pos.x + 1][pos.y + colorDir].color.equals(this.color)) {
|
||||
positions.add(new Position(pos.x + 1, pos.y + colorDir));
|
||||
Position test = new Position(pos.x + 1, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
if (Board.inBounds(new Position(pos.x - 1, pos.y + colorDir))
|
||||
&& !board.isOpen(new Position(pos.x - 1, pos.y + colorDir))
|
||||
&& !board.board[pos.x - 1][pos.y + colorDir].color.equals(this.color)) {
|
||||
positions.add(new Position(pos.x - 1, pos.y + colorDir));
|
||||
|
||||
test = new Position(pos.x - 1, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
|
||||
// one square in front: if blocked return early
|
||||
if (Board.inBounds(new Position(pos.x, pos.y + colorDir))
|
||||
&& board.isOpen(new Position(pos.x, pos.y + colorDir))) {
|
||||
positions.add(new Position(pos.x, pos.y + colorDir));
|
||||
test = new Position(pos.x, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
} else return positions;
|
||||
|
||||
// two squares in front
|
||||
if (!hasMoved && board.isOpen(new Position(pos.x, pos.y + 2 * colorDir))) {
|
||||
positions.add(new Position(pos.x, pos.y + 2 * colorDir));
|
||||
test = new Position(pos.x, pos.y + 2 * colorDir);
|
||||
if (!hasMoved && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Piece.class
BIN
Piece.class
Binary file not shown.
33
Piece.java
33
Piece.java
@@ -1,8 +1,7 @@
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Piece {
|
||||
ArrayList<Integer> legalMoves;
|
||||
public abstract class Piece {
|
||||
Position pos;
|
||||
Image sprite;
|
||||
String color;
|
||||
@@ -17,8 +16,32 @@ public class Piece {
|
||||
g.drawImage(sprite, pos.x * 40, pos.y * 40, null);
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
System.out.println("parent method not overriden");
|
||||
return null;
|
||||
public abstract ArrayList<Position> getLegalMoves(Board board);
|
||||
|
||||
public abstract ArrayList<Position> getPseudoLegalMoves(Board board);
|
||||
|
||||
public abstract Piece copy();
|
||||
|
||||
public boolean colorMatches(Piece p) {
|
||||
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
Position.class
BIN
Position.class
Binary file not shown.
33
Queen.java
Normal file
33
Queen.java
Normal 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;
|
||||
}
|
||||
}
|
||||
31
Rook.java
Normal file
31
Rook.java
Normal file
@@ -0,0 +1,31 @@
|
||||
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 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>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user