How to convert array indices into possible moves for a rook and bishop chess piece
up vote
0
down vote
favorite
I'm struggling to work out how to convert index numbers in a 2D array into possible moves for a rook and bishop to move. I want to get a boolean expression to see if a move is legal. Sorry, I'm new to programming, I'd really appreciate some help
for the rook, I have
if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
java arrays chess
add a comment |
up vote
0
down vote
favorite
I'm struggling to work out how to convert index numbers in a 2D array into possible moves for a rook and bishop to move. I want to get a boolean expression to see if a move is legal. Sorry, I'm new to programming, I'd really appreciate some help
for the rook, I have
if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
java arrays chess
1
what have you tried so far?
– scigs
Nov 7 at 20:51
for the rook, if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
– sheenareid
Nov 7 at 20:53
OK, that is a logical statement and it looks like your on the right path in terms of logic. but that statement is not alone is a program. do you have a main class? how are you running it? What is the error you are seeing? what is it doing that its not supposed to do? please update the question with these details -- these details are crucial to get a good SO reply
– scigs
Nov 7 at 20:57
Yes, please show the code you have tried in your post itself (use the "edit" button) and explain how it fails so that we can help you with a specific issue. Currently, there's no question in your.. question
– YakovL
2 days ago
looks like you have an unnecessary.
after&&
; does your expression have any problems aside that?
– YakovL
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm struggling to work out how to convert index numbers in a 2D array into possible moves for a rook and bishop to move. I want to get a boolean expression to see if a move is legal. Sorry, I'm new to programming, I'd really appreciate some help
for the rook, I have
if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
java arrays chess
I'm struggling to work out how to convert index numbers in a 2D array into possible moves for a rook and bishop to move. I want to get a boolean expression to see if a move is legal. Sorry, I'm new to programming, I'd really appreciate some help
for the rook, I have
if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
java arrays chess
java arrays chess
edited 2 days ago
YakovL
2,738102237
2,738102237
asked Nov 7 at 20:47
sheenareid
32
32
1
what have you tried so far?
– scigs
Nov 7 at 20:51
for the rook, if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
– sheenareid
Nov 7 at 20:53
OK, that is a logical statement and it looks like your on the right path in terms of logic. but that statement is not alone is a program. do you have a main class? how are you running it? What is the error you are seeing? what is it doing that its not supposed to do? please update the question with these details -- these details are crucial to get a good SO reply
– scigs
Nov 7 at 20:57
Yes, please show the code you have tried in your post itself (use the "edit" button) and explain how it fails so that we can help you with a specific issue. Currently, there's no question in your.. question
– YakovL
2 days ago
looks like you have an unnecessary.
after&&
; does your expression have any problems aside that?
– YakovL
2 days ago
add a comment |
1
what have you tried so far?
– scigs
Nov 7 at 20:51
for the rook, if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
– sheenareid
Nov 7 at 20:53
OK, that is a logical statement and it looks like your on the right path in terms of logic. but that statement is not alone is a program. do you have a main class? how are you running it? What is the error you are seeing? what is it doing that its not supposed to do? please update the question with these details -- these details are crucial to get a good SO reply
– scigs
Nov 7 at 20:57
Yes, please show the code you have tried in your post itself (use the "edit" button) and explain how it fails so that we can help you with a specific issue. Currently, there's no question in your.. question
– YakovL
2 days ago
looks like you have an unnecessary.
after&&
; does your expression have any problems aside that?
– YakovL
2 days ago
1
1
what have you tried so far?
– scigs
Nov 7 at 20:51
what have you tried so far?
– scigs
Nov 7 at 20:51
for the rook, if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
– sheenareid
Nov 7 at 20:53
for the rook, if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
– sheenareid
Nov 7 at 20:53
OK, that is a logical statement and it looks like your on the right path in terms of logic. but that statement is not alone is a program. do you have a main class? how are you running it? What is the error you are seeing? what is it doing that its not supposed to do? please update the question with these details -- these details are crucial to get a good SO reply
– scigs
Nov 7 at 20:57
OK, that is a logical statement and it looks like your on the right path in terms of logic. but that statement is not alone is a program. do you have a main class? how are you running it? What is the error you are seeing? what is it doing that its not supposed to do? please update the question with these details -- these details are crucial to get a good SO reply
– scigs
Nov 7 at 20:57
Yes, please show the code you have tried in your post itself (use the "edit" button) and explain how it fails so that we can help you with a specific issue. Currently, there's no question in your.. question
– YakovL
2 days ago
Yes, please show the code you have tried in your post itself (use the "edit" button) and explain how it fails so that we can help you with a specific issue. Currently, there's no question in your.. question
– YakovL
2 days ago
looks like you have an unnecessary
.
after &&
; does your expression have any problems aside that?– YakovL
2 days ago
looks like you have an unnecessary
.
after &&
; does your expression have any problems aside that?– YakovL
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The rook moves horizontally and vertically. So either the x
coordinate changes with the y
remaining unchanged, or the y
coordinate changes with the x
coordinate remaining unchanged.
if ((xFrom != xTo) && (yFrom == yTo) || (xFrom == xTo) && (yFrom != yTo))
Alternatively, you can use an exclusive OR (XOR), which is essentially the same thing as above just more concise.
if ((xFrom != xTo) ^ (yFrom != yTo))
An XOR returns true if one of them is true, but not both.
Remember that the Rook also has a special Castling move with the King, which you will need to cater for depending on the state of the game (both the rook and king haven't moved, the king is not in check, etc.)
The bishop moves diagonally, so the difference between xFrom and xTo must be the same as the difference between yFrom and yTo, in both directions.
So this should do the trick for the bishop:
if (Math.abs(xTo - xFrom) == Math.abs(yTo - yFrom))
Of course there is much more to both the Rook and Bishop. You have to make sure that the path is clear up till the destination coordinate, you have to make sure that the destination coordinate is not occupied by a piece of the same player, and you have to check that the move does not put the king of the same player in check (which you have to do anyway for any piece move).
Castling is a king move, not a rook move, so it would most likely be handled separately from the logic of a rook move.
– jsheeran
2 days ago
@jsheeran Well implement it as you wish I guess. Just keep it in mind as another possibility.
– jbx
2 days ago
thank you so much!
– sheenareid
2 days ago
@sheenareid please accept the answer if it is correct
– jbx
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The rook moves horizontally and vertically. So either the x
coordinate changes with the y
remaining unchanged, or the y
coordinate changes with the x
coordinate remaining unchanged.
if ((xFrom != xTo) && (yFrom == yTo) || (xFrom == xTo) && (yFrom != yTo))
Alternatively, you can use an exclusive OR (XOR), which is essentially the same thing as above just more concise.
if ((xFrom != xTo) ^ (yFrom != yTo))
An XOR returns true if one of them is true, but not both.
Remember that the Rook also has a special Castling move with the King, which you will need to cater for depending on the state of the game (both the rook and king haven't moved, the king is not in check, etc.)
The bishop moves diagonally, so the difference between xFrom and xTo must be the same as the difference between yFrom and yTo, in both directions.
So this should do the trick for the bishop:
if (Math.abs(xTo - xFrom) == Math.abs(yTo - yFrom))
Of course there is much more to both the Rook and Bishop. You have to make sure that the path is clear up till the destination coordinate, you have to make sure that the destination coordinate is not occupied by a piece of the same player, and you have to check that the move does not put the king of the same player in check (which you have to do anyway for any piece move).
Castling is a king move, not a rook move, so it would most likely be handled separately from the logic of a rook move.
– jsheeran
2 days ago
@jsheeran Well implement it as you wish I guess. Just keep it in mind as another possibility.
– jbx
2 days ago
thank you so much!
– sheenareid
2 days ago
@sheenareid please accept the answer if it is correct
– jbx
2 days ago
add a comment |
up vote
1
down vote
accepted
The rook moves horizontally and vertically. So either the x
coordinate changes with the y
remaining unchanged, or the y
coordinate changes with the x
coordinate remaining unchanged.
if ((xFrom != xTo) && (yFrom == yTo) || (xFrom == xTo) && (yFrom != yTo))
Alternatively, you can use an exclusive OR (XOR), which is essentially the same thing as above just more concise.
if ((xFrom != xTo) ^ (yFrom != yTo))
An XOR returns true if one of them is true, but not both.
Remember that the Rook also has a special Castling move with the King, which you will need to cater for depending on the state of the game (both the rook and king haven't moved, the king is not in check, etc.)
The bishop moves diagonally, so the difference between xFrom and xTo must be the same as the difference between yFrom and yTo, in both directions.
So this should do the trick for the bishop:
if (Math.abs(xTo - xFrom) == Math.abs(yTo - yFrom))
Of course there is much more to both the Rook and Bishop. You have to make sure that the path is clear up till the destination coordinate, you have to make sure that the destination coordinate is not occupied by a piece of the same player, and you have to check that the move does not put the king of the same player in check (which you have to do anyway for any piece move).
Castling is a king move, not a rook move, so it would most likely be handled separately from the logic of a rook move.
– jsheeran
2 days ago
@jsheeran Well implement it as you wish I guess. Just keep it in mind as another possibility.
– jbx
2 days ago
thank you so much!
– sheenareid
2 days ago
@sheenareid please accept the answer if it is correct
– jbx
2 days ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The rook moves horizontally and vertically. So either the x
coordinate changes with the y
remaining unchanged, or the y
coordinate changes with the x
coordinate remaining unchanged.
if ((xFrom != xTo) && (yFrom == yTo) || (xFrom == xTo) && (yFrom != yTo))
Alternatively, you can use an exclusive OR (XOR), which is essentially the same thing as above just more concise.
if ((xFrom != xTo) ^ (yFrom != yTo))
An XOR returns true if one of them is true, but not both.
Remember that the Rook also has a special Castling move with the King, which you will need to cater for depending on the state of the game (both the rook and king haven't moved, the king is not in check, etc.)
The bishop moves diagonally, so the difference between xFrom and xTo must be the same as the difference between yFrom and yTo, in both directions.
So this should do the trick for the bishop:
if (Math.abs(xTo - xFrom) == Math.abs(yTo - yFrom))
Of course there is much more to both the Rook and Bishop. You have to make sure that the path is clear up till the destination coordinate, you have to make sure that the destination coordinate is not occupied by a piece of the same player, and you have to check that the move does not put the king of the same player in check (which you have to do anyway for any piece move).
The rook moves horizontally and vertically. So either the x
coordinate changes with the y
remaining unchanged, or the y
coordinate changes with the x
coordinate remaining unchanged.
if ((xFrom != xTo) && (yFrom == yTo) || (xFrom == xTo) && (yFrom != yTo))
Alternatively, you can use an exclusive OR (XOR), which is essentially the same thing as above just more concise.
if ((xFrom != xTo) ^ (yFrom != yTo))
An XOR returns true if one of them is true, but not both.
Remember that the Rook also has a special Castling move with the King, which you will need to cater for depending on the state of the game (both the rook and king haven't moved, the king is not in check, etc.)
The bishop moves diagonally, so the difference between xFrom and xTo must be the same as the difference between yFrom and yTo, in both directions.
So this should do the trick for the bishop:
if (Math.abs(xTo - xFrom) == Math.abs(yTo - yFrom))
Of course there is much more to both the Rook and Bishop. You have to make sure that the path is clear up till the destination coordinate, you have to make sure that the destination coordinate is not occupied by a piece of the same player, and you have to check that the move does not put the king of the same player in check (which you have to do anyway for any piece move).
edited 2 days ago
answered 2 days ago
jbx
9,8821056108
9,8821056108
Castling is a king move, not a rook move, so it would most likely be handled separately from the logic of a rook move.
– jsheeran
2 days ago
@jsheeran Well implement it as you wish I guess. Just keep it in mind as another possibility.
– jbx
2 days ago
thank you so much!
– sheenareid
2 days ago
@sheenareid please accept the answer if it is correct
– jbx
2 days ago
add a comment |
Castling is a king move, not a rook move, so it would most likely be handled separately from the logic of a rook move.
– jsheeran
2 days ago
@jsheeran Well implement it as you wish I guess. Just keep it in mind as another possibility.
– jbx
2 days ago
thank you so much!
– sheenareid
2 days ago
@sheenareid please accept the answer if it is correct
– jbx
2 days ago
Castling is a king move, not a rook move, so it would most likely be handled separately from the logic of a rook move.
– jsheeran
2 days ago
Castling is a king move, not a rook move, so it would most likely be handled separately from the logic of a rook move.
– jsheeran
2 days ago
@jsheeran Well implement it as you wish I guess. Just keep it in mind as another possibility.
– jbx
2 days ago
@jsheeran Well implement it as you wish I guess. Just keep it in mind as another possibility.
– jbx
2 days ago
thank you so much!
– sheenareid
2 days ago
thank you so much!
– sheenareid
2 days ago
@sheenareid please accept the answer if it is correct
– jbx
2 days ago
@sheenareid please accept the answer if it is correct
– jbx
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197568%2fhow-to-convert-array-indices-into-possible-moves-for-a-rook-and-bishop-chess-pie%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
what have you tried so far?
– scigs
Nov 7 at 20:51
for the rook, if(Math.abs(xTo-xFrom)>1 &&. Math.abs(yTo-yFrom)==0)
– sheenareid
Nov 7 at 20:53
OK, that is a logical statement and it looks like your on the right path in terms of logic. but that statement is not alone is a program. do you have a main class? how are you running it? What is the error you are seeing? what is it doing that its not supposed to do? please update the question with these details -- these details are crucial to get a good SO reply
– scigs
Nov 7 at 20:57
Yes, please show the code you have tried in your post itself (use the "edit" button) and explain how it fails so that we can help you with a specific issue. Currently, there's no question in your.. question
– YakovL
2 days ago
looks like you have an unnecessary
.
after&&
; does your expression have any problems aside that?– YakovL
2 days ago