Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Help with a chess program

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having some trouble with a chess program. My validMove method does not work correctly. It's forcing all the pieces to move like a king. I think it has something to do with the way I'm passing String piece to the function, but everything I have tried to fix it has not worked.

 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your immediate problem is that you are comparing String values incorrectly. This:

compares to String references to see if they refer to the same object. If instead you want to compare two String references to see if their String objects contain the same sequence of characters -- which they could do even if they are two different String objects -- then do this:

I could go on and suggest that your idea of using Strings to represent your pieces is short-sighted. Instead you could have a Piece class which implements the validMove() method, with subclasses for each of the types of piece each of which implements that method in the appropriate way. But that's perhaps something to do for Version 2 of the game.
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That might help when the correct variable reaches the validMove mehtod. The problem is that the validMove method always gets piece as null. Since it doesn't match any of the strings, it treats the piece as a king.
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't that have been a good thing to state at the outset? You really should probably read some of the How to ask Questions. specifically, I'd suggest AvoidRedHerrings, TellTheDetails, and IsolateTheProblem.
reply
    Bookmark Topic Watch Topic
  • New Topic