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) 









share|improve this question



















  • 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














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) 









share|improve this question



















  • 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












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) 









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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).






share|improve this answer






















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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).






share|improve this answer






















  • 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














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).






share|improve this answer






















  • 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












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).






share|improve this answer














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).







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế