Added coordinate-based move validation

+ Simple input handler for testing purposes in BoardPanel
+ Abstract isValidMove method in Piece
+ Coordinate-based implementations of isValidMove in every subclass of
Piece
This commit is contained in:
Kai S. K. Engelbart 2019-07-01 21:46:30 +02:00
parent 3aa54cea82
commit bae5618f59
9 changed files with 68 additions and 2 deletions

View File

@ -24,6 +24,15 @@ public class Board {
initializeDefaultPositions(); 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. * 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; } public Piece[][] getBoardArr() { return boardArr; }
} }

View File

@ -5,8 +5,11 @@ import java.awt.Container;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image; import java.awt.Image;
import java.awt.Point;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -57,6 +60,23 @@ public class BoardPanel extends JPanel {
if (textures == null) loadPieceTextures(); 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 @Override

View File

@ -12,6 +12,11 @@ public class Bishop extends Piece {
super(color); super(color);
} }
@Override
public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) {
return Math.abs(xDest - xPos) == Math.abs(yDest - yPos);
}
@Override @Override
public Type getType() { return Type.BISHOP; } public Type getType() { return Type.BISHOP; }
} }

View File

@ -12,6 +12,11 @@ public class King extends Piece {
super(color); 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 @Override
public Type getType() { return Type.KING; } public Type getType() { return Type.KING; }
} }

View File

@ -12,6 +12,13 @@ public class Knight extends Piece {
super(color); 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 @Override
public Type getType() { return Type.KNIGHT; } public Type getType() { return Type.KNIGHT; }
} }

View File

@ -12,6 +12,14 @@ public class Pawn extends Piece {
super(color); 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 @Override
public Type getType() { return Type.PAWN; } public Type getType() { return Type.PAWN; }
} }

View File

@ -8,12 +8,14 @@ package dev.kske.chess.piece;
*/ */
public abstract class Piece { public abstract class Piece {
private Color color; protected Color color;
public Piece(Color color) { public Piece(Color color) {
this.color = color; this.color = color;
} }
public abstract boolean isValidMove(int xPos, int yPos, int xDest, int yDest);
public abstract Type getType(); public abstract Type getType();
public Color getColor() { return color; } public Color getColor() { return color; }

View File

@ -12,6 +12,11 @@ public class Queen extends Piece {
super(color); 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 @Override
public Type getType() { return Type.QUEEN; } public Type getType() { return Type.QUEEN; }
} }

View File

@ -12,6 +12,11 @@ public class Rook extends Piece {
super(color); super(color);
} }
@Override
public boolean isValidMove(int xPos, int yPos, int xDest, int yDest) {
return xPos == xDest || yPos == yDest;
}
@Override @Override
public Type getType() { return Type.ROOK; } public Type getType() { return Type.ROOK; }
} }