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"?










share|improve this question




















  • 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















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"?










share|improve this question




















  • 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













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"?










share|improve this question















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#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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.






share|improve this answer




























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





    share|improve this answer























    • 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


















    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.






    share|improve this answer






























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






      share|improve this answer























        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%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

























        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.






        share|improve this answer

























          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.






          share|improve this answer























            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 16:36









            asherber

            1,371810




            1,371810
























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





                share|improve this answer























                • 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















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





                share|improve this answer























                • 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













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





                share|improve this answer














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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                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


















                • 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










                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.






                share|improve this answer



























                  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.






                  share|improve this answer

























                    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.






                    share|improve this answer














                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 11 at 16:44

























                    answered Nov 11 at 16:37









                    dlatikay

                    6,10011848




                    6,10011848






















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






                        share|improve this answer



























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






                          share|improve this answer

























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






                            share|improve this answer














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







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 11 at 17:03

























                            answered Nov 11 at 16:56









                            Woody1193

                            2,168830




                            2,168830






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Full-time equivalent

                                さくらももこ

                                13 indicted, 8 arrested in Calif. drug cartel investigation