diff --git a/Bishop.java b/Bishop.java index c102748..53db7fa 100644 --- a/Bishop.java +++ b/Bishop.java @@ -24,6 +24,6 @@ public class Bishop extends Piece { } public ArrayList getLegalMoves(Board board) { - return null; + return getPseudoLegalMoves(board); } } diff --git a/Board.java b/Board.java index ec7d376..9ecbfad 100644 --- a/Board.java +++ b/Board.java @@ -26,6 +26,28 @@ public class Board { 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 draw(Graphics g) { for (Piece[] row : board) { for (Piece p : row) { @@ -43,11 +65,17 @@ public class Board { } public Piece getPiece(Position pos) { - return this.board[pos.x][pos.y]; + if (inBounds(pos)) { + return this.board[pos.x][pos.y]; + } + return null; } public Piece getPiece(int x, int y) { - return this.board[x][y]; + if (inBounds(new Position(x, y))) { + return this.board[x][y]; + } + return null; } public void setPiece(int x, int y, Piece p) { diff --git a/Chess.java b/Chess.java index 8e5218e..9756be1 100644 --- a/Chess.java +++ b/Chess.java @@ -1,9 +1,10 @@ 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 { +public class Chess extends JPanel implements ActionListener, MouseListener { // pieces stuff Board board; @@ -14,6 +15,10 @@ public class Chess extends JPanel implements ActionListener { Timer gameTimer; Color creme = new Color(254, 245, 218); Color brown = new Color(121, 92, 50); + Piece selected; + ArrayList legalMoves; + Image hover; + String turnColor; public Chess(int boardWidth, int boardHeight) { this.boardWidth = boardWidth; @@ -21,11 +26,16 @@ public class Chess extends JPanel implements ActionListener { 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.start(); repaint(); } @@ -44,6 +54,13 @@ public class Chess extends JPanel implements ActionListener { // 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) { @@ -55,4 +72,44 @@ public class Chess extends JPanel implements ActionListener { 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)) { + + board.move(selected, pos); + selected = null; + legalMoves = null; + turnColor = turnColor.equals("White") ? "Black" : "White"; + } + } + + public void mouseExited(MouseEvent e) {} } diff --git a/Knight.java b/Knight.java index d093531..0a4b7a9 100644 --- a/Knight.java +++ b/Knight.java @@ -33,6 +33,6 @@ public class Knight extends Piece { } public ArrayList getLegalMoves(Board board) { - return null; + return getPseudoLegalMoves(board); } } diff --git a/Pawn.java b/Pawn.java index 7d73ff5..7af2e8c 100644 --- a/Pawn.java +++ b/Pawn.java @@ -45,6 +45,6 @@ public class Pawn extends Piece { } public ArrayList getLegalMoves(Board board) { - return null; + return getPseudoLegalMoves(board); } } diff --git a/Piece.java b/Piece.java index dca1fe2..2a8d348 100644 --- a/Piece.java +++ b/Piece.java @@ -2,6 +2,7 @@ import java.awt.*; import java.util.*; public abstract class Piece { + public static int SIZE = 40; Position pos; Image sprite; String color; diff --git a/Position.java b/Position.java index 9edf1cd..39131f9 100644 --- a/Position.java +++ b/Position.java @@ -5,4 +5,9 @@ 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; + } } diff --git a/Queen.java b/Queen.java index fef89eb..a542972 100644 --- a/Queen.java +++ b/Queen.java @@ -28,6 +28,6 @@ public class Queen extends Piece { } public ArrayList getLegalMoves(Board board) { - return null; + return getPseudoLegalMoves(board); } } diff --git a/Rook.java b/Rook.java index 5c00914..b1053ac 100644 --- a/Rook.java +++ b/Rook.java @@ -26,6 +26,6 @@ public class Rook extends Piece { } public ArrayList getLegalMoves(Board board) { - return null; + return getPseudoLegalMoves(board); } } diff --git a/Sprites/hover.png b/Sprites/hover.png new file mode 100644 index 0000000..8e7ab52 Binary files /dev/null and b/Sprites/hover.png differ