diff --git a/src/dev/kske/chess/Board.java b/src/dev/kske/chess/Board.java index da918d4..b6f80f7 100644 --- a/src/dev/kske/chess/Board.java +++ b/src/dev/kske/chess/Board.java @@ -24,6 +24,15 @@ public class Board { initializeDefaultPositions(); } + public boolean move(int xPos, int yPos, int xDest, int yDest) { + if (boardArr[xPos][yPos] == null || !boardArr[xPos][yPos].isValidMove(xPos, yPos, xDest, yDest)) return false; + else { + boardArr[xDest][yDest] = boardArr[xPos][yPos]; + boardArr[xPos][yPos] = null; + return true; + } + } + /** * Initialized the board array with the default chess pieces and positions. */ @@ -62,7 +71,7 @@ public class Board { } /** - * @return the board array + * @return The board array */ public Piece[][] getBoardArr() { return boardArr; } } diff --git a/src/dev/kske/chess/BoardPanel.java b/src/dev/kske/chess/BoardPanel.java index 57c9a40..163d5c5 100644 --- a/src/dev/kske/chess/BoardPanel.java +++ b/src/dev/kske/chess/BoardPanel.java @@ -5,8 +5,11 @@ import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; +import java.awt.Point; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.io.File; import java.util.HashMap; import java.util.Map; @@ -57,6 +60,23 @@ public class BoardPanel extends JPanel { if (textures == null) loadPieceTextures(); } }); + + // Add a mouse adapter for testing piece movement + addMouseListener(new MouseAdapter() { + + private Point src; + + @Override + public void mousePressed(MouseEvent evt) { + if (src == null) src = evt.getPoint(); + else { + Point dest = evt.getPoint(); + board.move(src.x / tileSize, src.y / tileSize, dest.x / tileSize, dest.y / tileSize); + repaint(); + src = null; + } + } + }); } @Override diff --git a/src/dev/kske/chess/piece/Bishop.java b/src/dev/kske/chess/piece/Bishop.java index 11ccf33..30ba42b 100644 --- a/src/dev/kske/chess/piece/Bishop.java +++ b/src/dev/kske/chess/piece/Bishop.java @@ -12,6 +12,11 @@ public class Bishop extends Piece { super(color); } + @Override + public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) { + return Math.abs(xDest - xPos) == Math.abs(yDest - yPos); + } + @Override public Type getType() { return Type.BISHOP; } } diff --git a/src/dev/kske/chess/piece/King.java b/src/dev/kske/chess/piece/King.java index d1b2f4c..d0c9c89 100644 --- a/src/dev/kske/chess/piece/King.java +++ b/src/dev/kske/chess/piece/King.java @@ -12,6 +12,11 @@ public class King extends Piece { super(color); } + @Override + public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) { + return Math.abs(xDest - xPos) <= 1 && Math.abs(yDest - yPos) <= 1; + } + @Override public Type getType() { return Type.KING; } } diff --git a/src/dev/kske/chess/piece/Knight.java b/src/dev/kske/chess/piece/Knight.java index a713880..6e65fbb 100644 --- a/src/dev/kske/chess/piece/Knight.java +++ b/src/dev/kske/chess/piece/Knight.java @@ -12,6 +12,13 @@ public class Knight extends Piece { super(color); } + @Override + public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) { + int xDist = Math.abs(xDest - xPos); + int yDist = Math.abs(yDest - yPos); + return Math.abs(xDist - yDist) == 1 && xDist != 0 && yDist != 0; + } + @Override public Type getType() { return Type.KNIGHT; } } diff --git a/src/dev/kske/chess/piece/Pawn.java b/src/dev/kske/chess/piece/Pawn.java index 8d546e0..608d551 100644 --- a/src/dev/kske/chess/piece/Pawn.java +++ b/src/dev/kske/chess/piece/Pawn.java @@ -12,6 +12,14 @@ public class Pawn extends Piece { super(color); } + @Override + public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) { + int direction = color == Color.WHITE ? -1 : 1; + int xDist = Math.abs(xDest - xPos); + int yDist = (yDest - yPos) * direction; + return (xDist == 0 && yDist > 0 && yDist <= 2) || (xDist == 1 && yDist == 1); + } + @Override public Type getType() { return Type.PAWN; } } diff --git a/src/dev/kske/chess/piece/Piece.java b/src/dev/kske/chess/piece/Piece.java index cbd00a2..ebfae3a 100644 --- a/src/dev/kske/chess/piece/Piece.java +++ b/src/dev/kske/chess/piece/Piece.java @@ -8,12 +8,14 @@ package dev.kske.chess.piece; */ public abstract class Piece { - private Color color; + protected Color color; public Piece(Color color) { this.color = color; } + public abstract boolean isValidMove(int xPos, int yPos, int xDest, int yDest); + public abstract Type getType(); public Color getColor() { return color; } diff --git a/src/dev/kske/chess/piece/Queen.java b/src/dev/kske/chess/piece/Queen.java index 647896e..d215ba0 100644 --- a/src/dev/kske/chess/piece/Queen.java +++ b/src/dev/kske/chess/piece/Queen.java @@ -12,6 +12,11 @@ public class Queen extends Piece { super(color); } + @Override + public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) { + return (xPos == xDest || yPos == yDest) || (Math.abs(xDest - xPos) == Math.abs(yDest - yPos)); + } + @Override public Type getType() { return Type.QUEEN; } } diff --git a/src/dev/kske/chess/piece/Rook.java b/src/dev/kske/chess/piece/Rook.java index 8f176a7..7beb93b 100644 --- a/src/dev/kske/chess/piece/Rook.java +++ b/src/dev/kske/chess/piece/Rook.java @@ -12,6 +12,11 @@ public class Rook extends Piece { super(color); } + @Override + public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) { + return xPos == xDest || yPos == yDest; + } + @Override public Type getType() { return Type.ROOK; } }