Compare commits
2 Commits
main
...
a2b0c23b42
| Author | SHA1 | Date | |
|---|---|---|---|
| a2b0c23b42 | |||
| 4b222fabf7 |
1
.gitignore
vendored
@@ -1 +0,0 @@
|
||||
*.class
|
||||
29
Bishop.java
@@ -1,29 +0,0 @@
|
||||
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, 7));
|
||||
positions.addAll(slide(board, 1, -1, 7));
|
||||
positions.addAll(slide(board, -1, 1, 7));
|
||||
positions.addAll(slide(board, 1, 1, 7));
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return getPseudoLegalMoves(board);
|
||||
}
|
||||
}
|
||||
BIN
Board.class
Normal file
90
Board.java
@@ -6,56 +6,10 @@ public class Board {
|
||||
public Board() {
|
||||
board = new Piece[8][8];
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
board[i][1] = new Pawn(i, 1, "Black");
|
||||
board[i][6] = new Pawn(i, 6, "White");
|
||||
board[i][2] = new Pawn(i + 1, 2, "Black");
|
||||
}
|
||||
for (int i = 0; i <= 7; i += 7) {
|
||||
String color = i == 0 ? "Black" : "White";
|
||||
board[0][i] = new Rook(0, i, color);
|
||||
board[7][i] = new Rook(7, i, color);
|
||||
board[1][i] = new Knight(1, i, color);
|
||||
board[6][i] = new Knight(6, i, color);
|
||||
board[2][i] = new Bishop(2, i, color);
|
||||
board[5][i] = new Bishop(5, i, color);
|
||||
board[4][i] = new King(4, i, color);
|
||||
board[3][i] = new Queen(3, i, color);
|
||||
}
|
||||
}
|
||||
|
||||
public Board(boolean isCopy) {
|
||||
board = new Piece[8][8];
|
||||
}
|
||||
|
||||
public void capture(Piece capturing, Piece captured) {
|
||||
board[capturing.pos.x][capturing.pos.y] = null;
|
||||
board[captured.pos.x][captured.pos.y] = capturing;
|
||||
capturing.pos.x = captured.pos.x;
|
||||
capturing.pos.y = captured.pos.y;
|
||||
}
|
||||
|
||||
public void move(Piece p, Position posi) {
|
||||
board[p.pos.x][p.pos.y] = null;
|
||||
board[posi.x][posi.y] = p;
|
||||
p.pos = posi;
|
||||
if (p instanceof Pawn) {
|
||||
((Pawn) p).hasMoved = true;
|
||||
}
|
||||
if (p instanceof King) {
|
||||
((King) p).hasMoved = true;
|
||||
}
|
||||
if (p instanceof Rook) {
|
||||
((Rook) p).hasMoved = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void castle(King king, boolean kingSide) {
|
||||
int row = king.pos.y;
|
||||
if (kingSide) {
|
||||
move(king, new Position(6, row));
|
||||
move(board[7][row], new Position(5, row));
|
||||
} else {
|
||||
move(king, new Position(2, row));
|
||||
move(board[0][row], new Position(3, row));
|
||||
for (int i = 0; i <= 7; i++) {
|
||||
board[i][7] = new Pawn(i + 1, 7, "White");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,42 +26,6 @@ public class Board {
|
||||
}
|
||||
|
||||
public boolean isOpen(Position pos) {
|
||||
return this.getPiece(pos) == null;
|
||||
}
|
||||
|
||||
public Piece getPiece(Position pos) {
|
||||
if (inBounds(pos)) {
|
||||
return this.board[pos.x][pos.y];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Piece getPiece(int x, int y) {
|
||||
if (inBounds(new Position(x, y))) {
|
||||
return this.board[x][y];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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;
|
||||
return this.board[pos.x][pos.y] == null;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Chess.class
Normal file
65
Chess.java
@@ -1,23 +1,19 @@
|
||||
import java.awt.*;
|
||||
import java.awt.Color;
|
||||
import java.awt.event.*;
|
||||
import java.util.ArrayList;
|
||||
import javax.swing.*;
|
||||
|
||||
public class Chess extends JPanel implements ActionListener, MouseListener {
|
||||
public class Chess extends JPanel implements ActionListener {
|
||||
|
||||
// pieces stuff
|
||||
Board board;
|
||||
|
||||
// game vars
|
||||
int boardWidth, boardHeight;
|
||||
boolean whiteTurn;
|
||||
Timer gameTimer;
|
||||
Color creme = new Color(254, 245, 218);
|
||||
Color brown = new Color(121, 92, 50);
|
||||
Piece selected;
|
||||
ArrayList<Position> legalMoves;
|
||||
Image hover;
|
||||
String turnColor;
|
||||
|
||||
public Chess(int boardWidth, int boardHeight) {
|
||||
this.boardWidth = boardWidth;
|
||||
@@ -25,15 +21,10 @@ public class Chess extends JPanel implements ActionListener, MouseListener {
|
||||
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
|
||||
setBackground(Color.WHITE);
|
||||
setFocusable(true);
|
||||
addMouseListener(this);
|
||||
|
||||
board = new Board();
|
||||
selected = null;
|
||||
legalMoves = null;
|
||||
turnColor = "White";
|
||||
gameTimer = new Timer(200, this);
|
||||
|
||||
hover = new ImageIcon("sprites/hover.png").getImage();
|
||||
gameTimer = new Timer(200, this);
|
||||
|
||||
gameTimer.start();
|
||||
repaint();
|
||||
@@ -53,13 +44,6 @@ public class Chess extends JPanel implements ActionListener, MouseListener {
|
||||
|
||||
// draw pieces
|
||||
board.draw(g);
|
||||
|
||||
// draw legalMoves
|
||||
if (legalMoves != null) {
|
||||
for (Position p : legalMoves) {
|
||||
g.drawImage(hover, (p.x + 1) * 40, (p.y + 1) * 40, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
@@ -71,47 +55,4 @@ public class Chess extends JPanel implements ActionListener, MouseListener {
|
||||
gameLoop();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void mouseEntered(MouseEvent e) {}
|
||||
|
||||
public void mousePressed(MouseEvent e) {}
|
||||
|
||||
public void mouseReleased(MouseEvent e) {}
|
||||
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int col = (e.getX() / 40) - 1;
|
||||
int row = (e.getY() / 40) - 1;
|
||||
Position pos = new Position(col, row);
|
||||
Piece newSelected = board.getPiece(col, row);
|
||||
// if selecting one of our own pieces
|
||||
if (newSelected != null && newSelected.color.equals(turnColor)) {
|
||||
legalMoves = newSelected.getLegalMoves(board);
|
||||
selected = newSelected;
|
||||
return;
|
||||
}
|
||||
// if selecting an enemy piece -- must have one of ours selected
|
||||
else if (newSelected != null
|
||||
&& selected != null
|
||||
&& selected.color.equals(turnColor)
|
||||
&& !newSelected.color.equals(turnColor)
|
||||
&& legalMoves.contains(pos)) {
|
||||
board.capture(selected, newSelected);
|
||||
selected = null;
|
||||
legalMoves = null;
|
||||
turnColor = turnColor.equals("White") ? "Black" : "White";
|
||||
}
|
||||
// if selecting a blank square -- must have one of ours selected
|
||||
else if (selected != null && selected.color.equals(turnColor) && legalMoves.contains(pos)) {
|
||||
if (selected instanceof King && Math.abs(pos.x - selected.pos.x) == 2) {
|
||||
board.castle((King) selected, pos.x > selected.pos.x);
|
||||
} else {
|
||||
board.move(selected, pos);
|
||||
}
|
||||
selected = null;
|
||||
legalMoves = null;
|
||||
turnColor = turnColor.equals("White") ? "Black" : "White";
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseExited(MouseEvent e) {}
|
||||
}
|
||||
|
||||
BIN
Display.class
Normal file
90
King.java
@@ -1,90 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
int colorPos = this.color.equals("White") ? 7 : 0;
|
||||
boolean leftOpen = true;
|
||||
boolean rightOpen = true;
|
||||
if (!this.hasMoved && !inCheck(board,pos)){
|
||||
if(board.getPiece(7,colorPos) instanceof Rook && !Rook.class.cast(board.getPiece(7,colorPos)).hasMoved){
|
||||
ArrayList<Position> right = this.slide(board,1,0,2);
|
||||
|
||||
for (Position p : new ArrayList<>(right)){
|
||||
if (inCheck(board,p)){
|
||||
rightOpen = false;
|
||||
}
|
||||
}
|
||||
if (rightOpen) {positions.add(new Position(pos.x+2,pos.y));}
|
||||
}
|
||||
if (board.getPiece(0,colorPos) instanceof Rook && !Rook.class.cast(board.getPiece(0,colorPos)).hasMoved){
|
||||
|
||||
ArrayList<Position> left = this.slide(board,-1,0,2);
|
||||
for (Position p : new ArrayList<>(left)){
|
||||
if (inCheck(board,p)){
|
||||
leftOpen = false;
|
||||
}
|
||||
}
|
||||
if (leftOpen) positions.add(new Position(pos.x - 2, pos.y));
|
||||
}}
|
||||
|
||||
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
Normal file
22
Knight.java
@@ -9,30 +9,16 @@ public class Knight extends Piece {
|
||||
super(x, y, color, new ImageIcon("sprites/" + color + "/knight.png").getImage());
|
||||
}
|
||||
|
||||
public Piece copy() {
|
||||
Piece newP = new Knight(this.pos.x, this.pos.y, this.color);
|
||||
return newP;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getPseudoLegalMoves(Board board) {
|
||||
public ArrayList<Position> getLegalMoves(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]);
|
||||
for (int i : xDir) {
|
||||
for (int j : yDir) {
|
||||
Position test = new Position(pos.x + i, pos.y + j);
|
||||
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 getPseudoLegalMoves(board);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Pawn.class
Normal file
38
Pawn.java
@@ -11,40 +11,30 @@ public class Pawn extends Piece {
|
||||
colorDir = color.equals("White") ? -1 : 1;
|
||||
}
|
||||
|
||||
public Piece copy() {
|
||||
Piece newP = new Pawn(this.pos.x, this.pos.y, this.color);
|
||||
return newP;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getPseudoLegalMoves(Board board) {
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
|
||||
// diagonal moves (captures)
|
||||
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);
|
||||
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));
|
||||
}
|
||||
|
||||
// one square in front: if blocked return early
|
||||
test = new Position(pos.x, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
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));
|
||||
} else return positions;
|
||||
|
||||
// two squares in front
|
||||
test = new Position(pos.x, pos.y + 2 * colorDir);
|
||||
if (!hasMoved && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
if (!hasMoved && board.isOpen(new Position(pos.x, pos.y + 2 * colorDir))) {
|
||||
positions.add(new Position(pos.x, pos.y + 2 * colorDir));
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return getPseudoLegalMoves(board);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Piece.class
Normal file
36
Piece.java
@@ -1,8 +1,8 @@
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class Piece {
|
||||
public static int SIZE = 40;
|
||||
public class Piece {
|
||||
ArrayList<Integer> legalMoves;
|
||||
Position pos;
|
||||
Image sprite;
|
||||
String color;
|
||||
@@ -14,35 +14,11 @@ public abstract class Piece {
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
g.drawImage(sprite, (pos.x + 1) * 40, (pos.y + 1) * 40, null);
|
||||
g.drawImage(sprite, pos.x * 40, pos.y * 40, 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, int maxSteps) {
|
||||
ArrayList<Position> positions = new ArrayList<>();
|
||||
int step = 1;
|
||||
while (step < maxSteps) {
|
||||
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;
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
System.out.println("parent method not overriden");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Position.class
Normal file
@@ -5,9 +5,4 @@ public class Position {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
Position pos = (Position) o;
|
||||
return pos.x == this.x && pos.y == this.y;
|
||||
}
|
||||
}
|
||||
|
||||
33
Queen.java
@@ -1,33 +0,0 @@
|
||||
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, 7));
|
||||
positions.addAll(slide(board, 1, -1, 7));
|
||||
positions.addAll(slide(board, -1, 1, 7));
|
||||
positions.addAll(slide(board, 1, 1, 7));
|
||||
positions.addAll(slide(board, 1, 0, 7));
|
||||
positions.addAll(slide(board, -1, 0, 7));
|
||||
positions.addAll(slide(board, 0, 1, 7));
|
||||
positions.addAll(slide(board, 0, -1, 7));
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return getPseudoLegalMoves(board);
|
||||
}
|
||||
}
|
||||
31
Rook.java
@@ -1,31 +0,0 @@
|
||||
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, 7));
|
||||
positions.addAll(slide(board, -1, 0, 7));
|
||||
positions.addAll(slide(board, 0, 1, 7));
|
||||
positions.addAll(slide(board, 0, -1, 7));
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
return getPseudoLegalMoves(board);
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 869 B |
|
Before Width: | Height: | Size: 727 B |
|
Before Width: | Height: | Size: 592 B |
|
Before Width: | Height: | Size: 795 B |
|
Before Width: | Height: | Size: 724 B |
|
Before Width: | Height: | Size: 869 B |
|
Before Width: | Height: | Size: 727 B |
|
Before Width: | Height: | Size: 592 B |
|
Before Width: | Height: | Size: 795 B |
|
Before Width: | Height: | Size: 724 B |
|
Before Width: | Height: | Size: 299 B |