C# program. Determine the correct move of the queen in chess
up vote
3
down vote
favorite
I am trying to write a small c# program that determines the correct move of a queen in chess. The coordinates of the initial and final position of the move are parsed by the string in the usual chess notation, for example, "a1". It seems to be easy, one just needs to add the isCorrectMove() method, but I don’t understand how we can convert the string to an integer without having a number and not even converting it to a number. That is, what the strings mean: var dx = Math.Abs (to [0] - from [0]); and so forth.
Here is an example code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
public static void Main()
{
TestMove("a1", "d4");
TestMove("f4", "e7");
TestMove("a1", "a4");
TestMove("a1", "a1");
}
public static void TestMove(string from, string to)
{
Console.WriteLine("{0}-{1} {2}", from, to, IsCorrectMove(from, to));
}
public static bool IsCorrectMove(string from, string to)
{
var dx = Math.Abs(to[0] - from[0]); //horizontal move
var dy = Math.Abs(to[1] - from[1]); //vertical move
return (dx == dy || dx == 0 || dy == 0 && from != to); // this move, however does not yield appropriate result for a1-a1 Queen's move as it must be a false move or "no move".
}
}
the condition described by the line return (dx == dy || dx == 0 || dy == 0 && from != to); does not yield proper result, as boolean check says that it is a correct move. I do not understand how to express this condition using logical operators in C#. Please suggest your solution or how to find it. In chess, the acceptable moves by a queen are the same either as for bishop or as for the rook but how to express "no move"?
c#
add a comment |
up vote
3
down vote
favorite
I am trying to write a small c# program that determines the correct move of a queen in chess. The coordinates of the initial and final position of the move are parsed by the string in the usual chess notation, for example, "a1". It seems to be easy, one just needs to add the isCorrectMove() method, but I don’t understand how we can convert the string to an integer without having a number and not even converting it to a number. That is, what the strings mean: var dx = Math.Abs (to [0] - from [0]); and so forth.
Here is an example code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
public static void Main()
{
TestMove("a1", "d4");
TestMove("f4", "e7");
TestMove("a1", "a4");
TestMove("a1", "a1");
}
public static void TestMove(string from, string to)
{
Console.WriteLine("{0}-{1} {2}", from, to, IsCorrectMove(from, to));
}
public static bool IsCorrectMove(string from, string to)
{
var dx = Math.Abs(to[0] - from[0]); //horizontal move
var dy = Math.Abs(to[1] - from[1]); //vertical move
return (dx == dy || dx == 0 || dy == 0 && from != to); // this move, however does not yield appropriate result for a1-a1 Queen's move as it must be a false move or "no move".
}
}
the condition described by the line return (dx == dy || dx == 0 || dy == 0 && from != to); does not yield proper result, as boolean check says that it is a correct move. I do not understand how to express this condition using logical operators in C#. Please suggest your solution or how to find it. In chess, the acceptable moves by a queen are the same either as for bishop or as for the rook but how to express "no move"?
c#
1
whoever is voting to close this as "too broad", could you explain? the bugfix may be trivial, but we have an mcve and helpful answers and an interesting aspect: applying implicit type conversion to chess position strings to find distances.
– dlatikay
Nov 11 at 16:49
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to write a small c# program that determines the correct move of a queen in chess. The coordinates of the initial and final position of the move are parsed by the string in the usual chess notation, for example, "a1". It seems to be easy, one just needs to add the isCorrectMove() method, but I don’t understand how we can convert the string to an integer without having a number and not even converting it to a number. That is, what the strings mean: var dx = Math.Abs (to [0] - from [0]); and so forth.
Here is an example code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
public static void Main()
{
TestMove("a1", "d4");
TestMove("f4", "e7");
TestMove("a1", "a4");
TestMove("a1", "a1");
}
public static void TestMove(string from, string to)
{
Console.WriteLine("{0}-{1} {2}", from, to, IsCorrectMove(from, to));
}
public static bool IsCorrectMove(string from, string to)
{
var dx = Math.Abs(to[0] - from[0]); //horizontal move
var dy = Math.Abs(to[1] - from[1]); //vertical move
return (dx == dy || dx == 0 || dy == 0 && from != to); // this move, however does not yield appropriate result for a1-a1 Queen's move as it must be a false move or "no move".
}
}
the condition described by the line return (dx == dy || dx == 0 || dy == 0 && from != to); does not yield proper result, as boolean check says that it is a correct move. I do not understand how to express this condition using logical operators in C#. Please suggest your solution or how to find it. In chess, the acceptable moves by a queen are the same either as for bishop or as for the rook but how to express "no move"?
c#
I am trying to write a small c# program that determines the correct move of a queen in chess. The coordinates of the initial and final position of the move are parsed by the string in the usual chess notation, for example, "a1". It seems to be easy, one just needs to add the isCorrectMove() method, but I don’t understand how we can convert the string to an integer without having a number and not even converting it to a number. That is, what the strings mean: var dx = Math.Abs (to [0] - from [0]); and so forth.
Here is an example code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
public static void Main()
{
TestMove("a1", "d4");
TestMove("f4", "e7");
TestMove("a1", "a4");
TestMove("a1", "a1");
}
public static void TestMove(string from, string to)
{
Console.WriteLine("{0}-{1} {2}", from, to, IsCorrectMove(from, to));
}
public static bool IsCorrectMove(string from, string to)
{
var dx = Math.Abs(to[0] - from[0]); //horizontal move
var dy = Math.Abs(to[1] - from[1]); //vertical move
return (dx == dy || dx == 0 || dy == 0 && from != to); // this move, however does not yield appropriate result for a1-a1 Queen's move as it must be a false move or "no move".
}
}
the condition described by the line return (dx == dy || dx == 0 || dy == 0 && from != to); does not yield proper result, as boolean check says that it is a correct move. I do not understand how to express this condition using logical operators in C#. Please suggest your solution or how to find it. In chess, the acceptable moves by a queen are the same either as for bishop or as for the rook but how to express "no move"?
c#
c#
edited Nov 11 at 17:12
sirandy
1,48331623
1,48331623
asked Nov 11 at 16:26
Shepenkov Valeriy
353
353
1
whoever is voting to close this as "too broad", could you explain? the bugfix may be trivial, but we have an mcve and helpful answers and an interesting aspect: applying implicit type conversion to chess position strings to find distances.
– dlatikay
Nov 11 at 16:49
add a comment |
1
whoever is voting to close this as "too broad", could you explain? the bugfix may be trivial, but we have an mcve and helpful answers and an interesting aspect: applying implicit type conversion to chess position strings to find distances.
– dlatikay
Nov 11 at 16:49
1
1
whoever is voting to close this as "too broad", could you explain? the bugfix may be trivial, but we have an mcve and helpful answers and an interesting aspect: applying implicit type conversion to chess position strings to find distances.
– dlatikay
Nov 11 at 16:49
whoever is voting to close this as "too broad", could you explain? the bugfix may be trivial, but we have an mcve and helpful answers and an interesting aspect: applying implicit type conversion to chess position strings to find distances.
– dlatikay
Nov 11 at 16:49
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
You need to add parentheses in your last line to make sure things evaluate correctly:
return (dx == dy || dx == 0 || dy == 0) && (from != to);
This makes sure that the function returns false whenever from == to
, and otherwise returns true when any of the three other conditions is true.
As for your question about why Math.Abs()
can work on a string, that's because when you index into a string (like from[0]
), what you're getting is not a string but a char, and a char has an int underneath it. You can test this by checking the value of something like Math.Abs('a')
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char for more info about the value of a char.
add a comment |
up vote
1
down vote
It is the correct move if letter stays the same (same row) and the second number changes (condition 1). from != to
is to make sure that it has actually moved
if number stays the same, and letter changes (condition 2)
if number of squares moved in x direction is equal to those moved in y direction (condition 3)
public static bool IsCorrectMove(string from, string to)
{
return ((from[0] == to[0]) || // (1)
(from[1] == to[1]) || // (2)
(Math.Abs((int)from[0] - (int)to[0])) == Math.Abs((int)from[1] - (int)to[1]))) && from != to); // (3)
}
Thank you very much! Your answer is correct, I just checked it in Visual Studio. I will wait for your comments. I appreciate your prompt response to this issue.
– Shepenkov Valeriy
Nov 11 at 16:42
@shepengauer You are very welcome. I didn't get what comments are you talking about? do you need any explanation?
– uɐʞɥsɐ
Nov 11 at 16:45
Thanks a lot!. I just saw you added the description.
– Shepenkov Valeriy
Nov 11 at 17:00
@shepengauer You are wlcome
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer please upvote and mark as answer if helped
– uɐʞɥsɐ
Nov 11 at 17:01
add a comment |
up vote
1
down vote
You can do it with the dx and dy variables alone, but there is a mistake in the translation from chess rules to program logic: the C# logical or operator ||
in the expression
dx == dy || dx == 0 || dy == 0 && from != to
will result in true as soon as one of its conditions, including dx == 0
alone is true. This is because of the language's operator precedence and order of evaluation rules.
Translated to chess rules, this would mean "valid if there is no movement in x direction" which is incorrect.
You need to use parentheses to evaluate the left part as a whole and then apply the second condition with logical and &&
:
(dx == dy || dx == 0 || dy == 0) && from != to
This corresponds nicely to the valid moves which are: any number of squares diagonally (dx equal dy, both signs), forward/back (no dx), and left/right (no dy) with the additional condition that you can not end up on the square you started from.
add a comment |
up vote
1
down vote
Queens can move either horizontally, diagonally or vertically any number of spaces. Therefore, we should start by examining these cases separately:
isValidHorizontal = dy == 0 && dx != 0
isValidVertical = dx == 0 && dy != 0
isValidDiagonal = dx == dy && dx != 0 && dy != 0
Note that I have made these conditionals exhaustive. So, if we were to put them all together, we would have:
isValid = (dy == 0 && dx != 0) || (dx == 0 && dy != 0) || (dx == dy && dx != 0 && dy != 0)
Now, let's declare some Boolean variables to simplify this:
A <- dx == 0
B <- dy == 0
C <- dx == dy
Our equation then becomes B(~A) + A(~B) + C(~A)(~B)
. We can then simplify to (B + C(~B))(~A) + A(~B)
. This then simplifies to (B + C)(~A) + A(~B)
. We can then move some terms around to make ~AB + ~BA + ~AC
. This then simplifies A ^ B + ~AC
.
Translating this, we then have:
isValid = (dx == 0 ^ dy == 0) || (dx == dy && dx > 0)
Note that (dx == 0 ^ dy == 0) || (dx == dy && dy > 0)
would also work. Also note that not-moving is not considered valid in this system (isValid
will be false).
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You need to add parentheses in your last line to make sure things evaluate correctly:
return (dx == dy || dx == 0 || dy == 0) && (from != to);
This makes sure that the function returns false whenever from == to
, and otherwise returns true when any of the three other conditions is true.
As for your question about why Math.Abs()
can work on a string, that's because when you index into a string (like from[0]
), what you're getting is not a string but a char, and a char has an int underneath it. You can test this by checking the value of something like Math.Abs('a')
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char for more info about the value of a char.
add a comment |
up vote
1
down vote
You need to add parentheses in your last line to make sure things evaluate correctly:
return (dx == dy || dx == 0 || dy == 0) && (from != to);
This makes sure that the function returns false whenever from == to
, and otherwise returns true when any of the three other conditions is true.
As for your question about why Math.Abs()
can work on a string, that's because when you index into a string (like from[0]
), what you're getting is not a string but a char, and a char has an int underneath it. You can test this by checking the value of something like Math.Abs('a')
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char for more info about the value of a char.
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to add parentheses in your last line to make sure things evaluate correctly:
return (dx == dy || dx == 0 || dy == 0) && (from != to);
This makes sure that the function returns false whenever from == to
, and otherwise returns true when any of the three other conditions is true.
As for your question about why Math.Abs()
can work on a string, that's because when you index into a string (like from[0]
), what you're getting is not a string but a char, and a char has an int underneath it. You can test this by checking the value of something like Math.Abs('a')
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char for more info about the value of a char.
You need to add parentheses in your last line to make sure things evaluate correctly:
return (dx == dy || dx == 0 || dy == 0) && (from != to);
This makes sure that the function returns false whenever from == to
, and otherwise returns true when any of the three other conditions is true.
As for your question about why Math.Abs()
can work on a string, that's because when you index into a string (like from[0]
), what you're getting is not a string but a char, and a char has an int underneath it. You can test this by checking the value of something like Math.Abs('a')
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/char for more info about the value of a char.
answered Nov 11 at 16:36
asherber
1,371810
1,371810
add a comment |
add a comment |
up vote
1
down vote
It is the correct move if letter stays the same (same row) and the second number changes (condition 1). from != to
is to make sure that it has actually moved
if number stays the same, and letter changes (condition 2)
if number of squares moved in x direction is equal to those moved in y direction (condition 3)
public static bool IsCorrectMove(string from, string to)
{
return ((from[0] == to[0]) || // (1)
(from[1] == to[1]) || // (2)
(Math.Abs((int)from[0] - (int)to[0])) == Math.Abs((int)from[1] - (int)to[1]))) && from != to); // (3)
}
Thank you very much! Your answer is correct, I just checked it in Visual Studio. I will wait for your comments. I appreciate your prompt response to this issue.
– Shepenkov Valeriy
Nov 11 at 16:42
@shepengauer You are very welcome. I didn't get what comments are you talking about? do you need any explanation?
– uɐʞɥsɐ
Nov 11 at 16:45
Thanks a lot!. I just saw you added the description.
– Shepenkov Valeriy
Nov 11 at 17:00
@shepengauer You are wlcome
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer please upvote and mark as answer if helped
– uɐʞɥsɐ
Nov 11 at 17:01
add a comment |
up vote
1
down vote
It is the correct move if letter stays the same (same row) and the second number changes (condition 1). from != to
is to make sure that it has actually moved
if number stays the same, and letter changes (condition 2)
if number of squares moved in x direction is equal to those moved in y direction (condition 3)
public static bool IsCorrectMove(string from, string to)
{
return ((from[0] == to[0]) || // (1)
(from[1] == to[1]) || // (2)
(Math.Abs((int)from[0] - (int)to[0])) == Math.Abs((int)from[1] - (int)to[1]))) && from != to); // (3)
}
Thank you very much! Your answer is correct, I just checked it in Visual Studio. I will wait for your comments. I appreciate your prompt response to this issue.
– Shepenkov Valeriy
Nov 11 at 16:42
@shepengauer You are very welcome. I didn't get what comments are you talking about? do you need any explanation?
– uɐʞɥsɐ
Nov 11 at 16:45
Thanks a lot!. I just saw you added the description.
– Shepenkov Valeriy
Nov 11 at 17:00
@shepengauer You are wlcome
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer please upvote and mark as answer if helped
– uɐʞɥsɐ
Nov 11 at 17:01
add a comment |
up vote
1
down vote
up vote
1
down vote
It is the correct move if letter stays the same (same row) and the second number changes (condition 1). from != to
is to make sure that it has actually moved
if number stays the same, and letter changes (condition 2)
if number of squares moved in x direction is equal to those moved in y direction (condition 3)
public static bool IsCorrectMove(string from, string to)
{
return ((from[0] == to[0]) || // (1)
(from[1] == to[1]) || // (2)
(Math.Abs((int)from[0] - (int)to[0])) == Math.Abs((int)from[1] - (int)to[1]))) && from != to); // (3)
}
It is the correct move if letter stays the same (same row) and the second number changes (condition 1). from != to
is to make sure that it has actually moved
if number stays the same, and letter changes (condition 2)
if number of squares moved in x direction is equal to those moved in y direction (condition 3)
public static bool IsCorrectMove(string from, string to)
{
return ((from[0] == to[0]) || // (1)
(from[1] == to[1]) || // (2)
(Math.Abs((int)from[0] - (int)to[0])) == Math.Abs((int)from[1] - (int)to[1]))) && from != to); // (3)
}
edited Nov 11 at 16:44
answered Nov 11 at 16:33
uɐʞɥsɐ
19.7k1565114
19.7k1565114
Thank you very much! Your answer is correct, I just checked it in Visual Studio. I will wait for your comments. I appreciate your prompt response to this issue.
– Shepenkov Valeriy
Nov 11 at 16:42
@shepengauer You are very welcome. I didn't get what comments are you talking about? do you need any explanation?
– uɐʞɥsɐ
Nov 11 at 16:45
Thanks a lot!. I just saw you added the description.
– Shepenkov Valeriy
Nov 11 at 17:00
@shepengauer You are wlcome
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer please upvote and mark as answer if helped
– uɐʞɥsɐ
Nov 11 at 17:01
add a comment |
Thank you very much! Your answer is correct, I just checked it in Visual Studio. I will wait for your comments. I appreciate your prompt response to this issue.
– Shepenkov Valeriy
Nov 11 at 16:42
@shepengauer You are very welcome. I didn't get what comments are you talking about? do you need any explanation?
– uɐʞɥsɐ
Nov 11 at 16:45
Thanks a lot!. I just saw you added the description.
– Shepenkov Valeriy
Nov 11 at 17:00
@shepengauer You are wlcome
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer please upvote and mark as answer if helped
– uɐʞɥsɐ
Nov 11 at 17:01
Thank you very much! Your answer is correct, I just checked it in Visual Studio. I will wait for your comments. I appreciate your prompt response to this issue.
– Shepenkov Valeriy
Nov 11 at 16:42
Thank you very much! Your answer is correct, I just checked it in Visual Studio. I will wait for your comments. I appreciate your prompt response to this issue.
– Shepenkov Valeriy
Nov 11 at 16:42
@shepengauer You are very welcome. I didn't get what comments are you talking about? do you need any explanation?
– uɐʞɥsɐ
Nov 11 at 16:45
@shepengauer You are very welcome. I didn't get what comments are you talking about? do you need any explanation?
– uɐʞɥsɐ
Nov 11 at 16:45
Thanks a lot!. I just saw you added the description.
– Shepenkov Valeriy
Nov 11 at 17:00
Thanks a lot!. I just saw you added the description.
– Shepenkov Valeriy
Nov 11 at 17:00
@shepengauer You are wlcome
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer You are wlcome
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer please upvote and mark as answer if helped
– uɐʞɥsɐ
Nov 11 at 17:01
@shepengauer please upvote and mark as answer if helped
– uɐʞɥsɐ
Nov 11 at 17:01
add a comment |
up vote
1
down vote
You can do it with the dx and dy variables alone, but there is a mistake in the translation from chess rules to program logic: the C# logical or operator ||
in the expression
dx == dy || dx == 0 || dy == 0 && from != to
will result in true as soon as one of its conditions, including dx == 0
alone is true. This is because of the language's operator precedence and order of evaluation rules.
Translated to chess rules, this would mean "valid if there is no movement in x direction" which is incorrect.
You need to use parentheses to evaluate the left part as a whole and then apply the second condition with logical and &&
:
(dx == dy || dx == 0 || dy == 0) && from != to
This corresponds nicely to the valid moves which are: any number of squares diagonally (dx equal dy, both signs), forward/back (no dx), and left/right (no dy) with the additional condition that you can not end up on the square you started from.
add a comment |
up vote
1
down vote
You can do it with the dx and dy variables alone, but there is a mistake in the translation from chess rules to program logic: the C# logical or operator ||
in the expression
dx == dy || dx == 0 || dy == 0 && from != to
will result in true as soon as one of its conditions, including dx == 0
alone is true. This is because of the language's operator precedence and order of evaluation rules.
Translated to chess rules, this would mean "valid if there is no movement in x direction" which is incorrect.
You need to use parentheses to evaluate the left part as a whole and then apply the second condition with logical and &&
:
(dx == dy || dx == 0 || dy == 0) && from != to
This corresponds nicely to the valid moves which are: any number of squares diagonally (dx equal dy, both signs), forward/back (no dx), and left/right (no dy) with the additional condition that you can not end up on the square you started from.
add a comment |
up vote
1
down vote
up vote
1
down vote
You can do it with the dx and dy variables alone, but there is a mistake in the translation from chess rules to program logic: the C# logical or operator ||
in the expression
dx == dy || dx == 0 || dy == 0 && from != to
will result in true as soon as one of its conditions, including dx == 0
alone is true. This is because of the language's operator precedence and order of evaluation rules.
Translated to chess rules, this would mean "valid if there is no movement in x direction" which is incorrect.
You need to use parentheses to evaluate the left part as a whole and then apply the second condition with logical and &&
:
(dx == dy || dx == 0 || dy == 0) && from != to
This corresponds nicely to the valid moves which are: any number of squares diagonally (dx equal dy, both signs), forward/back (no dx), and left/right (no dy) with the additional condition that you can not end up on the square you started from.
You can do it with the dx and dy variables alone, but there is a mistake in the translation from chess rules to program logic: the C# logical or operator ||
in the expression
dx == dy || dx == 0 || dy == 0 && from != to
will result in true as soon as one of its conditions, including dx == 0
alone is true. This is because of the language's operator precedence and order of evaluation rules.
Translated to chess rules, this would mean "valid if there is no movement in x direction" which is incorrect.
You need to use parentheses to evaluate the left part as a whole and then apply the second condition with logical and &&
:
(dx == dy || dx == 0 || dy == 0) && from != to
This corresponds nicely to the valid moves which are: any number of squares diagonally (dx equal dy, both signs), forward/back (no dx), and left/right (no dy) with the additional condition that you can not end up on the square you started from.
edited Nov 11 at 16:44
answered Nov 11 at 16:37
dlatikay
6,10011848
6,10011848
add a comment |
add a comment |
up vote
1
down vote
Queens can move either horizontally, diagonally or vertically any number of spaces. Therefore, we should start by examining these cases separately:
isValidHorizontal = dy == 0 && dx != 0
isValidVertical = dx == 0 && dy != 0
isValidDiagonal = dx == dy && dx != 0 && dy != 0
Note that I have made these conditionals exhaustive. So, if we were to put them all together, we would have:
isValid = (dy == 0 && dx != 0) || (dx == 0 && dy != 0) || (dx == dy && dx != 0 && dy != 0)
Now, let's declare some Boolean variables to simplify this:
A <- dx == 0
B <- dy == 0
C <- dx == dy
Our equation then becomes B(~A) + A(~B) + C(~A)(~B)
. We can then simplify to (B + C(~B))(~A) + A(~B)
. This then simplifies to (B + C)(~A) + A(~B)
. We can then move some terms around to make ~AB + ~BA + ~AC
. This then simplifies A ^ B + ~AC
.
Translating this, we then have:
isValid = (dx == 0 ^ dy == 0) || (dx == dy && dx > 0)
Note that (dx == 0 ^ dy == 0) || (dx == dy && dy > 0)
would also work. Also note that not-moving is not considered valid in this system (isValid
will be false).
add a comment |
up vote
1
down vote
Queens can move either horizontally, diagonally or vertically any number of spaces. Therefore, we should start by examining these cases separately:
isValidHorizontal = dy == 0 && dx != 0
isValidVertical = dx == 0 && dy != 0
isValidDiagonal = dx == dy && dx != 0 && dy != 0
Note that I have made these conditionals exhaustive. So, if we were to put them all together, we would have:
isValid = (dy == 0 && dx != 0) || (dx == 0 && dy != 0) || (dx == dy && dx != 0 && dy != 0)
Now, let's declare some Boolean variables to simplify this:
A <- dx == 0
B <- dy == 0
C <- dx == dy
Our equation then becomes B(~A) + A(~B) + C(~A)(~B)
. We can then simplify to (B + C(~B))(~A) + A(~B)
. This then simplifies to (B + C)(~A) + A(~B)
. We can then move some terms around to make ~AB + ~BA + ~AC
. This then simplifies A ^ B + ~AC
.
Translating this, we then have:
isValid = (dx == 0 ^ dy == 0) || (dx == dy && dx > 0)
Note that (dx == 0 ^ dy == 0) || (dx == dy && dy > 0)
would also work. Also note that not-moving is not considered valid in this system (isValid
will be false).
add a comment |
up vote
1
down vote
up vote
1
down vote
Queens can move either horizontally, diagonally or vertically any number of spaces. Therefore, we should start by examining these cases separately:
isValidHorizontal = dy == 0 && dx != 0
isValidVertical = dx == 0 && dy != 0
isValidDiagonal = dx == dy && dx != 0 && dy != 0
Note that I have made these conditionals exhaustive. So, if we were to put them all together, we would have:
isValid = (dy == 0 && dx != 0) || (dx == 0 && dy != 0) || (dx == dy && dx != 0 && dy != 0)
Now, let's declare some Boolean variables to simplify this:
A <- dx == 0
B <- dy == 0
C <- dx == dy
Our equation then becomes B(~A) + A(~B) + C(~A)(~B)
. We can then simplify to (B + C(~B))(~A) + A(~B)
. This then simplifies to (B + C)(~A) + A(~B)
. We can then move some terms around to make ~AB + ~BA + ~AC
. This then simplifies A ^ B + ~AC
.
Translating this, we then have:
isValid = (dx == 0 ^ dy == 0) || (dx == dy && dx > 0)
Note that (dx == 0 ^ dy == 0) || (dx == dy && dy > 0)
would also work. Also note that not-moving is not considered valid in this system (isValid
will be false).
Queens can move either horizontally, diagonally or vertically any number of spaces. Therefore, we should start by examining these cases separately:
isValidHorizontal = dy == 0 && dx != 0
isValidVertical = dx == 0 && dy != 0
isValidDiagonal = dx == dy && dx != 0 && dy != 0
Note that I have made these conditionals exhaustive. So, if we were to put them all together, we would have:
isValid = (dy == 0 && dx != 0) || (dx == 0 && dy != 0) || (dx == dy && dx != 0 && dy != 0)
Now, let's declare some Boolean variables to simplify this:
A <- dx == 0
B <- dy == 0
C <- dx == dy
Our equation then becomes B(~A) + A(~B) + C(~A)(~B)
. We can then simplify to (B + C(~B))(~A) + A(~B)
. This then simplifies to (B + C)(~A) + A(~B)
. We can then move some terms around to make ~AB + ~BA + ~AC
. This then simplifies A ^ B + ~AC
.
Translating this, we then have:
isValid = (dx == 0 ^ dy == 0) || (dx == dy && dx > 0)
Note that (dx == 0 ^ dy == 0) || (dx == dy && dy > 0)
would also work. Also note that not-moving is not considered valid in this system (isValid
will be false).
edited Nov 11 at 17:03
answered Nov 11 at 16:56
Woody1193
2,168830
2,168830
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250757%2fc-sharp-program-determine-the-correct-move-of-the-queen-in-chess%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
whoever is voting to close this as "too broad", could you explain? the bugfix may be trivial, but we have an mcve and helpful answers and an interesting aspect: applying implicit type conversion to chess position strings to find distances.
– dlatikay
Nov 11 at 16:49