• 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 Moving King backwards and forwards

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using this code how do you make a method where once the checkers reach the end they are able to move both forward and backward? do you have to make each checker piece into an object? and once you do that what is the code to make it move backwards. thank you I'm really confused


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

using this code how do you make a method where once the checkers reach the end they are able to move both forward and backward? do you have to make each checker piece into an object? and once you do that what is the code to make it move backwards. thank you I'm really confused  


Sounds like you have good intuition.  Making a Checker class sounds like a good idea.  Let's think about that first and worry about the backward movement later.

What would a Checker class have as fields?  We know a few:

* color
* isKing

Anything else?  How would you change your code so that it has a checker board with Checker object in it?  Do you need any other classes?

Right now you are checking if the x or y difference is negative or not, depending on the color of the player.  Do you need that if the checker is a King?
 
Hollie Hunter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you say checkers class do you mean a whole other class or just another method in this existing Checkers3 class?
Also how do you make checkers into objects?
Thank you
 
Hollie Hunter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry the class name is Checkers not Checkers3.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant a new class; Checker (singular).  Maybe that doesn't make sense.  Is one checker called a piece?

But yeah, another class.  Other possible classes: CheckerBoard, Player, Game.  Whether you create a class or not should be determined by whether it makes the code simpler.  Note that this does not necessarily mean shorter (less code) but clearer.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you recommend creator 3 new classes, one called checkerboard, one called player, and one called game?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:. . . Is one checker called a piece? . . .

Culturally‑loaded question On this side of the Pond the game is called draughts and a piece is called a draughtsman, so you might call each piece a Man.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ishana Malhrat wrote:So you recommend creator 3 new classes, one called checkerboard, one called player, and one called game?


Welcome to the Ranch, Ishana Malhrat.

I definitely recommend a class called Checker or Piece or Daughtman for one piece.  That other class suggestions are just that.  I want to get the OP thinking about things like:

* If I had a CheckerBoard class, would that make my code easier to code and understand?
* What fields and methods would a CheckerBoard class have?  An array of arrays for the board? A method called thereAreNoMoves()?
* etc.

The same thought process would go on for a possible Player and Game class.
 
Ishana Malhrat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so I found this code as solution for moving kings backwards and forwards, but I am unable to figure out how to implement this into the program above. Can someone please help me?
 
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

Ishana Malhrat wrote:so I found this code as solution for moving kings backwards and forwards


It's not a very good solution.

First of all, it's way too long and it does way too many things.

Second, it's procedural "polymorphism" -- it uses an if-else statement to determine behavior of the checker piece based on its "isKing" state.  Moreover, this doesn't make much sense:

The last else block will never get executed since the if and else-if part already cover all possible cases.

A more object-oriented approach would let the object itself determine a strategy for moving. Having said that, in fact, a "move strategy" would be a good candidate object for this problem. That is, when a piece starts out in the game, it would have a MoveStrategy implementation that encapsulates the rules for moving as a regular piece. When it gets to the opposite end of the board, its MoveStrategy would be changed to an implementation that encapsulates the rules for moving as a King piece.
 
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
This is actually a very good exercise for learning object-oriented design and programming. I think it's also interesting to see these first designs being more procedural than OO even though the language supports OOP and there's an attempt to think in terms of objects.
 
Ishana Malhrat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Ishana Malhrat wrote:so I found this code as solution for moving kings backwards and forwards


It's not a very good solution.

First of all, it's way too long and it does way too many things.

Second, it's procedural "polymorphism" -- it uses an if-else statement to determine behavior of the checker piece based on its "isKing" state.  Moreover, this doesn't make much sense:

The last else block will never get executed since the if and else-if part already cover all possible cases.

A more object-oriented approach would let the object itself determine a strategy for moving. Having said that, in fact, a "move strategy" would be a good candidate object for this problem. That is, when a piece starts out in the game, it would have a MoveStrategy implementation that encapsulates the rules for moving as a regular piece. When it gets to the opposite end of the board, its MoveStrategy would be changed to an implementation that encapsulates the rules for moving as a King piece.

 
Ishana Malhrat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im in a beginner level java class so I'm not sure how to do the things you are talking about can you elaborate please
 
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

Ishana Malhrat wrote:im in a beginner level java class so I'm not sure how to do the things you are talking about can you elaborate please


This is a problem with your instruction, which seems typical of the type of instruction many schools offer these days. You are asked to create something using a specific tool, Java. That would be fine if it weren't for the fact that they don't seem to be equipping you with the proper skills and knowledge of how to use that tool to create what they want you to create. The skills and knowledge you lack relate to object-oriented thinking and being able to plan and organize your solution around objects.  It's like dumping a pile of car parts in front of you and telling you "Here's a bunch of stuff you'll need to build a car. Here's a bunch of tools that you'll need to put it all together. Now, go ahead and build me a car in two days." This would be fine if you were an experienced engineer and mechanic. However, you're not. You're just someone who's only just learning the basics of how to use the tools but have no idea about mechanics, dynamics, electronics, pneumatics, hydraulics, aerodynamics, physics, thermodynamics, and everything else one needs to know in order to design and build a car.

I realize this is not very helpful for you and it will probably only make you feel worse about your current predicament but it's also frustrating to watch folks like you struggle and try to figure out something that you're just not well-equipped to figure out.

Ishana Malhra wrote:I found this code as solution ..., but I am unable to figure out how to implement this into the program above


You have to realize this: That code was not written to be implemented into your program. You can't just simply copy someone else's code and expect that it will fit into what you have written. Your code and the code you found don't share the same design context. It's like trying to fit engine parts meant for a Toyota into a Chevy. Sure, they're both car parts but they are not designed to be interchangeable.

There is, however, something you might still be able to salvage out of it. Those are some of the ideas in that code about how to treat data and the behavior that's related to it. I don't agree with them but you can at least look at what's wrong and say "Well, that's wrong but if we think about it this other way..."

So, I think your first task here is to try to clearly state the ideas in this code that you found. I'll give you an example of one. What idea does the following line of code represent?

Try to express that in plain English. If you can express that in plain English, then you can try to see if it fits into the ideas you have in your code. If the idea fits, then you need to be able to express that in Java, in the context of your design.
 
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
If you're struggling to express the idea behind that code in plain English, let me give you another example, this time from the code that you wrote.

The ideas codified here are related to "when is the game over" and "who won the game," right?

In plain English, the code that you wrote in the gameOver() method says "The game is over when one of the players has no more pieces left on the board."  Notice how I expressed that in a way that did not mention any of the variables you defined in your code. Typically, when a student is asked to express the idea behind this kind of code, they will say something like "The game is over when the variable redcheckers is zero OR when the variable blackcheckers is zero." That is NOT the idea, that is the implementation.

Given that, can you express the idea behind the winnerIs() method that you wrote? If you can do that without referring to any of the implementation details, then you'll have a better idea of how to express that line from the code that you want to "borrow" in plain English.
 
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
If you're wondering where I'm trying to lead you with all this, it has to do with the following quote.

A problem clearly stated is a problem half solved —Dorothea Brande


If you can state your problem and solution clearly and succinctly in plain, everyday language, then translating those ideas into Java is not going to be as difficult. Right now you're confused because you have a bunch of disorganized and incoherent ideas floating around in your head and you have tried to codify them in your Java program. The state of the code you wrote reflects the state of your thoughts. You need to stop, organize your thoughts, come up with a plan, and then start expressing that plan clearly, first in plain, everyday language. Then only can you try to translate your ideas into Java.

The caveat to that, however, is that you have to do the translation to Java in small increments. Trying to express everything, including the minute details, in plain language first is not a very good approach either because it often leads to what we in the industry call "analysis and design paralysis."  In my experience, a more practical approach is to define a high-level context first, like a summary or outline of a story, and then do a bunch of experiments to flesh out the details, expressing smaller ideas and trying to see how well they translate to code. Through experimentation, you see what works and what doesn't. By doing only small increments, you can keep mistakes (and yes, you will make lots of them) small, manageable, and easier to recover from.

So, starting from a high-level context expressed in plain language, you slowly work your way down into the details expressed as Java code. This brings me to another quote I discovered just the other night:

The details are not the details; they make the design. —Charles Eames


Although the official Eames website gives the actual quote as

The details are not the details; they make the product. —Charles & Ray Eames



And that connects very nicely to what a guy named Jack Reeves wrote about Code as Design who proposed that writing code is a design activity.
 
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
Sorry to blather on about a lot of stuff you probably have no idea or interest about. I do have a bad tendency to get long-winded about these kinds of things. You may not understand any of it now but they are important ideas that will help you in the future, if you decide that programming is something you want to do professionally.

Anyway, reeling things back to what you might be able to actually use, the code in your main() method is actually not too bad. It's a pretty decent overview of what your program is trying to accomplish.

The next level of detail is where it kind of gets messy though:

The first thing you need to fix here is the indentation. What you currently have is misleading and confusing. Indenting code properly helps you see the program's structure and understand the flow better. Properly formatted, this is what the method should look like:

This method has way too many details. There are too many details because it's trying to express multiple ideas.  If you want to express multiple ideas in a single method, you need to make it a Composed Method, like how you wrote your main() method. It's not a good idea to have multiple ideas and all the detailed code that expresses those ideas mixed together in one method. That just makes things too confusing.
 
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
Here's what your getNextMove() would look like if it were a more Composed method:

Compare line 61 above with what you had before:

Do you see how your plain language comment was translated into actual code? Do you see how your comment is no longer even needed? This may seem like a trivial change in that it doesn't even change the way the program behaves. However, it's a HUGE change in terms of your program's organization and clarity. Doing this helps you gather your ideas and organize your thoughts along more coherent and precise lines.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . .. . .

That is where an array would be useful. You have a two‑element array and you can update the nextPlayer field with the ++ operator.That will suffer an arithmetic overflow and crash with negative array indices if you take > 10³¹ moves. Since draughts is always played between two people, you can short‑circuit the % operator with bitwise AND, and that never produces a negative result:
nextPlayer = players[++moveCount & 1];
No () required because ++ has a higher precedence than & and than %. Use preincrement if WHITE takes the first move. Depending on the location of moveCount, that will permit the losing player to take the first move in the following game.
 
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
There are many ways you can do it. You could also toggle a single Boolean variable. But to think in terms of those kinds of details so quickly is again falling into the implementation trap. The idea behind it is switch turns. That is, if it is one player's turn now, the next move will be made by the other player. This works because the game is a two-player game.

The array and the Boolean toggle are still procedural though. If you want to write OO code, you should call a method of an object to switch turns. Perhaps something like:
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . .

No maybe about it, that is much better
 
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
Yes, it is, isn't it?  

And that goes back to my point that everyone's first design(s) will most likely not be the best choices. The best one is often only revealed/discovered through experimentation with different options to see how they work. You also need to develop a sense for when there might be a better way. The only way to develop that sense that I know of is through constant practice and by understanding and recognizing the difference between object thinking and non-object thinking.

The order in which I wrote those three options is the order in which they came to me in my head. That also goes back to my observation that it seems most people, including myself, think procedurally even when they try to think in terms of objects. The progression is very seldom ever the other way around where you come up with the more OO option(s) first and the more procedural ones later. Seems one cannot escape Murphy's Law: You always find what you're looking for last.
 
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
Here's another observation. I used to take a lot of pride in being able to think like a compiler and write code that didn't have any compile-time or logic errors on the first try. While I still can do that, I am more suspicious of code that's written that way. Nowadays, I treat code that hasn't failed in some way through rigorous testing, that hasn't gone through a few iterations of refactoring, and/or hasn't been compared to other options as suspicious. Code like that is almost invariably code that will come back and give me trouble later.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic