Java Chess, piece moving several steps

• 01-29-2013, 04:11 PM
Gatsu
Java Chess, piece moving several steps
Hi, I am tryign to make a chess game in Java but I am stuck at figuring out how to check what squares my queen travels on to see if all squares are empty, since it can not jump over pieces.

My method to move a piece looks like this:

public void movePiece(int[] newPos, int[] oldPos)
{
//stuff to check if position is ok to step on
}

example of what the arguments looks like:
oldPos[0] = 0
oldPos[1] = 0

newPos[0] = 7
newPos[1] = 7

This is a queen traveling across the whole board, so how can I make some loop or something to check all positions in between?

like, 1,1 2,2 3,3 ... 7,7
• 01-29-2013, 04:30 PM
Fubarable
Re: Java Chess, piece moving several steps
This is a specific case of a more general problem, that of determining if a move is legal. I would imagine that the Queen class would descend from the abstract ChessPiece class which would have an abstract method, moveLegal(Position newPosition) which would return a boolean determining if a proposed move were legal. There have been books written on how to write efficient chess engines, none of which I've read, and so if it were me, as a first approximation, I'd just brute force it, perhaps giving each Piece class a method that would find every possible legal move, perhaps even using recursion, putting them in a Set<Position> and then check to see if the parameter is held by the Set. The Queen class could even perhaps hold private Bishop and Rook fields and call the methods of these fields to see if the move were legal and to help reduce redundant code.
• 01-29-2013, 08:07 PM
Gatsu
Re: Java Chess, piece moving several steps
"every possible legal move", one step at the time and then repeating it untill it hits the destination? that sounds wrong, what do you mean?
• 01-29-2013, 08:46 PM
Fubarable
Re: Java Chess, piece moving several steps
Quote:

Originally Posted by Gatsu
"every possible legal move", one step at the time and then repeating it untill it hits the destination? that sounds wrong, what do you mean?

I meant to create a set of all possible legal moves as your first step, and then after that has been complete, checking to see if your move is contained in that set.
• 01-29-2013, 09:13 PM
Gatsu
Re: Java Chess, piece moving several steps
how can that give me the positions between point A and B specifically so I can check if they are ok to step on.

I'm thinking now that maybe I should check if point A x and y values is more or less then point B x and y values and make loops that +1 or -1 or +0 for x and y each step and checking if that position is valid untill it gets to point B x y
• 01-29-2013, 09:17 PM
Fubarable
Re: Java Chess, piece moving several steps
Quote:

Originally Posted by Gatsu
how can that give me the positions between point A and B specifically so I can check if they are ok to step on.

You need to isolate the problem for the Bishop and the Rook first. You should be able to figure out recursive and non-recursive algorithms for these if you work it through. We could write something like this for you, but I'm sure if you apply yourself, you could do it, and would gain much more in the effort. Then when you've solved this, you can use both methods for the Queen.

Quote:

I'm thinking now that maybe I should check if point A x and y values is more or less then point B x and y values and make loops that +1 or -1 or +0 for x and y each step and checking if that position is valid.
Try it and see what happens.