boolean method does not behave as expected and returns false when true is expected
up vote
0
down vote
favorite
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
up vote
0
down vote
favorite
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
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
260k20210316
260k20210316
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
up vote
0
down vote
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 |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 |
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