
Imagine your player is a chess piece on a chessboard then look at the picture how every chess piece are moving and which field are valid fields for each piece, How ever your problem is more look like a Queen on a chess board because can move on every direction and you can adapt this algorithm to your player.
you need array to loop through every row and column.
Here is the code for how a Queen move on a chessboard.
public class Queen extends Chesspiece {
protected Queen(char color, char name) {
super(color, name);
}
@Override
public void markReachableFields() {
char rw = row;
byte col = column;
for (char i = 'a'; i <='h'; i++) {
rw = (char)i;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
col = column;
rw = row;
for (byte j = 0; j < 8; j++) {
col = (byte)j;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
rw = row;
col = column;
for (int i = 0; i < 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col + 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col + 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].mark();
}
}
}
@Override
public void unmarkReachableFields() {
char rw = row;
byte col = column;
for (char i = 'a'; i <='h'; i++) {
rw = (char)i;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
col = column;
rw = row;
for (byte j = 0; j < 8; j++) {
col = (byte)j;
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
rw = row;
col = column;
for (int i = 0; i < 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col + 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col + 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw + 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
rw = row;
col = column;
for (int i = 1; i <= 8; i++) {
rw = (char)(rw - 1);
col = (byte)(col - 1);
if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
&&Chessboard.this.isValidField(rw, col)){
int r = rw - FIRST_ROW;
int c = col - FIRST_COLUMN;
Chessboard.this.fields[r][c].unmark();
}
}
}
}
destX
anddestY
,playerX
andplayer
, andplayer.x
andplayer.y
? What do they represent? Why do you hardcode0.5
, what is this value representing? – Vaillancourt Mar 23 '17 at 02:11playerX < destX
andplayerY < destY
then the player will move positively in both the x and y axes (ie. northeast). Have I misunderstood your code or the question you're asking? – DMGregory Mar 23 '17 at 02:56I updated the post with the improved code, and also a representative image.
– Lucas Mar 23 '17 at 03:03