Results 1 to 6 of 6
- 01-29-2013, 03:11 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
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,7Last edited by Gatsu; 01-29-2013 at 03:22 PM.
-
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, 07:07 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
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?
Last edited by Gatsu; 01-29-2013 at 07:13 PM.
-
Re: Java Chess, piece moving several steps
- 01-29-2013, 08:13 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 36
- Rep Power
- 0
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 yLast edited by Gatsu; 01-29-2013 at 08:22 PM.
-
Re: Java Chess, piece moving several steps
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.
Try it and see what happens.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.
Similar Threads
-
problem selecting a chess piece
By wil3789 in forum New To JavaReplies: 3Last Post: 01-05-2012, 07:18 AM -
Chess Piece Painting Location on Board
By 5myl in forum New To JavaReplies: 4Last Post: 01-01-2012, 11:57 PM -
Tips for java exercise: print steps of n height
By maniku in forum New To JavaReplies: 3Last Post: 05-15-2011, 08:30 PM -
Java Chess piece Array
By obious in forum New To JavaReplies: 1Last Post: 05-04-2011, 01:20 PM -
Working out chess piece location / piece name
By danborgir in forum New To JavaReplies: 5Last Post: 04-20-2011, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks