• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checkers Engine Java - King and Moving Backwards  RSS feed

 
Shaun Khundker
Greenhorn
Posts: 9
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of class are you using for the Man?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will try out both and let you know how it goes. Thanks!
 
Shaun Khundker
Greenhorn
Posts: 9
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've figured it out, thanks guys!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right, thanks for pointing that out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!