Hey guys, I'm writing Chess Program for fun. I hit a road block. I wondering if guys can see what is wrong with it. Here is the basic code.
public class Piece { /** * The Piece's Row. */ private int row; /** * The Piece's Column. */ private int column; /** * The Piece's Color. */ private final boolean isWhite; /** * Piece's Constructor * @param row The Piece's Row. * @param column The Piece's Column. * @param isWhite The Piece's Color. */ public Piece(int row, int column, final boolean isWhite){ this.row = row; this.column = column; this.isWhite = isWhite; } /** * @return The Piece's Row. */ public int getRow(){ return this.row; } /** * @return The Piece's Column. */ public int getColumn(){ return this.column; } /** * @return true == isWhite. Otherwise false. */ public boolean isWhite(){ return this.isWhite; } /** * The Piece moving to a square. * @param row The Square's Row. * @param column The Square's Column. * @return true when the move is successful. Otherwise false. */ public boolean moveTo(int row, int column){ return false; } @Override public String toString(){ if(this.isWhite){ return "w"; } return "b"; } }
public interface Rook { /** * The Rook moving to a square. * @param row The Square's Row. * @param column The Square's Column. * @return true when the move is successful. Otherwise false. */ public boolean moveTo(int row, int column); /** * The movement of the Rook. */ public void moveLikeARook(); }
public interface Bishop { /** * The Bishop moving to a square. * @param row The Square's Row. * @param column The Square's Column. * @return true when the move is successful. Otherwise false. */ public boolean moveTo(int row, int column); /** * The movement of the Bishop. */ public void moveLikeABishop(); }
public class Queen extends Piece implements Bishop, Rook{ /** * Queen's Constructor. * @param row The Queen's Row. * @param column The Queen's Column. * @param isWhite The Queen's Cololr. */ public Queen(int row, int column, boolean isWhite) { super(row, column, isWhite); } @Override public void moveLikeARook() { System.out.println("Move Like A Rook"); } @Override public void moveLikeABishop() { System.out.println("Move Like A Bishop"); } @Override public boolean moveTo(int row, int column) { System.out.println("moveTo"); if(this instanceof Bishop){ System.out.println("It's a Bishop!"); }else if(this instanceof Rook){ System.out.println("It's a Rook!"); }else if(this instanceof Queen){ System.out.println("It's a Queen!"); } return false; } public String toString(){ if(this instanceof Bishop){ return super.toString() + "B"; }else if(this instanceof Rook){ return super.toString() + "R"; } return super.toString() + "Q"; } }
Now this is my issue. For this class:
/** * * @author Anthony Wong * */ public final class Chess { protected static Piece[][] chessBoard = new Piece[7][7]; public static void main(String[] args) { Bishop bishop1 = new Queen(1,2,true); chessBoard[0][0] = (Piece) bishop1; Piece piece = chessBoard[0][0]; piece.moveTo(1, 1); Rook rook1 = new Queen(1, 2, true); chessBoard[1][1] = (Piece)rook1; Piece piece2 = chessBoard[1][1]; piece2.moveTo(1, 2); } }
When I run the code, this is my output:
moveTo
It's a Bishop!
moveTo
It's a Bishop!
How come this is my output? (i.e. line: piece.moveTo(1,1); && piece2.moveTo(1,2) )