# Chess Problem

• February 9th, 2011, 05:50 PM
pmg
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());
}

}
• February 10th, 2011, 07:11 AM
samfin
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.
• February 10th, 2011, 07:15 AM
samfin
Re: Chess Problem
Also, can you put [ highlight=Java] and [ /highlight] tags round your code, makes it easier to read, cheers
• February 10th, 2011, 07:45 AM
aussiemcgr
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.
• February 10th, 2011, 11:07 AM
pmg
Re: Chess Problem
Thanks a lot, that is all very helpful and I will be sure to put the highlight around it next time.