Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

2D Arrays - Chess Queen Moves

807607Oct 10 2006 — edited Oct 10 2006
I'm making a class that uses a 2D array to move a queen around a chess board. The real challenge, I think, is to create a rule that determines which moves are illegal and which moves are legal. If statements are what I think would work.

My method accepts the coordinates of
- an element that will be moved (the Queen) -> from row and from column
- the square the Queen will be moved to -> to row and to column
public String move(int fr, int fc, int tr, int tc)
	{
		if ((fr < 0 && fr > 7) || (fc < 0 && fc > 7))
		{
			return "Invalid from coordinate";
		}
		if ((tr < 0 && tr > 7) || (tc < 0 && tc > 7))
		{
			return "Invalid to coordinate";
		}
		if (board[fr][fc] == SPACE)
		{
			return "Queen is not at from coordinate";
		}
		if (board[fr][fc] == board[tr][tc])
		{
			return "Cannot move to same square";
		}
	}
The last two things I want to accomplish are to
1) make a rule that returns an error if the queen's move is illigal
2) return a success message if the move is ok

Can anyone push me the right way?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 7 2006
Added on Oct 10 2006
13 comments
1,460 views