• Post Reply Bookmark Topic Watch Topic
  • New Topic

Same method with same parameters returning different values depending on placement?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a chess program, with a main class that handles the visual aspect of the game, and a secondary class (that I'm calling RuleBook) that handles the processing part. Specifically, the rulebook has a method, that I'm calling fullyReviewMove, that is supposed to determine the legality of a move in the main class. fullyReviewMove is based on two other methods in the RuleBook class, reviewMove (a surface review, based just on move capabilities), and reviewPosition (to determine if a seemingly legal move moved someone into check).

So anyone, the problem I'm currently experiencing is that when I'm trying to check the legality of a move in the main class, the move is always coming out as false, even when it's legal. However, even weirder, when I print out what the fullyReviewMove in the main class, it prints it out as true. Right after that, I use the same method and it comes out as false when it's actually used for processing. It sounds confusing, but hopefully I can make it clearer in the code.

//this is where I'm printing out what the various RuleBook methods are returning
JOptionPane.showMessageDialog(null, "reviewMove returns..." + (ref.reviewMove(position1, piece, move, whiteMove) == false)); //outputting true
JOptionPane.showMessageDialog(null, "reviewPosition returns..." + ref.reviewPosition(position2, pieces2, whiteMove)); //outputting "SNAFU"
JOptionPane.showMessageDialog(null, "fullyReviewMove returns..." + ref.fullyReviewMove(position1, position2, pieces1, pieces2, piece, move, whiteMove)); //outputting false //even though all the fullyReviewMove method is is an if statement saying that if reviewMove is true and if reviewPosition outputs SNAFU< then to return true

//here's where I'm actually using the method for processing
if (ref.fullyReviewMove(position1, position2, pieces1, pieces2, piece, move, whiteMove)==true) //legal move
{} else {}


//here's the fully reviewReviewMove method from the other class
//in it I again check reviewMove and reviewPosition by printing them out to the screen, but this time reviewMove comes out false despite having the same parameters

public boolean fullyReviewMove(Dimension3[][] position1, Dimension3[][] position2, Dimension[][] pieces1, Dimension[][] pieces2, Dimension3 piece, String move, boolean whiteMove) //full review, accounting for piece capabilities and check
{
boolean legal;
JOptionPane.showMessageDialog(null, "reviewMove returns... ..." + reviewMove(position1, piece, move, whiteMove)); //outputting false even though true in other class
JOptionPane.showMessageDialog(null, "reviewPosition returns... ..." + reviewPosition(position2, pieces2, whiteMove));
if ((reviewMove(position1, piece, move, whiteMove) == true) && //legal move for that piece (ex: bishops only diagonal)
(reviewPosition(position2, pieces2, whiteMove).equals("SNAFU")) //no check or checkmate
)
{
legal = true;
} else
{
legal = false;
}
//JOptionPane.showMessageDialog(null, "legal = " + legal);
return legal;
}

Obviously I'm not showing the entire program here, since it's too long, but any ideas as to why the reviewPosition method would be returning true at one part in the program, and false in another part, even though I've checked, and the parameters are the same both times?
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you put your code in properly indented way?
'Code' tab provided can be used for that.

Manish
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:...any ideas as to why the reviewPosition method would be returning true at one part in the program, and false in another part, even though I've checked, and the parameters are the same both times?


One possibility is that the method is using something other than the parameters, for example variables which are part of the object's state, and that "something" is different between the two times you checked.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:Obviously I'm not showing the entire program here...

But what you are showing is not easy to read, because you haven't put code tags around it.

But before you do that, please read the DontWriteLongLines page. Your lines are FAR too long for our code windows, so if you simply add code tags, your thread will still be very difficult to read - which is the main reason I didn't do it myself.

Remember: the rule for code here is:
80 characters max/line.
(the SSCCE page actually recommends 62)
And that includes String literals AND comments.

For information on adding code tags, read the UseCodeTags page.

Thanks.

Winston
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, next time I'll use the code tags. In fact, I just used them on another post, very helpful. Also, I figured out the problem. In one class I was printing out the statement (reviewMove()==false), which was true, while in the other class I was printing out reviewMove(), which was false. I meant to just print out reviewMove() in both, but mistyped.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet another reason why you shouldn't use "== true" and "== false" in your code.

If you've got something with a boolean value -- like your reviewMove method for example -- then you can and should just use it anywhere a boolean value is expected. For example:


rather than


However that works better if your boolean value actually has a name which makes it obvious that it's a boolean value. Your name here doesn't; I wouldn't expect "reviewMove" to return any value. If it's supposed to tell the caller whether the move is valid then "isValidMove" would be a better name. Then your code would be


which makes more sense to a person who isn't extremely familiar with the code. (Such as you, six months from now!)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!