Skip to content

Instantly share code, notes, and snippets.

@jbass86
Last active October 13, 2022 17:22
Show Gist options
  • Save jbass86/d1daac336c8c667298f7b4cf412648e4 to your computer and use it in GitHub Desktop.
Save jbass86/d1daac336c8c667298f7b4cf412648e4 to your computer and use it in GitHub Desktop.
//I just kind of threw all this together but these things would be in separate files.
public enum Color {
WHITE("w"),
BLACK("b")
private Color(final String pAbbr) {
abbr = pAbbr
}
public String getAbbr() {
return abbr;
}
public String toString() {
return abbr;
}
private String abbr;
}
public enum EType {
PAWN("Pawn", "P", 1),
KNIGHT("Knight", "Ne", 3),
BISHOP("Bishop", "B", 3),
ROOK("Rook", "R", 5),
QUEEN("Queen", "Q", 9),
KING("King", "K", -1);
private EType(final String pName, final String pAbbr, final int pValue) {
mName = pName;
mAbbr = pAbbr;
mValue = pValue;
}
public String getName() {
return mName;
}
public String getAbbr() {
return mAbbr;
}
public int getValue() {
return mValue;
}
private final String mName;
private final String mAbbr;
private final int mValue;
}
public interface IPiece {
public EType getType();
public Color getColor();
public String getLocation();
public String setLocation();
/**
* I think you probably want to just give the whole board to the piece when you attempt to find out if a move is possible or not, youll need to most likely walk
* path and such to find out if something is in your way etc..
*
* Would return true or false based on if that move were possible or not.
**/
public boolean attemptMove(final String pLocation, final Board pBoard);
// Might not be needed but it would be easy to track and could be useful if ever tried to implement special move i.e. Castling
public int getMoveCount();
}
/////////////////////
static final int aCode = (int)'a';
// remember that your board array is zero based but the rank is 1 based
static final int OneCode = (int)'1';
//Assuming the location is in 2 letter format one char and one number for rank and file e.g. a3
//this will return java.awt.Point which has an x and a y member
public Point convertLocation(final String pLocation) {
Point ret = null;
if (pLocation == null || pLocation.length() != 2) {
//This isnt a valid location
System.err.println("Invalid point provided...");
} else {
// I made this case insensitive here so like either a3 or A3 would both work but I dont think that was a requirement of your project.
int rank = (int) pLocation.toLowerCase().charAt(0) - aCode;
int file = (int) pLocation.charAt(1) - OneCode;
if (isWithinBoard(rank) && isWithinBoard(file)) {
ret = new Point(rank, file);
}
}
return ret;
}
public boolean isWithinBoard(final int p) {
if (p > 7 || p < 0) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment