boolean method does not behave as expected and returns false when true is expected
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
|
show 1 more comment
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
3
What isboard
? What isFREE
? 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 meanif (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 theprintln
and thereturn 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
|
show 1 more comment
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
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
java
edited Nov 11 at 3:08
Hovercraft Full Of Eels
261k20211317
261k20211317
asked Nov 11 at 3:03
wangmeister
1
1
3
What isboard
? What isFREE
? 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 meanif (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 theprintln
and thereturn 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
|
show 1 more comment
3
What isboard
? What isFREE
? 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 meanif (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 theprintln
and thereturn 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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 11 at 3:16
Sajal Preet Singh
220110
220110
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%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
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
3
What is
board
? What isFREE
? 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 thereturn 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