• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checkers Engine Java - King and Moving Backwards

 
Greenhorn
Posts: 9
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I can't seem to be able to move my checkers pieces backwards in this JavaFX checkers engine.


Any idea on how to let them go backwards as I need it to implement the kings?
Thanks!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It took me a few minutes to decipher what this code is doing; it's a perfect example of how to drown out the intent of the code with lots of noisy details.

Here's what I can figure out so far:

The above code would be what I would be left with after extracting and composing the original code so that it's at least understandable at a glance rather than after a few minutes of deciphering.  Now that we have a clearer picture of what the code is doing, what exactly would it mean in terms of X and Y positions on the board for a King piece to move backwards?
 
Shaun Khundker
Greenhorn
Posts: 9
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really sorry about that, should have commented what they do. It's a bad habit. For a red piece to move forward, the y coordinates are 1 while backwards, it is -1. For white piece it is the other way around, hope that answers your question!
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of class are you using for the Man?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if I were doing this, I'd probably refactor moveDir into two concepts: advance and retreat

That is, I'd probably have something like this:

On seeing that, you should recognize a responsibility that needs to be moved. I would then refactor to something like:

Something like that.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just writing comments won't cut it. Junilu's point is that the code should be clear by itself.

If I were you, I would create a Position class that easily lets you find neighboring positions in diagonal directions:
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like the Position class idea. That would make the previous code I posted take a Position object instead of newX and newY for the parameters. Code would look a lot cleaner.
 
Shaun Khundker
Greenhorn
Posts: 9
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will try out both and let you know how it goes. Thanks!
 
Shaun Khundker
Greenhorn
Posts: 9
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've figured it out, thanks guys!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us your solution so we can admire it and it might help somebody else in future.
 
Shaun Khundker
Greenhorn
Posts: 9
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't make a position class as I already had the piece position and its relation in my piece class. I just realised that moveDir was the one that I needed to change in order to make it move backwards and/or forwards. This is just in the meantime, I am hoping to improve the code in the future and also add a minimax algorithm for AI. If there are improvements that could be made, please do let me know! Thanks!

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 55 in the last code listing you posted will never be executed. Also, the second check is redundant.
 
Shaun Khundker
Greenhorn
Posts: 9
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh right, thanks for pointing that out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic