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
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
Ishana Malhrat wrote:So you recommend creator 3 new classes, one called checkerboard, one called player, and one called game?
All things are lawful, but not all things are profitable.
Ishana Malhrat wrote:so I found this code as solution for moving kings backwards and forwards
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 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
Ishana Malhra wrote:I found this code as solution ..., but I am unable to figure out how to implement this into the program above
A problem clearly stated is a problem half solved —Dorothea Brande
The details are not the details; they make the design. —Charles Eames
The details are not the details; they make the product. —Charles & Ray Eames
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:Junilu Lacar wrote:. . .. . .
No maybe about it, that is much betterJunilu Lacar wrote:. . .
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |