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 5 of 5

Thread: Chess Problem

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Chess Problem

    Hi, im pretty new to programming, and I am struggling with my current project.

    This class is supposed to imitate a Queen piece from a chess board. I take its position (int row, int column ) and I must make an isValid method that will return whether or not the piece can move from its current position to a new given position that is brought into the isValid method through its parameters. What I have so far covers the rows and columns, but I can not figure out how to validate the diagonals.

    Here is the code:

    public class Queen {
    // declare needed variables
    private int queenRow;
    private int queenColumn;
    private String name = "Queen";
    private boolean valid;

    public Queen(int queenRow, int queenColumn, String name) {

    this.queenRow = queenRow;
    this.queenColumn = queenColumn;
    this.name = name;

    }

    // method to set the Queen's position
    public void setQueenPos(int queenRow, int queenColumn) {
    this.queenRow = queenRow;
    this.queenColumn = queenColumn;
    }

    public void moveQueen(int i, int j) {

    if (valid == true) {
    this.queenRow = i;
    this.queenColumn = j;
    } else {
    System.out.println("The queen cannot move there.");
    }

    }

    // method to validate the piece's move
    public boolean isValid(int row, int column) {

    if (row == this.queenRow && column <= 8 && column > 0) {
    valid = true;
    } else if (column == this.queenColumn && row <= 8 && row > 0) {
    valid = true;
    }

    return valid;
    }

    // method to return the Queen's position
    public int getQueenRow() {
    return queenRow;

    }

    public int getQueenColumn() {
    return queenColumn;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public static void main(String[] args) {
    Queen obj = new Queen(1, 1, "Queen");

    System.out.println(obj.getQueenRow() + " " + obj.getQueenColumn());
    obj.isValid(1, 5);
    obj.moveQueen(1, 5);
    System.out.println(obj.getQueenRow() + " " + obj.getQueenColumn());
    }

    }


  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Chess Problem

    Basically, to get to a diagonal from a square, you have to move up by a certain number, then across by the same number

    You need to think about the relation between the row and column value of the square its on and the row and column values of the diagonals. So if the queen is on row 4 column 6:
    (4,6) then some of the diagonals are:
    (5,5)
    (7,3)
    (6,8)
    The difference between the row of the diagonal and the starting square and the difference between the column of the diagonal and the starting square are the same.

  3. #3
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Chess Problem

    Also, can you put [ highlight=Java] and [ /highlight] tags round your code, makes it easier to read, cheers

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Chess Problem

    Also, purely a structure design suggestion:

    When making something like this, it can be assumed 1) you will have an interface of sorts to display each piece and 2) you will want to fire events for all of the pieces on either side to communicate with each other in some way (I'm not explaining this well, but try and follow me anyway).

    For that reason, this is an example as to when Inheritance is useful. I would have made a Piece class, that contained the variables and methods that would be shared among all the pieces, such as it's alliance (which side the piece is associated to), its ability to move, ect. I would have then created a class for each Piece (such as Queen) that would inherit from the Piece class.

    For example, looking at your code above, it would be logical to assume that each Piece (whether it is a Queen, Bishop, ect.) would share the following methods and variables:
    Variables:
    1) int Row
    2) int Column
    3) String Name
    4) boolean valid
    5) int alliance (where 0 is black and 1 is white, or a boolean can be used here)
    Methods:
    1) setPosition(...)
    2) move(...)
    3) isValid(...)
    4) getRow()
    5) getColumn()
    6) getName()
    7) setName(...)

    You can then set up some sort of structure where each Piece will have a list of possible moves it can do (along rows, along columns, diagonally, ect.). It can then use that list to create another list of possible locations that it can go (possibly by using recursion to move and test locations on the board). Lastly, you can create a few methods in the Piece class, where each method is a different type of movement that finds the list of locations. Then in each inherited class it will call the inherited methods for each movement and create a list that way.

    It was sort of loosely described, but it would be a good design that would use a significant amount of inheritance. If you're a beginner to JAVA programming, it is very important to learn how inheritance can be your friend and this would be a good exercise to get experience using inheritance.

    Just a though, if you are comfortable with the way you have it now, don't try to muck it up by redesigning everything.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Problem

    Thanks a lot, that is all very helpful and I will be sure to put the highlight around it next time.

Similar Threads

  1. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM