Skip to content

Instantly share code, notes, and snippets.

@jquast
Created May 27, 2021 17:08
Show Gist options
  • Save jquast/238f52db95f4439ee693ed8a70b3beb5 to your computer and use it in GitHub Desktop.
Save jquast/238f52db95f4439ee693ed8a70b3beb5 to your computer and use it in GitHub Desktop.
ChessPiece.java
public class ChessPiece {
String kind;
Position pos;
ChessPiece next;
ChessPiece(String kind, int xpos, int ypos)
{
this.kind = kind;
this.pos = new Position(xpos, ypos);
next = null;
}
public String toString(){
String str_color = isWhite() ? "White" : "Black";
return kind + "(" + pos + ", " + str_color + ")";
}
public boolean isWhite(){
// piece is "White" if kind string lowercase
return kind.toLowerCase().equals(kind);
}
public boolean isBlack(){
return !isWhite();
}
public boolean isMoveLegal(Position dest) {
// Firstly, a move must *move*, if the destination position
// is equal to our current position, this is illegal.
if (dest.x == pos.x && dest.y == pos.y) {
System.out.println("Chess piece must move, illegal.");
return false;
}
if (kind.equalsIgnoreCase("r") || kind.equalsIgnoreCase("q")) {
// Queen or Rook:
// determine horizontal or vertical move. This can be determined if either
// the dest_x is equal to our current xpos, or dest_y is equal to our
// current ypos, as we can always assume that the other coordinate has
// changed given the guard at the top of our method.
if (dest.x == pos.x || dest.y == pos.y) {
System.out.println("Move is horizontal or vertical, legal.");
return true;
}
}
if (kind.equalsIgnoreCase("b") || kind.equalsIgnoreCase("q")) {
// Bishop or Queen:
// Determine if we move in any 45 degree angles. We can already safely
// assume that either dest_x or dest_y is different than our given, so,
// as long as the absolute delta between (dest_x, x) is equal to
// the absolute delta of (dest_y, y), then a diagonal move has
// occurred.
if (Math.abs(dest.x - pos.x) == Math.abs(dest.y - pos.y)) {
System.out.println("Move is diagonal, legal.");
return true;
}
}
if (kind.equalsIgnoreCase("k")) {
// King:
// if the delta between (x,y)->(x,y) is less than or equal to 1,
// then the king has moved in any of its 8 legal positions
if (Math.abs(pos.y - dest.y) <= 1 &&
Math.abs(pos.x - dest.x) <= 1) {
System.out.println("Move is fit for King, legal.");
return true;
}else{
System.out.println("Move too big for King, illegal.");
return false;
}
}
if (kind.equalsIgnoreCase("n")) {
// Knight:
// Determine if the delta difference of (x,y) is (1,2) or (2,1), which
// tells us whether it has performed any of its legal L-shaped maneuvers.
int delta_x = Math.abs(dest.x - pos.x);
int delta_y = Math.abs(dest.y - pos.y);
if ((delta_x == 1 && delta_y == 2) || (delta_x == 2 && delta_y == 1)) {
System.out.println("Knights move is legal.");
return true;
}
}
if (kind.equalsIgnoreCase("p")) {
// Pawn: TODO request clarification of board layout for starting positions!
//
// White may only move upward, black downward, and only 1 position, unless
// either piece is at their starting position, then they move 2 positions.
// I can't tell whether (0, 0) is bottom-left or top-right, and whether
// white begins on the bottom, or the top, so its hard to discern that.0w
}
System.out.println("This move is not legal. ");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment