Closed Starlight-30036225 closed 7 months ago
I have created the horizontal movement for the straight interface, but I am not very happy with the code. I am sure there is a way to simplify this and bring it together into one function, but for now this will do:
`List
for (int i = piece.x + 1; i < 8; i++) { //To the right of the board
Piece temp = board[i][piece.y];
if (temp != null && temp.white == piece.white) {
break; //The piece here is the same colour, cant move.
}
moves.add((i) + "" + piece.y); //can move here
if (temp != null) {
break; //opposite colour, take
}
}
for (int i = piece.x - 1; i > -1; i--) { //To the left of the board
Piece temp = board[i][piece.y];
if (temp != null && temp.white == piece.white) {
break; //The piece here is the same colour, cant move.
}
moves.add((i) + "" + piece.y); //can move here, its an empty space
if (temp != null) {
break; //The piece here can be taken, but must stop here.
}
}
return moves;`
I will just repeat the same with Y for vertical movement
Diagonal is an even bigger mess, I will NEED to come back to tidy this up later if for nothing else but my pride. But it works.
Diagonals work. (Somehow)
`List
Piece temp = board[piece.x + i][piece.y + i];
if (temp != null && temp.white == piece.white) {
break; //The piece here is the same colour, cant move.
}
moves.add((piece.x + i) + "" + (piece.y + i)); //can move here
if (temp != null) {
break; //opposite colour, take
}
}
for (int i = 1; i < 8; i++) { //up x , down Y
if(piece.x + i > 7 || piece.y - i < 0) {break;} //Feels very illegal, gatekeeping
Piece temp = board[piece.x + i][piece.y - i];
if (temp != null && temp.white == piece.white) {
break; //The piece here is the same colour, cant move.
}
moves.add((piece.x + i) + "" + (piece.y - i)); //can move here, its an empty space
if (temp != null) {
break; //The piece here can be taken, but must stop here.
}
}
}`
The same is repeated for moving left.
This must be able to be simplified. I am going to make another piece with this messy method, then find a way to simplify them both in one method.
Through my work here: #13 , I have made it so these functions can be greatly simplified. Through the use of the validate move function, i can check if a space is free or take-able easier
This is my new idea, It uses the validate move function to test if the space is empty or is taking a piece, then uses a secondary check to test if a piece is being taken, this should work?
I will use it for all diagonal, then report back.
Works perfectly, Could probably be tidied up even more
This is a little nicer and easier to read, now the calculation is run once per loop instead of twice. Which means nothing but looks better.
It has been simplified even further to thus:
I think calling the internal function Validate move to reflect the function inside the piece may be a bit confusing as they are not truly related, one just calls the other. So I Should really rename it slightly.
Tho now saying this, as i moved it to the new function and cut it down, It can probably just be moved back into the while condition as its only a little bit longer than whats currently in there, saving on a whole confucing function
I have chosen to keep it, as moving it back makes the x and y variables slightly messy, And now renamed it to 'CheckEndOfPath'
Here is a bishop using the diagonal interface:
Here is a rook using the straight interface:
And here is a queen using both:
These interfaces will be used by the rook and bishop respectively, then combined to make the queens movement.
I have started work on the straight interface.