Results 1 to 6 of 6
  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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
    Last edited by Gatsu; 01-29-2013 at 03:22 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Chess, piece moving several steps

    Quote Originally Posted by Gatsu View Post
    "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.

  5. #5
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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
    Last edited by Gatsu; 01-29-2013 at 08:22 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Chess, piece moving several steps

    Quote Originally Posted by Gatsu View Post
    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.

    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.

Similar Threads

  1. problem selecting a chess piece
    By wil3789 in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 07:18 AM
  2. Chess Piece Painting Location on Board
    By 5myl in forum New To Java
    Replies: 4
    Last Post: 01-01-2012, 11:57 PM
  3. Tips for java exercise: print steps of n height
    By maniku in forum New To Java
    Replies: 3
    Last Post: 05-15-2011, 08:30 PM
  4. Java Chess piece Array
    By obious in forum New To Java
    Replies: 1
    Last Post: 05-04-2011, 01:20 PM
  5. Working out chess piece location / piece name
    By danborgir in forum New To Java
    Replies: 5
    Last Post: 04-20-2011, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •