Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Chess

  1. #1
    Junior Member fireredlink5000's Avatar
    Join Date
    May 2014
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Chess

    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) )


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Chess

    There are 3 main points:
    1. bishop1 is assigned the reference to a Queen object, which is then assigned to piece; rook1 is also assigned the reference to a Queen object, which is then assigned to piece2
    2. Queen implements Bishop and Rook
    3. (Most important point) "The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface." (Source: Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics))

    Since Queen implements Bishop, and "instanceof Bishop" is the first test in the moveTo() method, this is why it prints "It's a Bishop!"

    This will become even clearer if you go through the example in the link above.

  3. The Following User Says Thank You to jashburn For This Useful Post:

    fireredlink5000 (May 22nd, 2014)

  4. #3
    Junior Member fireredlink5000's Avatar
    Join Date
    May 2014
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess

    Thanks a lot jashburn!

Similar Threads

  1. Chess Game
    By KTJW88 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 2nd, 2014, 09:45 AM
  2. If you had a chess-like boardgame....
    By integer9 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 2nd, 2014, 12:49 PM
  3. Chess Program
    By gokuball in forum Java Theory & Questions
    Replies: 218
    Last Post: August 19th, 2013, 04:47 PM
  4. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 08:42 PM
  5. Chess Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 12:07 PM