boolean method does not behave as expected and returns false when true is expected












0














Here's the code in question:



public boolean validMoveRook(int start, int end) {

int xDif = end[0]-start[0];
int yDif = end[1]-start[1];

if (xDif == 0 ^ yDif == 0) {

int distance;
int direction;

if (xDif == 0) {
distance = Math.abs(yDif);
direction = 1;
}
else {
distance = Math.abs(xDif);
direction = 0;
}

for (int i = distance - 1; i >= 0; i--) {
if (direction == 0) {
int newX = start[0] + utilities.AbsoluteChange(xDif, -i);
if (this.board[newX][start[1]] != FREE) return false;
}
if (direction == 1) {
int newY = start[1] + utilities.AbsoluteChange(yDif, -i);
if (this.board[start[0]][newY] != FREE) return false;
}

if (this.board[start[0]][start[1]] == TURN0_WHITEROOK) {
this.board[start[0]][start[1]] = WHITEROOK;
}
if (this.board[start[0]][start[1]] == TURN0_BLACKROOK) {
this.board[start[0]][start[1]] = BLACKROOK;
}
}
System.out.println("success");
return true;
}
System.out.println("Oh no!");
return false;
}


I'm trying to make a chess game. The issue is that it keeps returning false even when it shouldn't. "success" is printed, but it still returns false. Why is this?










share|improve this question




















  • 3




    What is board? What is FREE? Please post a Minimal, Complete, and Verifiable example. Also this appears to be a great opportunity to use your debugging skills and figure out what the problem is
    – GBlodgett
    Nov 11 at 3:05








  • 1




    Did you actually mean if (xDif == 0 ^ yDif == 0) ?
    – nullpointer
    Nov 11 at 3:06












  • board is the 2d array of characters that acts as the chessboard. FREE is just an unobtrusive symbol that is printed in the empty squares. The AbsoluteChange method takes the absolute value of the first value, adds the second value, and then returns that with the original sign.
    – wangmeister
    Nov 11 at 3:07












  • What makes you think it is returning false? The fact that the println and the return true; are two consecutive lines makes me think that either the printing is happening somewhere else or the method really is returning true
    – GBlodgett
    Nov 11 at 3:10






  • 1




    I would suggest running the code with just the code necessary to call this method and see what happens.
    – GBlodgett
    Nov 11 at 3:18
















0














Here's the code in question:



public boolean validMoveRook(int start, int end) {

int xDif = end[0]-start[0];
int yDif = end[1]-start[1];

if (xDif == 0 ^ yDif == 0) {

int distance;
int direction;

if (xDif == 0) {
distance = Math.abs(yDif);
direction = 1;
}
else {
distance = Math.abs(xDif);
direction = 0;
}

for (int i = distance - 1; i >= 0; i--) {
if (direction == 0) {
int newX = start[0] + utilities.AbsoluteChange(xDif, -i);
if (this.board[newX][start[1]] != FREE) return false;
}
if (direction == 1) {
int newY = start[1] + utilities.AbsoluteChange(yDif, -i);
if (this.board[start[0]][newY] != FREE) return false;
}

if (this.board[start[0]][start[1]] == TURN0_WHITEROOK) {
this.board[start[0]][start[1]] = WHITEROOK;
}
if (this.board[start[0]][start[1]] == TURN0_BLACKROOK) {
this.board[start[0]][start[1]] = BLACKROOK;
}
}
System.out.println("success");
return true;
}
System.out.println("Oh no!");
return false;
}


I'm trying to make a chess game. The issue is that it keeps returning false even when it shouldn't. "success" is printed, but it still returns false. Why is this?










share|improve this question




















  • 3




    What is board? What is FREE? Please post a Minimal, Complete, and Verifiable example. Also this appears to be a great opportunity to use your debugging skills and figure out what the problem is
    – GBlodgett
    Nov 11 at 3:05








  • 1




    Did you actually mean if (xDif == 0 ^ yDif == 0) ?
    – nullpointer
    Nov 11 at 3:06












  • board is the 2d array of characters that acts as the chessboard. FREE is just an unobtrusive symbol that is printed in the empty squares. The AbsoluteChange method takes the absolute value of the first value, adds the second value, and then returns that with the original sign.
    – wangmeister
    Nov 11 at 3:07












  • What makes you think it is returning false? The fact that the println and the return true; are two consecutive lines makes me think that either the printing is happening somewhere else or the method really is returning true
    – GBlodgett
    Nov 11 at 3:10






  • 1




    I would suggest running the code with just the code necessary to call this method and see what happens.
    – GBlodgett
    Nov 11 at 3:18














0












0








0







Here's the code in question:



public boolean validMoveRook(int start, int end) {

int xDif = end[0]-start[0];
int yDif = end[1]-start[1];

if (xDif == 0 ^ yDif == 0) {

int distance;
int direction;

if (xDif == 0) {
distance = Math.abs(yDif);
direction = 1;
}
else {
distance = Math.abs(xDif);
direction = 0;
}

for (int i = distance - 1; i >= 0; i--) {
if (direction == 0) {
int newX = start[0] + utilities.AbsoluteChange(xDif, -i);
if (this.board[newX][start[1]] != FREE) return false;
}
if (direction == 1) {
int newY = start[1] + utilities.AbsoluteChange(yDif, -i);
if (this.board[start[0]][newY] != FREE) return false;
}

if (this.board[start[0]][start[1]] == TURN0_WHITEROOK) {
this.board[start[0]][start[1]] = WHITEROOK;
}
if (this.board[start[0]][start[1]] == TURN0_BLACKROOK) {
this.board[start[0]][start[1]] = BLACKROOK;
}
}
System.out.println("success");
return true;
}
System.out.println("Oh no!");
return false;
}


I'm trying to make a chess game. The issue is that it keeps returning false even when it shouldn't. "success" is printed, but it still returns false. Why is this?










share|improve this question















Here's the code in question:



public boolean validMoveRook(int start, int end) {

int xDif = end[0]-start[0];
int yDif = end[1]-start[1];

if (xDif == 0 ^ yDif == 0) {

int distance;
int direction;

if (xDif == 0) {
distance = Math.abs(yDif);
direction = 1;
}
else {
distance = Math.abs(xDif);
direction = 0;
}

for (int i = distance - 1; i >= 0; i--) {
if (direction == 0) {
int newX = start[0] + utilities.AbsoluteChange(xDif, -i);
if (this.board[newX][start[1]] != FREE) return false;
}
if (direction == 1) {
int newY = start[1] + utilities.AbsoluteChange(yDif, -i);
if (this.board[start[0]][newY] != FREE) return false;
}

if (this.board[start[0]][start[1]] == TURN0_WHITEROOK) {
this.board[start[0]][start[1]] = WHITEROOK;
}
if (this.board[start[0]][start[1]] == TURN0_BLACKROOK) {
this.board[start[0]][start[1]] = BLACKROOK;
}
}
System.out.println("success");
return true;
}
System.out.println("Oh no!");
return false;
}


I'm trying to make a chess game. The issue is that it keeps returning false even when it shouldn't. "success" is printed, but it still returns false. Why is this?







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:08









Hovercraft Full Of Eels

261k20211317




261k20211317










asked Nov 11 at 3:03









wangmeister

1




1








  • 3




    What is board? What is FREE? Please post a Minimal, Complete, and Verifiable example. Also this appears to be a great opportunity to use your debugging skills and figure out what the problem is
    – GBlodgett
    Nov 11 at 3:05








  • 1




    Did you actually mean if (xDif == 0 ^ yDif == 0) ?
    – nullpointer
    Nov 11 at 3:06












  • board is the 2d array of characters that acts as the chessboard. FREE is just an unobtrusive symbol that is printed in the empty squares. The AbsoluteChange method takes the absolute value of the first value, adds the second value, and then returns that with the original sign.
    – wangmeister
    Nov 11 at 3:07












  • What makes you think it is returning false? The fact that the println and the return true; are two consecutive lines makes me think that either the printing is happening somewhere else or the method really is returning true
    – GBlodgett
    Nov 11 at 3:10






  • 1




    I would suggest running the code with just the code necessary to call this method and see what happens.
    – GBlodgett
    Nov 11 at 3:18














  • 3




    What is board? What is FREE? Please post a Minimal, Complete, and Verifiable example. Also this appears to be a great opportunity to use your debugging skills and figure out what the problem is
    – GBlodgett
    Nov 11 at 3:05








  • 1




    Did you actually mean if (xDif == 0 ^ yDif == 0) ?
    – nullpointer
    Nov 11 at 3:06












  • board is the 2d array of characters that acts as the chessboard. FREE is just an unobtrusive symbol that is printed in the empty squares. The AbsoluteChange method takes the absolute value of the first value, adds the second value, and then returns that with the original sign.
    – wangmeister
    Nov 11 at 3:07












  • What makes you think it is returning false? The fact that the println and the return true; are two consecutive lines makes me think that either the printing is happening somewhere else or the method really is returning true
    – GBlodgett
    Nov 11 at 3:10






  • 1




    I would suggest running the code with just the code necessary to call this method and see what happens.
    – GBlodgett
    Nov 11 at 3:18








3




3




What is board? What is FREE? Please post a Minimal, Complete, and Verifiable example. Also this appears to be a great opportunity to use your debugging skills and figure out what the problem is
– GBlodgett
Nov 11 at 3:05






What is board? What is FREE? Please post a Minimal, Complete, and Verifiable example. Also this appears to be a great opportunity to use your debugging skills and figure out what the problem is
– GBlodgett
Nov 11 at 3:05






1




1




Did you actually mean if (xDif == 0 ^ yDif == 0) ?
– nullpointer
Nov 11 at 3:06






Did you actually mean if (xDif == 0 ^ yDif == 0) ?
– nullpointer
Nov 11 at 3:06














board is the 2d array of characters that acts as the chessboard. FREE is just an unobtrusive symbol that is printed in the empty squares. The AbsoluteChange method takes the absolute value of the first value, adds the second value, and then returns that with the original sign.
– wangmeister
Nov 11 at 3:07






board is the 2d array of characters that acts as the chessboard. FREE is just an unobtrusive symbol that is printed in the empty squares. The AbsoluteChange method takes the absolute value of the first value, adds the second value, and then returns that with the original sign.
– wangmeister
Nov 11 at 3:07














What makes you think it is returning false? The fact that the println and the return true; are two consecutive lines makes me think that either the printing is happening somewhere else or the method really is returning true
– GBlodgett
Nov 11 at 3:10




What makes you think it is returning false? The fact that the println and the return true; are two consecutive lines makes me think that either the printing is happening somewhere else or the method really is returning true
– GBlodgett
Nov 11 at 3:10




1




1




I would suggest running the code with just the code necessary to call this method and see what happens.
– GBlodgett
Nov 11 at 3:18




I would suggest running the code with just the code necessary to call this method and see what happens.
– GBlodgett
Nov 11 at 3:18












1 Answer
1






active

oldest

votes


















0














As @Gblodgett pointed out, it's quite possible that you have a Sop("success") in your utilities.AbsoluteChange method and the false is being returned just after that call.






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',
    autoActivateHeartbeat: false,
    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%2f53245492%2fboolean-method-does-not-behave-as-expected-and-returns-false-when-true-is-expect%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As @Gblodgett pointed out, it's quite possible that you have a Sop("success") in your utilities.AbsoluteChange method and the false is being returned just after that call.






    share|improve this answer


























      0














      As @Gblodgett pointed out, it's quite possible that you have a Sop("success") in your utilities.AbsoluteChange method and the false is being returned just after that call.






      share|improve this answer
























        0












        0








        0






        As @Gblodgett pointed out, it's quite possible that you have a Sop("success") in your utilities.AbsoluteChange method and the false is being returned just after that call.






        share|improve this answer












        As @Gblodgett pointed out, it's quite possible that you have a Sop("success") in your utilities.AbsoluteChange method and the false is being returned just after that call.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 3:16









        Sajal Preet Singh

        220110




        220110






























            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%2f53245492%2fboolean-method-does-not-behave-as-expected-and-returns-false-when-true-is-expect%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini